-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (100 loc) · 3.19 KB
/
main.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
package main
import (
"context"
"fmt"
"io"
"mocker/common"
"mocker/config"
"mocker/util"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
)
func main() {
var configFileName string
if len(os.Args) < 2 {
fmt.Println("Needs config file path")
os.Exit(EXITING_AS_ERROR_IN_ARGS)
}
configFileName = os.Args[1]
configFileReader, err := os.Open(configFileName)
if err != nil {
fmt.Println("Error opening the config file", configFileName, err)
os.Exit(EXITING_AS_ERROR_PROCESSING_CONFIG_FILE)
}
//Initialise Config
appConfig, err := config.New(configFileReader)
if err != nil {
fmt.Println("Error reading config file", err)
os.Exit(EXITING_AS_ERROR_PROCESSING_CONFIG_FILE)
}
defer appConfig.Close()
appContext := appConfig.NewContext("main")
defer appContext.Close()
//Close the config file
if err := configFileReader.Close(); err != nil {
appContext.Log.Errorln("error while closing yml config file")
}
//Last step is to start the server
ginEngine, err := initServer(appContext)
if err != nil {
os.Exit(EXITING_AS_ERROR_WHILE_SERVER_START)
}
if err := InitModules(appContext); err != nil {
appContext.Log.Errorln("error while initialising modules", err)
os.Exit(EXITING_AS_ERROR_INIT_MODULES)
}
startServerWithSignalledShutdown(ginEngine, appContext)
}
func initServer(ac *config.AppContext) (*gin.Engine, error) {
//serverConfig:=ac.Config.ServerConfig
//gin.DefaultWriter = io.MultiWriter()
errorLogFile, _ := ac.GetFile("server.log.error")
accessLogFile, _ := ac.GetFile("server.log.access")
apimode := ac.Config.APIMode
gin.DefaultErrorWriter = io.MultiWriter(errorLogFile)
ac.NewContext("startup.server").Log.Infoln("Running in Mode " + util.APIMODE_GIN_MODE[apimode])
gin.SetMode(util.APIMODE_GIN_MODE[apimode])
ginEngine := gin.Default()
ginEngine.Use(config.AppContextGinMiddleware(ac.AppConfig))
ginEngine.Use(util.AccessLogGinMiddleware(accessLogFile), util.GinBodyLogMiddleware())
//staticPath:=serverConfig.StaticPath
//ginEngine.LoadHTMLGlob(staticPath + "/webroot/**/*[.html|tmpl]")
//ginEngine.Use(util.SentryMiddleware(raven.DefaultClient, false))
RouterPatternsInit(ac, ginEngine)
return ginEngine, nil
}
func startServerWithSignalledShutdown(engine *gin.Engine, ac *config.AppContext) {
serverConfig := ac.Config.ServerConfig
srv := &http.Server{
Addr: serverConfig.Host + ":" + serverConfig.Port,
Handler: engine,
}
ac.Go(func(tac common.AppContexter) {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
panic(err)
}
})
// Wait for interrupt signal to gracefully shutdown the server with
// 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
fmt.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
ac.Log.Fatal("Server Shutdown: ", err)
}
ac.Log.Println("Server exiting")
}
func InitModules(ac *config.AppContext) error {
return nil
}