-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.go
96 lines (83 loc) · 1.92 KB
/
env.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
package web
import (
"encoding/base64"
"fmt"
"log/syslog"
"github.com/facebookgo/inject"
log "github.com/sirupsen/logrus"
logrus_syslog "github.com/sirupsen/logrus/hooks/syslog"
"github.com/spf13/viper"
"github.com/urfave/cli"
)
const (
// PRODUCTION production mode
PRODUCTION = "production"
)
// MODE run mode
func MODE() string {
return viper.GetString("env")
}
// SECRET secret key 32-bits
func SECRET() ([]byte, error) {
return base64.StdEncoding.DecodeString(viper.GetString("secret"))
}
type injectLogger struct {
}
func (p *injectLogger) Debugf(format string, v ...interface{}) {
log.Debugf(format, v...)
}
// ConfigAction read config at first
func ConfigAction(f cli.ActionFunc) cli.ActionFunc {
return func(c *cli.Context) error {
viper.SetEnvPrefix("arche")
viper.BindEnv("env")
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
return err
}
if MODE() == PRODUCTION {
// ----------
log.SetLevel(log.InfoLevel)
wrt, err := syslog.New(
syslog.LOG_INFO,
fmt.Sprintf(":%d", viper.GetInt("server.port")),
)
if err != nil {
return err
}
log.AddHook(&logrus_syslog.SyslogHook{Writer: wrt})
} else {
log.SetLevel(log.DebugLevel)
}
log.Infof("read config from %s", viper.ConfigFileUsed())
return f(c)
}
}
// InjectAction open beans at first
func InjectAction(f cli.ActionFunc) cli.ActionFunc {
return ConfigAction(func(c *cli.Context) error {
gh := inject.Graph{Logger: &injectLogger{}}
for _, p := range plugins {
if err := p.Init(&gh); err != nil {
return err
}
if err := gh.Provide(&inject.Object{Value: p}); err != nil {
return err
}
}
if err := gh.Populate(); err != nil {
return err
}
for _, p := range plugins {
if err := p.Mount(); err != nil {
return err
}
}
return f(c)
})
}
func init() {
viper.SetDefault("env", "development")
}