-
Notifications
You must be signed in to change notification settings - Fork 3
/
application.builder.go
178 lines (149 loc) · 4.6 KB
/
application.builder.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
package app
import (
"context"
"github.com/beego/beego/v2/client/orm"
"github.com/lishimeng/app-starter/amqp"
"github.com/lishimeng/app-starter/amqp/rabbit"
"github.com/lishimeng/app-starter/application/api"
"github.com/lishimeng/app-starter/cache"
"github.com/lishimeng/app-starter/mqtt"
"github.com/lishimeng/app-starter/persistence"
"github.com/lishimeng/app-starter/server"
"github.com/lishimeng/app-starter/token"
"github.com/lishimeng/app-starter/version"
"github.com/lishimeng/go-log"
"github.com/lishimeng/x/etc"
"net/http"
"os"
)
type TokenValidatorInjectFunc func(storage token.Storage)
type TokenValidatorBuilder func(injectFunc TokenValidatorInjectFunc)
type ApplicationBuilder struct {
webEnable bool
webListen string
webComponents []server.Component
webStaticEnable bool
vdir string
assetInfo func(name string) (os.FileInfo, error)
asset func(string) ([]byte, error)
assetNames func() []string
webStaticHome string
assetFile func() http.FileSystem
webLogLevel string
dbEnable bool
dbConfig persistence.BaseConfig
dbModels []interface{}
cacheEnable bool
redisOpts cache.RedisOptions
cacheOpts cache.Options
tokenValidatorEnable bool
tokenValidatorBuilder TokenValidatorBuilder
amqpEnable bool
amqpOptions amqp.Connector
amqpHandler []amqp.Handler
sessionOptions []rabbit.SessionOption
mqttEnable bool
mqttOptions []mqtt.ClientOption
// other components
componentsBeforeWebServer []func(ctx context.Context) (err error)
componentsAfterWebServer []func(ctx context.Context) (err error)
}
var WithDefaultCallback = func(configName string) (f func(loader etc.Loader)) {
return func(loader etc.Loader) {
loader.SetFileSearcher(configName, ".").SetEnvPrefix("").SetEnvSearcher()
}
}
func (h *ApplicationBuilder) LoadConfig(config interface{}, callback func(etc.Loader)) (err error) {
var cb = callback
loader := etc.New()
if cb == nil {
cb = WithDefaultCallback("config")
}
cb(loader)
err = loader.Load(config)
return
}
func (h *ApplicationBuilder) EnableWeb(listen string, components ...server.Component) *ApplicationBuilder {
h.webEnable = true
h.webListen = listen
h.webComponents = append(h.webComponents, api.Router)
if len(components) > 0 {
h.webComponents = append(h.webComponents, components...)
}
// TODO check
return h
}
func (h *ApplicationBuilder) SetMonitorPrefix(prefix string) *ApplicationBuilder {
api.MonitorPrefix = prefix
return h
}
func (h *ApplicationBuilder) HealthyHandler(handler func() int) *ApplicationBuilder {
if handler != nil {
api.LivenessHandler = handler
}
return h
}
func (h *ApplicationBuilder) ReadyHandler(handler func() int) *ApplicationBuilder {
if handler != nil {
api.ReadinessHandler = handler
}
return h
}
func (h *ApplicationBuilder) SetWebLogLevel(lvl string) *ApplicationBuilder {
h.webLogLevel = lvl
return h
}
func (h *ApplicationBuilder) EnableStaticWeb(assetFile func() http.FileSystem) *ApplicationBuilder {
h.webStaticEnable = true
h.assetFile = assetFile
return h
}
func (h *ApplicationBuilder) EnableDatabase(config persistence.BaseConfig,
models ...interface{}) *ApplicationBuilder {
h.dbEnable = true
h.dbConfig = config
h.dbModels = models
// TODO check
return h
}
func (h *ApplicationBuilder) EnableCache(redisOpts cache.RedisOptions, cacheOpts cache.Options) *ApplicationBuilder {
h.cacheEnable = true
h.redisOpts = redisOpts
h.cacheOpts = cacheOpts
return h
}
func (h *ApplicationBuilder) EnableOrmLog() *ApplicationBuilder {
orm.Debug = true
return h
}
func (h *ApplicationBuilder) EnableAmqp(c amqp.Connector, options ...rabbit.SessionOption) *ApplicationBuilder {
h.amqpEnable = true
h.amqpOptions = c
h.sessionOptions = append(h.sessionOptions, options...)
return h
}
// RegisterAmqpHandlers 注册amqp handler
//
// 业务类任务使用延时执行策略,在连接型任务之后执行
func (h *ApplicationBuilder) RegisterAmqpHandlers(handlers ...amqp.Handler) *ApplicationBuilder {
h.amqpHandler = append(h.amqpHandler, handlers...)
return h
}
func (h *ApplicationBuilder) EnableMqtt(options ...mqtt.ClientOption) *ApplicationBuilder {
h.mqttEnable = true
if len(options) > 0 {
h.mqttOptions = append(h.mqttOptions, options...)
}
log.Debug("enable mqtt module")
return h
}
// EnableTokenValidator 验证Token,使用RedisTokenValidator前需要enableCache
func (h *ApplicationBuilder) EnableTokenValidator(builder TokenValidatorBuilder) *ApplicationBuilder {
h.tokenValidatorEnable = true
h.tokenValidatorBuilder = builder
return h
}
func (h *ApplicationBuilder) PrintVersion() *ApplicationBuilder {
version.Print()
return h
}