-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.go
166 lines (134 loc) · 3.04 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
package hitrix
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/coretrix/hitrix/service"
"github.com/fatih/color"
"github.com/99designs/gqlgen/graphql"
)
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)
}
srv := &http.Server{
Addr: ":" + port,
Handler: InitGin(server, ginInitHandler, gqlServerInitHandler),
}
h.preDeploy()
h.forceAlters()
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(err)
}
h.done <- true
}()
h.await()
app := service.DI().App()
defer app.CancelContext()
if err := srv.Shutdown(app.GlobalContext); err != nil {
log.Println("Server forced to shutdown")
}
}
func (h *Hitrix) RunBackgroundProcess(callback func(b *BackgroundProcessor)) {
h.preDeploy()
h.forceAlters()
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.(Script)
defScript.Run(ctx, &exit{s: h})
return
}
}
panic(fmt.Sprintf("unknown script %s", code))
}
func (h *Hitrix) preDeploy() {
app := service.DI().App()
if app.IsInTestMode() {
return
}
preDeployFlag := app.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() {
app := service.DI().App()
if !app.IsInLocalMode() {
return
}
forceAltersFlag := app.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")
}
func (h *Hitrix) await() {
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, syscall.SIGINT, syscall.SIGTERM)
app := service.DI().App()
select {
case code := <-h.exit:
app.CancelContext()
os.Exit(code)
case <-h.done:
app.CancelContext()
case <-termChan:
log.Println("TERMINATING")
app.CancelContext()
time.Sleep(time.Millisecond * 300)
log.Println("TERMINATED")
}
}