-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
82 lines (73 loc) · 2.11 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
package main
import (
"context"
"errors"
"flag"
"go-crawler-distributed/global"
"go-crawler-distributed/initConf"
"go-crawler-distributed/internal/routers"
"log"
"net/http"
"time"
)
/**
* @Author: super
* @Date: 2020-08-21 20:37
* @Description:
**/
var (
port string
runMode string
config string
isVersion bool
)
func init() {
err := setupFlag()
if err != nil {
log.Printf("init setupSetting err: %v\n", err)
}
initConf.Init(config)
}
func main() {
router := routers.NewRouter()
s := &http.Server{
Addr: ":" + global.ServerSetting.HttpPort,
Handler: router,
ReadTimeout: global.ServerSetting.ReadTimeout * time.Second,
WriteTimeout: global.ServerSetting.WriteTimeout * time.Second,
MaxHeaderBytes: 1 << 20,
}
go func() {
if err := pingServer(); err != nil {
global.Logger.Errorf(context.Background(), "The server has no response, or it might took too long to start up.")
}
global.Logger.Info(context.Background(), "The server has been deployed successfully.")
}()
global.Logger.Infof(context.Background(), "Start to listening the incoming requests on http address :%s", global.ServerSetting.HttpPort)
err := s.ListenAndServe()
if err != nil {
global.Logger.Fatalf(context.Background(), "start listen server err: %v", err)
}
}
func setupFlag() error {
flag.StringVar(&port, "port", "", "启动端口")
flag.StringVar(&runMode, "mode", "", "启动模式")
flag.StringVar(&config, "config", "configs/", "指定要使用的配置文件路径")
flag.BoolVar(&isVersion, "version", false, "编译信息")
flag.Parse()
return nil
}
// pingServer pings the http server to make sure the router is working.
func pingServer() error {
for i := 0; i < 3; i++ {
time.Sleep(time.Second)
// Ping the server by sending a GET request to `/health`.
resp, err := http.Get(":" + global.ServerSetting.HttpPort + "/sd/health")
if err == nil && resp.StatusCode == 200 {
return nil
}
// Sleep for a second to continue the next ping.
global.Logger.Info(context.Background(), "Waiting for the server, retry in 1 second.")
}
return errors.New("cannot connect to the server")
}