-
Notifications
You must be signed in to change notification settings - Fork 1
/
boot.go
89 lines (67 loc) · 1.75 KB
/
boot.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
package app
import (
"errors"
"fmt"
"github.com/astaxie/beego/logs"
"github.com/fvbock/endless"
"github.com/spf13/viper"
"github.com/zqhong/albedo/util"
"log"
"net/http"
"os"
"time"
)
func InitWeb() {
InitConfig()
InitLogger(util.GetRootDir() + "/runtime/log/albedo-web.log")
InitEnv()
InitDb()
InitRedis()
InitMemCache()
InitGin()
}
func InitCli() {
InitConfig()
InitLogger(util.GetRootDir() + "/runtime/log/albedo-cli.log")
InitEnv()
InitDb()
InitRedis()
InitMemCache()
}
func InitEnv() {
loc, err := util.GetLocation()
if err != nil {
log.Println("时区设置失败:" + err.Error())
os.Exit(1)
}
time.Local = loc
logs.Debug(fmt.Sprintf("默认时区(%s)设置成功", loc.String()))
}
func RunWeb() {
logs.Debug(fmt.Sprintf("Start to listening the incoming requests on http address: %s", viper.GetString("addr")))
logs.Info(endless.ListenAndServe(viper.GetString("addr"), Engine).Error())
CheckServer()
}
func CheckServer() {
// Ping the server to make sure the router is working.
go func() {
if err := pingServer(); err != nil {
logs.Error(fmt.Sprintf("The router has no response, or it might took too long to start up, err: %s", err.Error()))
}
logs.Debug("The router has been deployed successfully")
}()
}
// pingServer pings the http server to make sure the router is working.
func pingServer() error {
for i := 0; i < 10; i++ {
// Ping the server by sending a GET request to `/health`.
resp, err := http.Get(viper.GetString("url") + "/sd/health")
if err == nil && resp.StatusCode == 200 {
return nil
}
// Sleep for a second to continue the next ping.
logs.Debug("Waiting for the router, retry in 1 second")
time.Sleep(time.Second)
}
return errors.New("can not connect to the router")
}