/
application.go
398 lines (333 loc) · 9.11 KB
/
application.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package app
import (
"errors"
"fmt"
"log"
"net/url"
"os"
"os/signal"
"path/filepath"
goruntime "runtime"
"strings"
"sync"
"syscall"
"github.com/casualjim/go-app/logging"
"github.com/casualjim/go-app/tracing"
cjm "github.com/casualjim/middlewares"
"github.com/fsnotify/fsnotify"
"github.com/kardianos/osext"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
// we enable remote config providers by default
_ "github.com/spf13/viper/remote"
)
var (
// ErrModuleUnknown returned when no module can be found for the specified key
ErrModuleUnknown error
execName func() (string, error)
// Version of the application
Version string
)
func init() {
ErrModuleUnknown = errors.New("unknown module")
execName = osext.Executable
log.SetOutput(logrus.StandardLogger().Writer())
log.SetFlags(0)
}
// A Key represents a key for a module.
// Users of this package can define their own keys, this is just the type definition.
type Key string
// Application is an application level context package
// It can be used as a kind of dependency injection container
type Application interface {
// Add modules to the application context
Add(...Module) error
// Get the module at the specified key, thread-safe
Get(Key) interface{}
// Get the module at the specified key, thread-safe
GetOK(Key) (interface{}, bool)
// Set the module at the specified key, this should be safe across multiple threads
Set(Key, interface{}) error
// Logger gets the root logger for this application
Logger() logrus.FieldLogger
// NewLogger creates a new named logger for this application
NewLogger(string, logrus.Fields) logrus.FieldLogger
// Tracer returns the root
Tracer() tracing.Tracer
// Config returns the viper config for this application
Config() *viper.Viper
// Info returns the app info object for this application
Info() cjm.AppInfo
// Init the application and its modules with the config.
Init() error
// Start the application an its enabled modules
Start() error
// Stop the application an its enabled modules
Stop() error
}
func addDefaultConfigPaths(v *viper.Viper, name string) {
v.SetConfigName("config")
norm := strings.ToLower(name)
paths := filepath.Join(os.Getenv("HOME"), ".config", norm) + ":" + filepath.Join("/etc", norm) + ":etc:."
if os.Getenv("CONFIG_PATH") != "" {
paths = os.Getenv("CONFIG_PATH")
}
for _, path := range filepath.SplitList(paths) {
v.AddConfigPath(path)
}
}
var viperLock *sync.Mutex
func init() {
viperLock = new(sync.Mutex)
}
func createViper(name string, configPath string) (*viper.Viper, error) {
viperLock.Lock()
defer viperLock.Unlock()
v := viper.New()
if configPath == "" {
addDefaultConfigPaths(v, name)
} else {
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return nil, fmt.Errorf("No config file found at %s", configPath)
}
dir, fname := filepath.Split(configPath)
// viper wants the file name without extention...
suffixes := []string{".json", ".yml", ".yaml", ".hcl", ".toml"}
for _, suffix := range suffixes {
if strings.HasSuffix(fname, suffix) {
fname = strings.Split(fname, suffix)[0]
break
}
}
v.SetConfigName(fname)
v.AddConfigPath(dir)
}
if err := addViperRemoteConfig(v); err != nil {
return nil, err
}
if err := v.ReadInConfig(); err != nil {
if _, ok := err.(viper.UnsupportedConfigError); !ok && v.ConfigFileUsed() != "" {
return nil, err
}
}
v.SetEnvPrefix(name)
v.AutomaticEnv()
addViperDefaults(v)
return v, nil
}
func addViperRemoteConfig(v *viper.Viper) error {
// check if encryption is required CONFIG_KEYRING
keyring := os.Getenv("CONFIG_KEYRING")
// check for etcd env vars CONFIG_REMOTE_URL, eg:
// etcd://localhost:2379/[app-name]/config.[type]
// consul://localhost:8500/[app-name]/config.[type]
remURL := os.Getenv("CONFIG_REMOTE_URL")
if remURL == "" {
return nil
}
var provider, host, path, tpe string
u, err := url.Parse(remURL)
if err != nil {
return err
}
provider = strings.ToLower(u.Scheme)
host = u.Host
if provider == "etcd" {
host = "http://" + host
}
path = u.Path
tpe = strings.ToLower(strings.TrimLeft(filepath.Ext(path), "."))
if tpe == "" {
tpe = "json"
}
v.SetConfigType(tpe)
if keyring != "" {
if err := v.AddSecureRemoteProvider(provider, host, path, keyring); err != nil {
return err
}
} else {
if err := v.AddRemoteProvider(provider, host, path); err != nil {
return err
}
}
if err := v.ReadRemoteConfig(); err != nil {
return fmt.Errorf("config is invalid as %s", tpe)
}
return nil
}
func addViperDefaults(v *viper.Viper) {
v.SetDefault("tracer", map[interface{}]interface{}{"enable": true})
v.SetDefault("logging", map[interface{}]interface{}{"root": map[interface{}]interface{}{"level": "info"}})
}
func ensureDefaults(name string) (string, string, error) {
// configure version defaults
version := "dev"
if Version != "" {
version = Version
}
// configure name defaults
if name == "" {
name = os.Getenv("APP_NAME")
if name == "" {
exe, err := execName()
if err != nil {
return "", "", err
}
name = filepath.Base(exe)
}
}
return name, version, nil
}
func newWithCallback(nme string, configPath string, reload func(fsnotify.Event)) (Application, error) {
name, version, err := ensureDefaults(nme)
if err != nil {
return nil, err
}
appInfo := cjm.AppInfo{
Name: name,
BasePath: "/",
Version: version,
Pid: os.Getpid(),
}
cfg, err := createViper(name, configPath)
if err != nil {
return nil, err
}
allLoggers := logging.NewRegistry(cfg, logrus.Fields{"app": appInfo.Name})
log.SetOutput(allLoggers.Writer())
tracer := allLoggers.Root().WithField("module", "trace")
trace := tracing.New("", tracer, nil)
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGQUIT)
buf := make([]byte, 1<<20)
for {
<-sigs
ln := goruntime.Stack(buf, true)
allLoggers.Root().Println(string(buf[:ln]))
}
}()
app := &defaultApplication{
appInfo: appInfo,
allLoggers: allLoggers,
rootTracer: trace,
config: cfg,
registry: make(map[Key]interface{}, 100),
regLock: new(sync.Mutex),
}
app.watchConfigurations(func(in fsnotify.Event) {
if reload != nil {
reload(in)
}
allLoggers.Reload()
for _, mod := range app.modules {
if err := mod.Reload(app); err != nil {
allLoggers.Root().Errorf("reload config: %v", err)
}
}
allLoggers.Root().Infoln("config file changed:", in.Name)
})
return app, nil
}
// New application with the specified name, at the specified basepath
func New(nme string) (Application, error) {
return newWithCallback(nme, "", nil)
}
// NewWithConfig application with the specified name, with a specific config file path
func NewWithConfig(nme string, configPath string) (Application, error) {
return newWithCallback(nme, configPath, nil)
}
type defaultApplication struct {
appInfo cjm.AppInfo
allLoggers *logging.Registry
rootTracer tracing.Tracer
config *viper.Viper
modules []Module
registry map[Key]interface{}
regLock *sync.Mutex
}
func (d *defaultApplication) watchConfigurations(reload func(fsnotify.Event)) {
viperLock.Lock()
defer viperLock.Unlock()
d.config.WatchConfig()
d.config.OnConfigChange(reload)
// we made it this far, it's clear the url means we're also connecting remotely
if os.Getenv("CONFIG_REMOTE_URL") != "" {
go func() {
for {
err := d.config.WatchRemoteConfig()
if err != nil {
d.Logger().Errorf("watching remote config: %v", err)
continue
}
reload(fsnotify.Event{Name: os.Getenv("CONFIG_REMOTE_URL"), Op: fsnotify.Write})
}
}()
}
}
func (d *defaultApplication) Add(modules ...Module) error {
if len(modules) > 0 {
d.modules = append(d.modules, modules...)
}
return nil
}
// Get the module at the specified key, return nil when the component doesn't exist
func (d *defaultApplication) Get(key Key) interface{} {
mod, _ := d.GetOK(key)
return mod
}
// Get the module at the specified key, return false when the component doesn't exist
func (d *defaultApplication) GetOK(key Key) (interface{}, bool) {
d.regLock.Lock()
defer d.regLock.Unlock()
mod, ok := d.registry[key]
if !ok {
return nil, ok
}
return mod, ok
}
func (d *defaultApplication) Set(key Key, module interface{}) error {
d.regLock.Lock()
d.registry[key] = module
d.regLock.Unlock()
return nil
}
func (d *defaultApplication) Logger() logrus.FieldLogger {
return d.allLoggers.Root()
}
func (d *defaultApplication) NewLogger(name string, ctx logrus.Fields) logrus.FieldLogger {
return d.allLoggers.Root().New(name, ctx)
}
func (d *defaultApplication) Tracer() tracing.Tracer {
return d.rootTracer
}
func (d *defaultApplication) Config() *viper.Viper {
return d.config
}
func (d *defaultApplication) Info() cjm.AppInfo {
return d.appInfo
}
func (d *defaultApplication) Init() error {
for _, mod := range d.modules {
if err := mod.Init(d); err != nil {
return err
}
}
return nil
}
func (d *defaultApplication) Start() error {
for _, mod := range d.modules {
if err := mod.Start(d); err != nil {
return err
}
}
return nil
}
func (d *defaultApplication) Stop() error {
for _, mod := range d.modules {
if err := mod.Stop(d); err != nil {
return err
}
}
return nil
}