-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
216 lines (180 loc) · 3.99 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
210
211
212
213
214
215
216
// A quickly mysql access component.
// Copyright 2024 The 范文瀚 Authors. All rights reserved.
// Package tros golang OS
package tros
import (
"context"
"errors"
trlogger "github.com/woaijssss/tros/logx"
"github.com/woaijssss/tros/server/http"
"github.com/woaijssss/tros/trkit/mysqlx"
"github.com/woaijssss/tros/trkit/redisx"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"os"
"os/signal"
"syscall"
)
// App application
type App struct {
signals []os.Signal
//logger trlogger.Adapter
ctx context.Context //nolint:contained ctx
cancel func()
initializers []Initializer
servers []Server
}
// SettingFunc of app
type SettingFunc func(*App)
type (
// Server interface of server
Server interface {
// Start a server
Start(ctx context.Context) error
// Stop a server
Stop() error
}
// Initializer interface with Init func
Initializer interface {
// Init component
Init(atx AppContext) error
}
)
// AppContext app context
type AppContext interface {
// HTTPRouter http router
HTTPRouter() http.Router
// ServiceRegistrar register grpc service
ServiceRegistrar() grpc.ServiceRegistrar
}
type appContext struct {
router http.Router
registrar grpc.ServiceRegistrar
}
func New(settings ...SettingFunc) *App {
ctx, cancel := context.WithCancel(context.Background())
app := &App{
signals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT},
ctx: ctx,
cancel: cancel,
}
for _, f := range settings {
f(app)
}
err := app.Init()
if err != nil {
app.exit(err)
}
return app
}
func (app *App) Init() error {
var router http.Router
var registrar grpc.ServiceRegistrar
for _, server := range app.servers {
if r, ok := server.(http.Router); ok {
router = r
continue
}
if r, ok := server.(grpc.ServiceRegistrar); ok {
registrar = r
}
}
atx := newAppContext(router, registrar)
// db init
mysqlx.InitMysqlX(app.ctx)
redisx.Setup(app.ctx)
for _, initializer := range app.initializers {
err := initializer.Init(atx)
if err != nil {
return err
}
}
return nil
}
func newAppContext(router http.Router, registrar grpc.ServiceRegistrar) *appContext {
return &appContext{
router: router,
registrar: registrar,
}
}
// HTTPRouter returns http router
func (atx *appContext) HTTPRouter() http.Router {
if atx.router == nil {
panic("http transport not enabled")
}
return atx.router
}
// ServiceRegistrar returns gRpc service registrar
func (atx *appContext) ServiceRegistrar() grpc.ServiceRegistrar {
if atx.registrar == nil {
panic("gRpc transport not enabled")
}
return atx.registrar
}
// Servers register servers to app
func Servers(servers ...Server) SettingFunc {
return func(app *App) {
app.servers = servers
}
}
// WithInitializers register initializers to app
func WithInitializers(initializers ...Initializer) SettingFunc {
return func(app *App) {
app.initializers = append(app.initializers, initializers...)
}
}
func (app *App) exit(err error) {
trlogger.Infof(app.ctx, "service exit", "error", err)
//nolint
os.Exit(1)
}
// Stop application
func (app *App) Stop() error {
if app.cancel != nil {
app.cancel()
}
return nil
}
// Run application
func (app *App) Run() {
trlogger.Infof(app.ctx, "start application")
defer func() {}()
//go debugServerProcess(logger)
//go func() {
// provider.DefaultProvider()
//}()
eg, ctx := errgroup.WithContext(app.ctx)
for _, server := range app.servers {
s := server
eg.Go(func() error {
// wait for stop signal
<-ctx.Done()
return s.Stop()
})
eg.Go(func() error {
err := s.Start(ctx)
if err != nil {
trlogger.Infof(ctx, "failed to start server", "error", err)
return err
}
return nil
})
}
c := make(chan os.Signal, 1)
signal.Notify(c, app.signals...)
eg.Go(func() error {
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-c:
//nolint
_ = app.Stop()
}
}
})
if err := eg.Wait(); err != nil && !errors.Is(err, context.Canceled) {
app.exit(err)
}
trlogger.Infof(ctx, "service exit")
}