-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
209 lines (175 loc) · 5.92 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
197
198
199
200
201
202
203
204
205
206
207
208
209
package framework
import (
"context"
"fmt"
"git.golaxy.org/core"
"git.golaxy.org/core/utils/generic"
"github.com/spf13/cobra"
"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()
cmd := &cobra.Command{
Short: "属性同步代码生成工具。",
PreRun: func(cmd *cobra.Command, args []string) {
// 初始化回调
app.initCB.Exec(nil, app)
},
Run: func(cmd *cobra.Command, args []string) {
// 合并启动参数配置
startupConf := app.startupConf
startupConf.AutomaticEnv()
startupConf.BindPFlags(cmd.Flags())
startupConf.SetConfigType(startupConf.GetString("conf.format"))
startupConf.SetConfigFile(startupConf.GetString("conf.local_path"))
if startupConf.ConfigFileUsed() != "" {
if err := startupConf.ReadInConfig(); err != nil {
panic(fmt.Errorf("read 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.init(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, no int) {
defer wg.Done()
<-generic.generate(ctx, no).Run()
}(pt.generic, i)
}
}
wg.Wait()
},
PostRun: func(cmd *cobra.Command, args []string) {
// 结束回调
app.terminatedCB.Exec(nil, app)
},
CompletionOptions: cobra.CompletionOptions{
DisableDefaultCmd: true,
DisableNoDescFlag: true,
DisableDescriptions: true,
},
}
// 日志参数
cmd.PersistentFlags().String("log.format", "console", "logging output format (json|console)")
cmd.PersistentFlags().String("log.level", "info", "logging level")
cmd.PersistentFlags().String("log.dir", "./log/", "logging directory path")
cmd.PersistentFlags().Int("log.size", 100*1024*1024, "log file splitting size")
cmd.PersistentFlags().Bool("log.stdout", false, "logging output to stdout")
cmd.PersistentFlags().Bool("log.development", false, "logging in development mode")
cmd.PersistentFlags().Bool("log.service_info", false, "logging output service info")
cmd.PersistentFlags().Bool("log.runtime_info", false, "logging output generic info")
// 配置参数
cmd.PersistentFlags().String("conf.format", "json", "config file format")
cmd.PersistentFlags().String("conf.local_path", "", "local config file path")
cmd.PersistentFlags().String("conf.remote_provider", "", "remote config provider")
cmd.PersistentFlags().String("conf.remote_endpoint", "", "remote config endpoint")
cmd.PersistentFlags().String("conf.remote_path", "", "remote config file path")
cmd.PersistentFlags().Bool("conf.auto_update", true, "auto update config")
// nats参数
cmd.PersistentFlags().String("nats.address", "localhost:4222", "nats address")
cmd.PersistentFlags().String("nats.username", "", "nats auth username")
cmd.PersistentFlags().String("nats.password", "", "nats auth password")
// etcd参数
cmd.PersistentFlags().String("etcd.address", "localhost:2379", "etcd address")
cmd.PersistentFlags().String("etcd.username", "", "etcd auth username")
cmd.PersistentFlags().String("etcd.password", "", "etcd auth password")
// 分布式服务参数
cmd.PersistentFlags().String("service.version", "v0.0.0", "service version info")
cmd.PersistentFlags().StringToString("service.meta", map[string]string{}, "service meta info")
cmd.PersistentFlags().Duration("service.ttl", 10*time.Second, "ttl for service keepalive")
cmd.PersistentFlags().Duration("service.future_timeout", 3*time.Second, "timeout for future model of service interaction")
cmd.PersistentFlags().Duration("service.dent_ttl", 10*time.Second, "ttl for distributed entity keepalive")
cmd.PersistentFlags().Bool("service.auto_recover", false, "enable panic auto recover")
// 启动的服务列表
cmd.PersistentFlags().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")
// 开始运行
if err := cmd.Execute(); err != nil {
panic(err)
}
}
// GetStartupConf 获取启动参数配置
func (app *App) GetStartupConf() *viper.Viper {
return app.startupConf
}