-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
115 lines (95 loc) · 2.68 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
113
114
115
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/vesoft-inc/nebula-http-gateway/common"
_ "github.com/vesoft-inc/nebula-http-gateway/routers"
"github.com/vesoft-inc/nebula-http-gateway/ccore/nebula/gateway/pool"
)
func main() {
/*
session config
*/
beego.BConfig.WebConfig.Session.SessionCookieLifeTime = 0
beego.BConfig.WebConfig.Session.SessionGCMaxLifetime = 60 * 60 * 24
beego.BConfig.WebConfig.Session.SessionName = beego.AppConfig.String("sessionkey")
beego.BConfig.WebConfig.Session.SessionOn = true
/*
logger config
*/
logsPath := beego.AppConfig.DefaultString("logspath", "./logs/")
absLogsPath, _ := filepath.Abs(logsPath)
_, err := common.CreateFileWithPerm(absLogsPath+"/", "0720")
if err != nil && os.IsNotExist(err) {
log.Fatalf("create file %s with error: %s", absLogsPath, err.Error())
}
logFileName := beego.AppConfig.DefaultString("appname", "nebula-http-gateway")
logFileName += ".log"
logFilePath := filepath.Join(
absLogsPath,
logFileName,
)
logLevelString := beego.AppConfig.String("logLevel")
logLevel := logs.LevelWarning
switch logLevelString {
case "error":
logLevel = logs.LevelError
case "warning", "warn":
logLevel = logs.LevelWarning
case "notice":
logLevel = logs.LevelNotice
case "informational", "info":
logLevel = logs.LevelInformational
case "debug":
logLevel = logs.LevelDebug
}
logs.SetLogger("file", fmt.Sprintf(`{"filename":"%s","MaxSize":104857600,"perm":"0620"}`, logFilePath))
logs.GetBeeLogger().DelLogger("console")
logs.SetLogFuncCall(true)
logs.SetLogFuncCallDepth(3)
logs.SetLevel(logLevel)
defer func() {
logs.GetBeeLogger().Flush()
}()
/*
importer file uploads config
*/
uploadsPath := beego.AppConfig.String("uploadspath")
absUploadsPath, _ := filepath.Abs(uploadsPath)
_, err = common.CreateFileWithPerm(absUploadsPath+"/", "0720")
if err != nil && os.IsNotExist(err) {
log.Fatalf("create file %s with error: %s", absLogsPath, err.Error())
}
/*
use channel to wait server quit.
*/
done := make(chan bool, 1)
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
if !signal.Ignored(syscall.SIGHUP) {
signal.Notify(quit, syscall.SIGHUP)
}
go func() {
<-quit
logs.Info("server is shutting down")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
pool.ClearClients()
beego.BeeApp.Server.SetKeepAlivesEnabled(false)
if err := beego.BeeApp.Server.Shutdown(ctx); err != nil {
logs.Error(err.Error())
}
close(done)
}()
beego.Run()
<-done
logs.Info("server closed")
}