-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.go
201 lines (157 loc) · 3.72 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
package hitrix
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/fatih/color"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/app"
)
type Hitrix struct {
done chan bool
exit chan int
}
func (h *Hitrix) RunServer(
defaultPort uint,
server graphql.ExecutableSchema,
ginInitHandler GinInitHandler,
gqlServerInitHandler GQLServerInitHandler,
) {
port := os.Getenv("PORT")
if port == "" {
port = fmt.Sprintf("%d", defaultPort)
}
//nolint //G112: Potential Slowloris Attack because ReadHeaderTimeout is not configured in the http.Server
srv := &http.Server{
Addr: ":" + port,
Handler: InitGin(server, ginInitHandler, gqlServerInitHandler),
}
h.preDeploy()
h.forceAlters()
h.startup()
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(err)
}
h.done <- true
}()
h.await()
appService := service.DI().App()
defer appService.CancelContext()
if err := srv.Shutdown(appService.GlobalContext); err != nil {
log.Println("Server forced to shutdown")
}
}
func (h *Hitrix) RunBackgroundProcess(callback func(b *BackgroundProcessor)) {
h.preDeploy()
h.forceAlters()
h.startup()
callback(&BackgroundProcessor{Server: h})
h.await()
}
func (h *Hitrix) runDynamicScrips(ctx context.Context, code string) {
scripts := service.DI().App().Scripts
if len(scripts) == 0 {
panic(fmt.Sprintf("unknown script %s", code))
}
for _, defCode := range scripts {
if defCode == code {
def, has := service.GetServiceOptional(defCode)
if !has {
panic(fmt.Sprintf("unknown script %s", code))
}
defScript := def.(app.IScript)
defScript.Run(ctx, &exit{s: h})
return
}
}
panic(fmt.Sprintf("unknown script %s", code))
}
func (h *Hitrix) startup() {
if service.HasService(service.FeatureFlagService) {
ormService := service.DI().OrmEngine()
clockService := service.DI().Clock()
featureFlagService := service.DI().FeatureFlag()
featureFlagService.Sync(ormService, clockService)
}
}
func (h *Hitrix) preDeploy() {
appService := service.DI().App()
if appService.IsInTestMode() {
return
}
preDeployFlag := appService.Flags.Bool("pre-deploy")
if !preDeployFlag {
return
}
ormService := service.DI().OrmEngine()
alters := ormService.GetAlters()
hasAlters := false
for _, alter := range alters {
if alter.Safe {
color.Green("%s\n\n", alter.SQL)
} else {
color.Red("%s\n\n", alter.SQL)
}
hasAlters = true
}
if hasAlters {
os.Exit(1)
}
os.Exit(0)
}
func (h *Hitrix) forceAlters() {
appService := service.DI().App()
if !appService.IsInLocalMode() && !appService.IsInQAMode() {
return
}
forceAltersFlag := appService.Flags.Bool("force-alters")
if !forceAltersFlag {
return
}
ormService := service.DI().OrmEngine()
dbService := ormService.GetMysql()
alters := ormService.GetAlters()
var queries string
for _, alter := range alters {
queries += alter.SQL
}
if queries != "" {
_, def := dbService.Query(queries)
defer def()
}
altersSearch := ormService.GetRedisSearchIndexAlters()
for _, alter := range altersSearch {
alter.Execute()
}
log.Println("FORCE ALTERS executed")
if appService.IsInQAMode() {
os.Exit(0)
}
}
func (h *Hitrix) await() {
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
appService := service.DI().App()
select {
case code := <-h.exit:
appService.CancelContext()
appService.Wait()
os.Exit(code)
case <-h.done:
appService.CancelContext()
appService.Wait()
case <-termChan:
log.Println("TERMINATING")
appService.CancelContext()
appService.Wait()
time.Sleep(time.Millisecond * 300)
log.Println("TERMINATED")
}
}