-
Notifications
You must be signed in to change notification settings - Fork 3
/
bootstrap.go
217 lines (170 loc) · 4.79 KB
/
bootstrap.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package snout
import (
"context"
"fmt"
"os/signal"
"reflect"
"strings"
"syscall"
"time"
"github.com/go-playground/validator/v10"
"github.com/mitchellh/mapstructure"
"github.com/octago/sflags"
"github.com/octago/sflags/gen/gpflag"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
type Kernel struct {
RunE interface{}
}
type env struct {
VarFile string
VarsPrefix string
}
type kernelOptions struct {
ServiceName string
Env env
}
func newKernelOptions() *kernelOptions {
return &kernelOptions{
ServiceName: "",
Env: env{
VarFile: ".",
VarsPrefix: "",
},
}
}
// WithServiceName creates a profile based on the service name to look up for envVar files
func WithServiceName(name string) Options {
return func(kernel *kernelOptions) {
kernel.ServiceName = name
}
}
// WithEnvVarPrefix strips any prefix from os EnvVars to map it into Config struct.
func WithEnvVarPrefix(prefix string) Options {
return func(kernel *kernelOptions) {
kernel.Env.VarsPrefix = prefix
}
}
// WithEnvVarFolderLocation Specify where to look up form the env var file.
func WithEnvVarFolderLocation(folderLocation string) Options {
return func(kernel *kernelOptions) {
kernel.Env.VarFile = folderLocation
}
}
type Options func(kernel *kernelOptions)
// Bootstrap service creating a Ctx with Signalling and fetching EnvVars from
// env, yaml or json file, or straight from envVars from the OS.
func (k *Kernel) Bootstrap(ctx context.Context, cfg interface{}, opts ...Options) kernelBootstrap {
krnlOpt := newKernelOptions()
for _, o := range opts {
o(krnlOpt)
}
ctx = signallingContext(ctx)
k.varFetching(cfg, krnlOpt)
return kernelBootstrap{ctx, cfg, k.RunE}
}
func (k Kernel) varFetching(cfg interface{}, options *kernelOptions) {
viper.SetEnvPrefix(options.Env.VarsPrefix)
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
flagSet := pflag.NewFlagSet(options.ServiceName, pflag.ContinueOnError)
if err := gpflag.ParseTo(cfg, flagSet, sflags.FlagDivider("."), sflags.FlagTag("snout")); err != nil {
panic(err)
}
if err := viper.BindPFlags(flagSet); err != nil {
panic(err)
}
viper.SetConfigName(options.ServiceName)
viper.AddConfigPath(options.Env.VarFile)
if err := viper.ReadInConfig(); err == nil {
fmt.Printf("Using config file: %s \n", viper.ConfigFileUsed())
}
setDefaultValues(reflect.TypeOf(cfg).Elem(), "")
if err := viper.Unmarshal(cfg, unmarshalWithStructTag("snout")); err != nil {
panic(err)
}
}
func setDefaultValues(p reflect.Type, path string) {
for i := 0; i < p.NumField(); i++ {
field := p.Field(i)
PathMap := map[bool]string{
true: strings.ToUpper(fmt.Sprintf("%s.%s", path, field.Tag.Get("snout"))),
false: strings.ToUpper(field.Tag.Get("snout")),
}
finalPath := PathMap[path != ""]
var typ reflect.Type
switch field.Type.Kind() {
case reflect.Ptr:
typ = field.Type.Elem()
default:
typ = field.Type
}
if typ.Kind() != reflect.Struct {
get := field.Tag.Get("default")
viper.SetDefault(finalPath, get)
continue
}
setDefaultValues(typ, finalPath)
}
}
func unmarshalWithStructTag(tag string) viper.DecoderConfigOption {
return func(config *mapstructure.DecoderConfig) {
config.TagName = tag
config.DecodeHook = mapstructure.ComposeDecodeHookFunc(customUnMarshallerHookFunc)
}
}
func customUnMarshallerHookFunc(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) {
if t.String() == "time.Duration" && f.Kind() == reflect.String {
var s string
if s = data.(string); s == "" {
s = "0s"
}
return time.ParseDuration(s)
}
return data, nil
}
func signallingContext(ctx context.Context) context.Context {
ctx, _ = signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
return ctx
}
type kernelBootstrap struct {
context context.Context
cfg interface{}
runE interface{}
}
var ErrPanic = fmt.Errorf("panic")
var ErrValidation = fmt.Errorf("validation error")
// Initialize Runs the Bootstrapped service
func (kb kernelBootstrap) Initialize() (err error) {
validate := validator.New()
if err = validate.Struct(kb.cfg); err != nil {
return fmt.Errorf("%w:%s", ErrValidation, err.Error())
}
typeOf := reflect.TypeOf(kb.runE)
if typeOf.Kind() != reflect.Func {
return fmt.Errorf("%s is not a reflect.Func", reflect.TypeOf(kb.runE))
}
defer func() {
if r := recover(); r != nil {
switch pErr := r.(type) {
case string:
err = fmt.Errorf("%w:%s", ErrPanic, pErr)
case error:
err = fmt.Errorf("%w:%v", ErrPanic, pErr)
default:
err = fmt.Errorf("%w:%+v", ErrPanic, pErr)
}
return
}
}()
var In []reflect.Value
In = append(In, reflect.ValueOf(kb.context))
In = append(In, reflect.ValueOf(kb.cfg).Elem())
call := reflect.ValueOf(kb.runE).Call(In)
err, ok := call[0].Interface().(error)
if !ok {
return nil
}
return err
}