-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (51 loc) · 1.24 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
package main
import (
"SvGoBlogApi/config"
"SvGoBlogApi/model"
"SvGoBlogApi/router"
"errors"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
"net/http"
"time"
)
var (
cfg = pflag.StringP("config", "c", "", "config file path")
)
func main() {
pflag.Parse()
if err := config.Init(*cfg); err != nil {
panic(err)
}
model.DB.Init()
defer model.DB.Close()
gin.SetMode(viper.GetString("runmode"))
g := gin.New()
middlewares := []gin.HandlerFunc{}
router.Load(
g,
middlewares...,
)
log.Printf("Start to listening the incoming requests on http address: %s", viper.GetString("port"))
log.Printf(http.ListenAndServe(viper.GetString("port"), g).Error())
go func() {
err := pingServer()
if err != nil {
log.Fatal("The router has no response, or it might took too long to start up.", err)
}
log.Print("The router has been deployed successfully.")
}()
}
func pingServer() error {
for i := 0; i < viper.GetInt("max_ping_count"); i++ {
rsp, err := http.Get(viper.GetString("url") + "/sd/health")
if err == nil && rsp.StatusCode == 200 {
return nil
}
log.Print("Waiting for the router, retry in 1 second.")
time.Sleep(time.Second)
}
return errors.New("Cannot connect to the router.")
}