-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
113 lines (96 loc) · 2.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"Go-RestfulAPI/config"
"Go-RestfulAPI/pkg/db"
"Go-RestfulAPI/pkg/logging"
v "Go-RestfulAPI/pkg/version"
"Go-RestfulAPI/router"
"Go-RestfulAPI/router/middleware"
"encoding/json"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/zap"
"log"
"net/http"
"os"
"time"
)
/**
* @Author: super
* @Date: 2020-08-26 14:57
* @Description:
**/
var (
cfg = pflag.StringP("config", "c", "", "config file path")
version = pflag.BoolP("version", "v", false, "show version info.")
port string
)
func main() {
pflag.Parse()
if *version {
v := v.Get()
marshalled, err := json.MarshalIndent(&v, "", " ")
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
fmt.Println(string(marshalled))
return
}
//init config
if err := config.Init(*cfg); err != nil {
log.Println(err)
panic(err)
}
//init log
logging.InitLog()
//init db
db.InitDB()
port = viper.GetString("addr")
gin.SetMode(viper.GetString("runmode"))
// Create the Gin engine.
g := gin.New()
// Routes.
router.Load(
// Cores.
g,
// Middlwares.
middleware.Logging(),
middleware.RequestId(),
)
// Ping the server to make sure the router is working.
go func() {
if err := pingServer(); err != nil {
logging.GetLogger().Error("The router has no response, or it might took too long to start up.", zap.Error(err))
}
logging.GetLogger().Info("The router has been deployed successfully.")
}()
// Start to listening the incoming requests.
cert := viper.GetString("tls.cert")
key := viper.GetString("tls.key")
if cert != "" && key != "" {
go func() {
logging.GetLogger().Info("Start to listening the incoming requests on https address: " + viper.GetString("tls.addr"))
logging.GetLogger().Info(http.ListenAndServeTLS(viper.GetString("tls.addr"), cert, key, g).Error())
}()
}
logging.GetLogger().Info("Start to listening the incoming requests on http address" + port)
log.Printf(http.ListenAndServe(port, g).Error())
}
// pingServer pings the http server to make sure the router is working.
func pingServer() error {
for i := 0; i < 2; 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.
logging.GetLogger().Info("Waiting for the router, retry in 1 second.")
time.Sleep(time.Second)
}
return errors.New("cannot connect to the router")
}