-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.go
242 lines (221 loc) · 6.14 KB
/
server.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package server
import (
"context"
"fmt"
"log/slog"
"os"
"time"
"github.com/common-nighthawk/go-figure"
"github.com/gin-gonic/gin"
"github.com/mss-boot-io/mss-boot/core/server"
"github.com/mss-boot-io/mss-boot/core/server/listener"
"github.com/mss-boot-io/mss-boot/core/server/task"
"github.com/mss-boot-io/mss-boot/pkg"
"github.com/mss-boot-io/mss-boot/pkg/config/gormdb"
"github.com/mss-boot-io/mss-boot/pkg/config/source"
"github.com/mss-boot-io/mss-boot/pkg/enum"
"github.com/mss-boot-io/mss-boot/virtual/action"
"github.com/robfig/cron/v3"
"github.com/spf13/cobra"
"github.com/mss-boot-io/mss-boot-admin/center"
"github.com/mss-boot-io/mss-boot-admin/config"
"github.com/mss-boot-io/mss-boot-admin/middleware"
"github.com/mss-boot-io/mss-boot-admin/models"
"github.com/mss-boot-io/mss-boot-admin/router"
)
/*
* @Author: lwnmengjing<lwnmengjing@qq.com>
* @Date: 2023/8/10 00:33:48
* @Last Modified by: lwnmengjing<lwnmengjing@qq.com>
* @Last Modified time: 2023/8/10 00:33:48
*/
var (
apiCheck bool
group string
driver string
dsn string
configProvider string
StartCmd = &cobra.Command{
Use: "server",
Short: "start server",
Long: "start mss-boot-admin server",
Example: "mss-boot-admin server",
PreRunE: func(cmd *cobra.Command, args []string) error {
return setup()
},
RunE: func(cmd *cobra.Command, args []string) error {
return run()
},
}
)
func init() {
StartCmd.PersistentFlags().BoolVarP(&apiCheck,
"api", "a",
false,
"Start server with check api data")
StartCmd.PersistentFlags().StringVarP(&configProvider,
"config-provider", "c",
os.Getenv("CONFIG_PROVIDER"),
"Start server with config provider")
StartCmd.PersistentFlags().StringVarP(&group,
"group", "g",
"/admin",
"Start server with group path")
StartCmd.PersistentFlags().StringVarP(&driver,
"gorm-driver", "r",
"mysql", "Start server with db driver")
StartCmd.PersistentFlags().StringVarP(&dsn,
"gorm-dsn", "n",
"root:123456@tcp(127.0.0.1:3306)/mss-boot-admin-local?charset=utf8&parseTime=True&loc=Local",
"Start server with db dsn")
center.SetTenant(&models.Tenant{}).
SetVerify(&models.User{})
}
func setup() error {
// setup 00 set params
// env overwrite args
if os.Getenv("DB_DRIVER") != "" {
driver = os.Getenv("DB_DRIVER")
} else {
_ = os.Setenv("DB_DRIVER", driver)
}
if os.Getenv("DB_DSN") != "" {
dsn = os.Getenv("DB_DSN")
} else {
_ = os.Setenv("DB_DSN", dsn)
}
// setup 01 config init
opts := []source.Option{
// use local config file
source.WithDir("config"),
source.WithProvider(source.Local),
source.WithWatch(true),
}
switch source.Provider(configProvider) {
case source.GORM:
opts = []source.Option{
source.WithProvider(source.GORM),
source.WithGORMDriver(driver),
source.WithGORMDsn(dsn),
source.WithDriver(&models.SystemConfig{}),
}
case source.FS:
opts = []source.Option{
source.WithProvider(source.FS),
source.WithFrom(config.FS),
}
case source.ConfigMap:
opts = []source.Option{
source.WithProvider(source.ConfigMap),
source.WithConfigmap("mss-boot-admin"),
source.WithNamespace(pkg.GetStage()),
}
case source.Consul:
opts = []source.Option{
source.WithProvider(source.Consul),
source.WithDir("mss-boot-admin/config"),
source.WithWatch(true),
}
case source.Local, "":
default:
slog.Error("config provider not support", "provider", configProvider)
os.Exit(-1)
}
center.SetConfig(config.Cfg).Init(opts...)
err := models.InitTenant(gormdb.DB)
if err != nil {
return err
}
// app config
center.SetAppConfig(&models.AppConfig{})
// user config
center.SetUserConfig(&models.UserConfig{})
// statistics config
center.SetStatistics(&models.Statistics{})
center.SetGRPCClient(&config.Cfg.GRPC)
// setup 02 middleware init
middleware.Verifier = center.GetUser()
middleware.Init()
// setup 03 router init
r := gin.Default()
center.SetMakeRouter(router.DefaultMakeRouter)
center.SetRouter(r)
center.Default.MakeRouter(r.Group(group))
config.Cfg.Application.Init(center.GetRouter())
// setup 04 api check
if apiCheck {
err := models.SaveAPI(r.Routes())
if err != nil {
slog.Error("save api error", "err", err)
}
os.Exit(0)
}
// setup 05 server init
runnable := []server.Runnable{
config.Cfg.Server.Init(
listener.WithStartedHook(tips),
listener.WithName("admin"),
listener.WithHandler(r)),
}
// setup 06 task init
if config.Cfg.Task.Enable {
runnable = append(runnable,
task.New(task.WithStorage(&models.TaskStorage{DB: gormdb.DB}), task.WithSchedule("task", config.Cfg.Task.Spec, &taskE{})))
}
// setup 07 init virtual models
//todo every tenant has different models
ms, err := center.SetVirtualModel(&models.Model{}).GetModels(nil)
if err != nil {
return err
}
for i := range ms {
action.SetModel(ms[i].GetKey(), ms[i].Make())
}
// setup 08 add runnable to manager
center.Default.Add(runnable...)
return nil
}
func run() error {
ctx := context.Background()
return center.Default.Start(ctx)
}
func tips() {
figure.NewFigure(config.Cfg.Application.Name, "rectangles", true).Print()
fmt.Println()
}
type taskE struct {
}
func (t *taskE) Run() {
tasks := make([]*models.Task, 0)
err := gormdb.DB.Where("checked_at < ? or checked_at is null", time.Now().Add(-1*time.Minute)).
Where("status = ?", enum.Enabled).Find(&tasks).Error
if err != nil {
slog.Error("task run get tasks error", slog.Any("err", err))
return
}
for i := range tasks {
slog.Info("task", "id", tasks[i].ID, "checked_at", tasks[i].CheckedAt)
err = task.UpdateJob(tasks[i].ID, tasks[i].Spec, tasks[i])
if err != nil {
slog.Error("task run update job error", slog.Any("err", err))
continue
}
}
//check
err = gormdb.DB.Where("status = ?", enum.Enabled).Find(&tasks).Error
if err != nil {
slog.Error("task run get tasks error", slog.Any("err", err))
return
}
for i := range tasks {
if entry := task.Entry(cron.EntryID(tasks[i].EntryID)); entry.ID > 0 {
err = gormdb.DB.Model(&models.Task{}).
Where("id = ?", tasks[i].ID).
Update("checked_at", time.Now()).Error
if err != nil {
slog.Error("task run update task error", slog.Any("err", err))
continue
}
}
}
}