/
app.go
196 lines (160 loc) · 5.1 KB
/
app.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
package framework
import (
"context"
"fmt"
"git.golaxy.org/core"
"git.golaxy.org/core/util/generic"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
)
// NewApp 创建应用
func NewApp() *App {
return &App{}
}
type _ServPT struct {
generic _IServiceGeneric
num int
}
// App 应用
type App struct {
servicePTs map[string]*_ServPT
startupConf *viper.Viper
initCB, startingCB, terminatedCB generic.DelegateAction1[*App]
}
func (app *App) lazyInit() {
if app.servicePTs == nil {
app.servicePTs = make(map[string]*_ServPT)
}
if app.startupConf == nil {
app.startupConf = viper.New()
}
}
// Setup 安装服务泛化类型
func (app *App) Setup(name string, generic any) *App {
app.lazyInit()
if generic == nil {
panic(fmt.Errorf("%w: generic is nil", core.ErrArgs))
}
_generic, ok := generic.(_IServiceGeneric)
if !ok {
panic(fmt.Errorf("%w: incorrect generic type", core.ErrArgs))
}
app.servicePTs[name] = &_ServPT{
generic: _generic,
num: 1,
}
return app
}
// InitCB 初始化回调
func (app *App) InitCB(cb generic.DelegateAction1[*App]) *App {
app.initCB = cb
return app
}
// StartingCB 启动回调
func (app *App) StartingCB(cb generic.DelegateAction1[*App]) *App {
app.startingCB = cb
return app
}
// TerminateCB 终止回调
func (app *App) TerminateCB(cb generic.DelegateAction1[*App]) *App {
app.terminatedCB = cb
return app
}
// Run 运行
func (app *App) Run() {
app.lazyInit()
// 日志参数
pflag.String("log.format", "console", "logging output format (json|console)")
pflag.String("log.level", "info", "logging level")
pflag.String("log.dir", "./log/", "logging directory path")
pflag.Int("log.size", 100*1024*1024, "log file splitting size")
pflag.Bool("log.stdout", false, "logging output to stdout")
pflag.Bool("log.development", false, "logging in development mode")
pflag.Bool("log.service_info", false, "logging output service info")
pflag.Bool("log.runtime_info", false, "logging output generic info")
// 配置参数
pflag.String("conf.format", "json", "config file format")
pflag.String("conf.startup_path", "", "startup config file path")
pflag.String("conf.local_path", "", "local config file path")
pflag.String("conf.remote_provider", "", "remote config provider")
pflag.String("conf.remote_endpoint", "", "remote config endpoint")
pflag.String("conf.remote_path", "", "remote config file path")
pflag.Bool("conf.auto_update", true, "auto update config")
// nats参数
pflag.String("nats.address", "localhost:4222", "nats address")
pflag.String("nats.username", "", "nats auth username")
pflag.String("nats.password", "", "nats auth password")
// etcd参数
pflag.String("etcd.address", "localhost:2379", "etcd address")
pflag.String("etcd.username", "", "etcd auth username")
pflag.String("etcd.password", "", "etcd auth password")
// 分布式服务参数
pflag.String("service.version", "v0.0.0", "service version info")
pflag.StringToString("service.meta", map[string]string{}, "service meta info")
pflag.Duration("service.ttl", 10*time.Second, "ttl for service keepalive")
pflag.Duration("service.future_timeout", 3*time.Second, "timeout for future model of service interaction")
pflag.Duration("service.dent_ttl", 10*time.Second, "ttl for distributed entity keepalive")
pflag.Bool("service.auto_recover", false, "enable panic auto recover")
// 启动的服务列表
pflag.StringToString("startup.services", func() map[string]string {
ret := map[string]string{}
for name, pt := range app.servicePTs {
ret[name] = strconv.Itoa(pt.num)
}
return ret
}(), "instances required for each service to start")
// 初始化回调
app.initCB.Exec(nil, app)
// 解析启动参数
pflag.Parse()
// 合并启动参数配置
startupConf := app.startupConf
startupConf.AutomaticEnv()
startupConf.BindPFlags(pflag.CommandLine)
startupConf.SetConfigType(startupConf.GetString("conf.format"))
startupConf.SetConfigFile(startupConf.GetString("conf.startup_path"))
if startupConf.ConfigFileUsed() != "" {
if err := startupConf.ReadInConfig(); err != nil {
panic(fmt.Errorf("read startup config file failed, %s", err))
}
}
// 启动回调
app.startingCB.Exec(nil, app)
// 监听退出信号
ctx, cancel := context.WithCancel(context.Background())
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-sigChan
cancel()
}()
// 启动服务
wg := &sync.WaitGroup{}
serviceNum := startupConf.GetStringMapString("startup.services")
for name, pt := range app.servicePTs {
pt.generic.setup(app.startupConf, name, pt.generic)
pt.num, _ = strconv.Atoi(serviceNum[name])
}
for _, pt := range app.servicePTs {
for i := 0; i < pt.num; i++ {
wg.Add(1)
go func(generic _IServiceGeneric, idx int) {
defer wg.Done()
<-generic.generate(ctx, idx).Run()
}(pt.generic, i)
}
}
wg.Wait()
// 结束回调
app.terminatedCB.Exec(nil, app)
}
// GetStartupConf 获取启动参数配置
func (app *App) GetStartupConf() *viper.Viper {
return app.startupConf
}