-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (61 loc) · 1.77 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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/init-new-world/Go_API_learning/model"
"github.com/init-new-world/Go_API_learning/router/middleware"
"github.com/spf13/viper"
"github.com/init-new-world/Go_API_learning/config"
ver "github.com/init-new-world/Go_API_learning/pkg/version"
"github.com/spf13/pflag"
"github.com/init-new-world/Go_API_learning/handler/sd"
"github.com/gin-gonic/gin"
"github.com/init-new-world/Go_API_learning/router"
"github.com/lexkong/log"
)
var (
cfg = pflag.StringP("config", "c", "", "Go_API_learning Server config file path.")
version = pflag.BoolP("version", "v", false, "Show version info.")
)
func main() {
pflag.Parse()
if *version {
v := ver.Get()
marshalled, err := json.MarshalIndent(&v, "", " ")
if err != nil {
fmt.Printf("%v\n", err)
os.Exit(1)
}
fmt.Println(string(marshalled))
return
}
gin.SetMode(viper.GetString("runmode"))
server := gin.Default()
if err := config.Init(*cfg); err != nil {
panic(err)
}
middlewares := []gin.HandlerFunc{
middleware.RequestId(),
middleware.Logging(),
}
model.DB.Init()
defer model.DB.Close()
server = router.Load(server, middlewares...)
go func() {
if err := sd.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.")
}()
cert := viper.GetString("tls.cert")
key := viper.GetString("tls.key")
if cert != "" && key != "" {
go func() {
log.Infof("Start TLS-Server at Port %s", viper.GetString("tls.port"))
log.Info(server.RunTLS(":"+viper.GetString("tls.port"), cert, key).Error())
}()
}
log.Infof("Start Server at Port %s", viper.GetString("port"))
log.Info(server.Run(":" + viper.GetString("port")).Error())
}