-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
58 lines (47 loc) · 1.3 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
package main
import (
"github.com/LucasGao67/MMSM2Wechat/config"
"github.com/LucasGao67/MMSM2Wechat/router"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"net/http"
"time"
)
var (
cfg = pflag.StringP("config", "c", "", "api config file path")
)
func main() {
pflag.Parse()
if err := config.Init(*cfg); err != nil {
panic(err)
}
gin.SetMode(viper.GetString("runmode"))
g := gin.New()
middlewares := []gin.HandlerFunc{}
router.Load(
g,
middlewares...,
)
go func() {
if err := pingServer(); err != nil {
log.Fatal("The router has no response, or it might took too long to start up.", err)
}
log.Info("The router has been deployed successfully.")
}()
log.Infof("Start to listening the incoming requests on http address: %s", viper.GetString("addr"))
log.Infof(http.ListenAndServe(viper.GetString("addr"), g).Error())
}
func pingServer() error {
for i := 0; i < viper.GetInt("max_ping_count"); i++ {
resp, err := http.Get(viper.GetString("url") + viper.GetString("addr") + "/sd/health")
if err == nil && resp.StatusCode == http.StatusOK {
return nil
}
log.Info("Wating for the router,retry in 1 second")
time.Sleep(time.Second)
}
return errors.New("can not connect to the router")
}