-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
62 lines (50 loc) · 1.31 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
package main
import (
"crypto/tls"
"errors"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"net/http"
"time"
"vc-gin-api/pkg"
"vc-gin-api/pkg/log"
"vc-gin-api/router"
"vc-gin-api/router/middleware"
)
var (
cfg = pflag.StringP("config", "c", "", "config file path")
)
func main() {
pflag.Parse()
pkg.Init(*cfg)
gin.SetMode(viper.GetString("run_mode"))
g := gin.New()
router.Load(
g,
middleware.Logging(),
)
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("ListenAndServe:%v", http.ListenAndServe(viper.GetString("addr"), g).Error())
}
func pingServer() error {
for i := 0; i < viper.GetInt("max_ping_count"); i++ {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
resp, err := client.Get(viper.GetString("url") + "/api/check/health")
if err == nil && resp.StatusCode == 200 {
return nil
}
log.Info("Waiting for the router, retry in 1 second")
time.Sleep(time.Second)
}
return errors.New("connect to the router error")
}