-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
254 lines (206 loc) · 4.86 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
243
244
245
246
247
248
249
250
251
252
253
254
package kserve
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/gookit/color"
"github.com/gookit/event"
"github.com/gookit/goutil/sysutil"
)
// HTTPServer an HTTP web server
type HTTPServer struct {
srv *http.Server
pidFile string
address []string
realAddr string
processID int
}
// NewHTTPServer create new HTTPServer.
//
// Usage:
//
// srv := NewHTTPServer("127.0.0.1")
// srv := NewHTTPServer("127.0.0.1:8090")
// srv := NewHTTPServer("127.0.0.1", "8090")
func NewHTTPServer(address ...string) *HTTPServer {
return &HTTPServer{
processID: os.Getpid(),
address: address,
realAddr: resolveAddress(address),
}
}
/*************************************************************
* Start HTTP server
*************************************************************/
// Start server, begin handle HTTP request
func (s *HTTPServer) Start() error {
app := nako.App()
s.srv = &http.Server{
Addr: s.realAddr,
}
s.srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
w.WriteHeader(500)
}
}()
evtData := event.M{"w": w, "r": r}
// Fire before route
app.MustFire(OnBeforeRoute, evtData)
// Route and dispatch request
app.Router.ServeHTTP(w, r)
// Fire after route
app.MustFire(OnAfterRoute, evtData)
})
app.MustFire(OnServerStart, event.M{"addr": s.srv.Addr})
// Listen signal
s.handleSignal(s.srv)
// Save pid to file
savePidToFile(s.processID, s.pidFile)
// Start server
err := s.srv.ListenAndServe()
if err != http.ErrServerClosed {
return err
}
return removePidFile(s.pidFile)
}
// Stop by send signal to exist process
func (s *HTTPServer) Stop(timeout int) error {
if s.srv != nil {
return s.Shutdown(timeout)
}
pid := s.processID
if pid <= 0 {
return fmt.Errorf("invalid process ID value")
}
// Server is not started
if !sysutil.ProcessExists(pid) {
return fmt.Errorf("cannot stop, the process is not exists(PID: %d)", pid)
}
err := sysutil.Kill(pid, syscall.SIGTERM)
if err == nil {
return removePidFile(s.pidFile)
}
return err
}
// Shutdown from internal
func (s *HTTPServer) Shutdown(timeout int) error {
if s.srv == nil {
return fmt.Errorf("server is not running")
}
ctx, cancel := context.WithTimeout(
context.TODO(),
time.Duration(timeout)*time.Second,
)
defer cancel()
return s.srv.Shutdown(ctx)
}
/*************************************************************
* Getter/Setter methods
*************************************************************/
// IsRunning get server is running
func (s *HTTPServer) IsRunning() bool {
if s.srv != nil {
return true
}
// Get stat by pid
if s.pidFile == "" {
return false
}
// Get pid from file
bts, err := os.ReadFile(s.pidFile)
if err != nil {
// panic(err)
return false
}
pid, _ := strconv.Atoi(string(bts))
if pid <= 0 {
// panic("invalid process ID value, read from pidFile")
return false
}
// Storage pid value
s.processID = pid
// Server is not started
return sysutil.ProcessExists(pid)
}
// RealAddr get resolved read addr
func (s *HTTPServer) RealAddr() string {
return s.realAddr
}
// ProcessID return
func (s *HTTPServer) ProcessID() int {
return s.processID
}
// PidFile get pid file path
func (s *HTTPServer) PidFile() string {
return s.pidFile
}
// SetPidFile set pid file path
func (s *HTTPServer) SetPidFile(pidFile string) {
s.pidFile = pidFile
}
// handleSignal handles system signal for graceful shutdown.
func (s *HTTPServer) handleSignal(server *http.Server) {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
s := <-c
fmt.Printf("Got signal [%s], exiting server now\n", s)
if err := server.Close(); err != nil {
fmt.Printf("Server close failed: %s", err.Error())
}
nako.App().MustFire(OnServerClose, event.M{"sig": s})
// service.DisconnectDB()
color.Infoln("Server exited")
os.Exit(0)
}()
}
func savePidToFile(pid int, pidFile string) {
if pidFile == "" {
return
}
txt := fmt.Sprintf("%d", pid)
err := ioutil.WriteFile(pidFile, []byte(txt), 0664)
if err != nil {
panic(err)
}
}
func removePidFile(pidFile string) error {
if pidFile == "" {
return nil
}
return os.Remove(pidFile)
}
func resolveAddress(addr []string) (fullAddr string) {
ip := "0.0.0.0"
switch len(addr) {
case 0:
if port := os.Getenv("PORT"); len(port) > 0 {
fmt.Printf("Environment variable PORT=\"%s\"", port)
return ip + ":" + port
}
fmt.Printf("Environment variable PORT is undefined. Using port :8080 by default")
return ip + ":8080"
case 1:
var port string
if strings.IndexByte(addr[0], ':') != -1 {
ss := strings.SplitN(addr[0], ":", 2)
if ss[0] != "" {
return addr[0]
}
port = ss[1]
} else {
port = addr[0]
}
return ip + ":" + port
default:
panic("too much parameters")
}
}