This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (62 loc) · 1.65 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
package main
import (
"errors"
"git.zjuqsc.com/miniprogram/wechat-backend/config"
"git.zjuqsc.com/miniprogram/wechat-backend/model"
"git.zjuqsc.com/miniprogram/wechat-backend/router"
"git.zjuqsc.com/miniprogram/wechat-backend/rpc"
"git.zjuqsc.com/miniprogram/wechat-backend/service"
"github.com/gin-gonic/gin"
"github.com/lexkong/log"
"github.com/spf13/viper"
"io"
"net/http"
"os"
"time"
)
func pingServer() error {
for i := 0; i < 10; i++ {
resp, err := http.Get( "http://" + viper.GetString("host") + viper.GetString("addr") + "/sd/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("cannot connect to the router")
}
func healthChecking() {
if err := pingServer(); err != nil {
log.Fatal("The router has no response, or it might took too long to start up.", err)
}
log.Info("Health checking completes successfully.")
}
func main() {
// Init for config and log
if err := config.Init(""); err != nil {
panic(err)
}
// Init for database
model.DB.Init()
defer model.DB.Close()
// Init for gRPC client
rpc.GRPCClient.Init()
defer rpc.GRPCClient.Close()
// Init for Minio client
service.MinioInit()
// Settings for Gin
fgin, _ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(fgin, os.Stdout)
gin.SetMode(viper.GetString("RUNMODE"))
// Create Gin instance
g := gin.Default()
// Load router
router.Load(g)
// Health checking
go healthChecking()
// gRPC checking
go rpc.PingRPCServer()
// Start listening
log.Infof("Server is listening on %s", viper.GetString("addr"))
g.Run(viper.GetString("addr"))
}