-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbeemod.go
145 lines (135 loc) · 2.73 KB
/
beemod.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
package beemod
import (
"bytes"
"errors"
"fmt"
"github.com/astaxie/beego/logs"
"github.com/beego/beemod/pkg/datasource"
"github.com/beego/beemod/pkg/module"
"github.com/spf13/viper"
"gopkg.in/ini.v1"
"os"
)
type BeeMod struct {
invokers []module.Invoker
cfgType string
isSetConfig bool
err error
ds datasource.Datasource
}
var (
BEEMOD_DEBUG = false
)
// init debug
func init() {
if os.Getenv("BEEMOD_DEBUG") == "true" {
BEEMOD_DEBUG = true
}
}
// Register Interface
func Register(invokerFuncs ...module.InvokerFunc) (obj *BeeMod) {
obj = &BeeMod{}
invokers, err := sortInvokers(invokerFuncs)
if err != nil {
obj.err = err
return
}
obj.invokers = invokers
return
}
// Set Config
func (m *BeeMod) SetCfg(cfg interface{}, cfgType string) *BeeMod {
if m.err != nil {
return m
}
module.Config = &module.ConfigStore{}
switch cfgInfo := cfg.(type) {
case string:
if cfgType == "ini" {
f, err := ini.Load(cfgInfo)
if err != nil {
m.err = err
return m
}
m.ds = &datasource.Ini{
Ini: f,
}
} else {
viper.SetConfigFile(cfgInfo)
if cfgType != "" {
viper.SetConfigType(cfgType)
}
viper.AutomaticEnv() // read in environment variables that match
err := viper.ReadInConfig()
if err != nil {
logs.Critical("Using config file:", viper.ConfigFileUsed())
m.err = err
return m
}
m.ds = &datasource.Toml{
Viper: viper.GetViper(),
}
}
case []byte:
if cfgType == "ini" {
f, err := ini.Load(cfgInfo)
if err != nil {
m.err = err
return m
}
m.ds = &datasource.Ini{
Ini: f,
}
} else {
rBytes := bytes.NewReader(cfgInfo)
if cfgType != "" {
viper.SetConfigType(cfgType)
}
viper.AutomaticEnv() // read in environment variables that match
err := viper.ReadConfig(rBytes)
if err != nil {
logs.Critical("Using config file:", viper.ConfigFileUsed())
m.err = err
return m
}
m.ds = &datasource.Toml{
Viper: viper.GetViper(),
}
}
default:
m.err = fmt.Errorf("type is error %s", cfg)
return m
}
m.cfgType = cfgType
m.isSetConfig = true
if BEEMOD_DEBUG {
viper.Debug()
}
return m
}
func (m *BeeMod) Run() (err error) {
if !m.isSetConfig {
err = errors.New("bee mod need set config")
return
}
if m.err != nil {
err = m.err
return
}
for _, invoker := range m.invokers {
name := getCallerName(invoker)
logs.Info("module", name, "cfg start")
if err = invoker.InitCfg(m.ds); err != nil {
logs.Info("module", name, "init config error")
return
}
logs.Info("module", name, "cfg end")
logs.Info("module", name, "run start")
if err = invoker.Run(); err != nil {
logs.Info("module", name, "run error")
return
}
logs.Info("module", name, "run ok")
}
return
}