This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
helium.go
166 lines (135 loc) · 3.08 KB
/
helium.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package helium
import (
"context"
"fmt"
stdlog "log"
"os"
"reflect"
"strings"
"go.uber.org/atomic"
"go.uber.org/dig"
"go.uber.org/zap"
"github.com/im-kulikov/helium/logger"
"github.com/im-kulikov/helium/module"
"github.com/im-kulikov/helium/settings"
)
type (
// App implementation for helium.
App interface {
Run(ctx context.Context) error
}
// Helium struct.
Helium struct {
di *dig.Container
}
// Settings struct.
Settings struct {
File string
Type string
Name string
Prefix string
BuildTime string
BuildVersion string
Defaults settings.Defaults
}
)
var (
// nolint:gochecknoglobals
appName = atomic.NewString("helium")
// nolint:gochecknoglobals
appVersion = atomic.NewString("dev")
)
// New helium instance.
func New(cfg *Settings, mod ...module.Module) (*Helium, error) {
h := &Helium{di: dig.New()}
modules := module.Combine(mod...)
if cfg != nil {
if cfg.Prefix == "" {
cfg.Prefix = cfg.Name
}
cfg.Prefix = strings.ToUpper(cfg.Prefix)
if tmp := os.Getenv(cfg.Prefix + "_CONFIG"); tmp != "" {
cfg.File = tmp
}
if tmp := os.Getenv(cfg.Prefix + "_CONFIG_TYPE"); tmp != "" {
cfg.Type = tmp
}
core := settings.Core{
File: cfg.File,
Type: cfg.Type,
Name: cfg.Name,
Prefix: cfg.Prefix,
BuildTime: cfg.BuildTime,
BuildVersion: cfg.BuildVersion,
}
appName.Store(cfg.Name)
appVersion.Store(cfg.BuildVersion)
modules = append(modules, core.Provider())
modules = append(modules, settings.DIProvider(h.di))
}
if err := module.Provide(h.di, modules); err != nil {
return nil, err
}
if cfg == nil || cfg.Defaults == nil {
return h, nil
}
return h, h.di.Invoke(cfg.Defaults)
}
// Invoke dependencies from DI container.
func (h Helium) Invoke(fn interface{}, args ...dig.InvokeOption) error {
return h.di.Invoke(fn, args...)
}
// Run trying invoke app instance from DI container and start app with Run call.
func (h Helium) Run() error {
return h.di.Invoke(func(ctx context.Context, app App) error {
return app.Run(ctx)
})
}
// Catch errors.
func Catch(err error) {
if err == nil {
return
}
log, logErr := logger.
NewLogger(logger.NewLoggerConfig(settings.Viper()), &settings.Core{
Name: appName.Load(),
BuildVersion: appVersion.Load(),
})
if logErr != nil {
stdlog.Fatal(err)
} else {
log.Fatal("Can't run app",
zap.Error(err))
}
}
// CatchTrace catch errors for debugging use that function just for debug your application.
func CatchTrace(err error) {
if err == nil {
return
}
// digging into the root of the problem
loop:
for {
var (
ok bool
v = reflect.ValueOf(err)
fn reflect.Value
)
switch {
case v.Type().Kind() != reflect.Struct,
!v.FieldByName("Reason").IsValid():
break loop
case v.FieldByName("Func").IsValid():
fn = v.FieldByName("Func")
}
// nolint:forbidigo
fmt.Printf("Place: %#v\nReason: %s\n\n", fn, err)
if err, ok = v.FieldByName("Reason").Interface().(error); ok {
continue
}
if err, ok = v.Interface().(error); ok {
break
}
}
panic(err)
}