-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
87 lines (77 loc) · 2.1 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
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"healthcare-panel/common"
"healthcare-panel/middleware"
model "healthcare-panel/models"
"healthcare-panel/routers"
"healthcare-panel/utils/logging"
redisUtil "healthcare-panel/utils/redis"
"healthcare-panel/utils/setting"
"net"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
var logger = logging.Setup("main-logger", nil)
func init() {
setting.Setup()
model.Setup()
common.InitValidate()
if setting.RedisSetting.Host != "" {
redisUtil.Setup()
}
}
// @title Healthcare panel
// @version 1.0
// @securityDefinitions.apikey ApiKeyAuth
// @in header like: Bearer xxxx
// @name Authorization
func main() {
g := gin.New()
g.Use(gin.Logger(), gin.Recovery())
if setting.AppSetting.EnabledCORS {
g.Use(middleware.CORS())
}
routersInit := routers.InitRouter(g)
readTimeout := setting.ServerSetting.ReadTimeout
writeTimeout := setting.ServerSetting.WriteTimeout
endPoint := fmt.Sprintf(":%d", setting.ServerSetting.HttpPort)
maxHeaderBytes := 1 << 20
server := &http.Server{
Addr: endPoint,
Handler: routersInit,
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
MaxHeaderBytes: maxHeaderBytes,
}
go func() {
// service connection
err := server.ListenAndServe()
if err != nil {
logger.Fatalln(err)
}
}()
conn, _ := net.Dial("ip:icmp", "localhost")
fmt.Println(conn.LocalAddr())
logger.Printf("[info] start http server listening %s", endPoint)
logger.Printf("[info] Actual pid is %d", os.Getpid())
// Wait for an interrupt signal to gracefully shut down the server (set a timeout of 5 seconds)
quit := make(chan os.Signal, 1)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger.Println("Shutdown Server...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
logger.Fatal("Server Shutdown: ", err)
}
logger.Println("Server exiting")
}