This repository has been archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
145 lines (134 loc) · 3.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
"github.com/spf13/viper"
"github.com/zjutjh/info-backend/controller"
"github.com/zjutjh/info-backend/handler"
"github.com/zjutjh/info-backend/model"
"log"
"net/http"
"os"
"os/signal"
"time"
)
const (
VERSION = "v1.2.1"
)
func main() {
// set release mode
gin.SetMode(gin.ReleaseMode)
// parse cmd args
var options model.Options
if _, err := flags.Parse(&options); err != nil {
return
}
// show VERSION
if options.Version {
fmt.Println("Info Server version " + VERSION)
return
}
// read config from file(yaml)
if options.ConfigPath != "" {
viper.SetConfigFile(options.ConfigPath)
} else {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("/etc/info")
}
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
fmt.Println("Config file 'config.yml' not found")
os.Exit(2)
} else {
fmt.Println(err)
os.Exit(5)
}
}
// load data from excel file
if options.LoadData != "" {
fmt.Println("Start read & load...")
handler.ReadInfo(options.LoadData, options.Passwd, options.Sheet, options.Update)
fmt.Println("Load finished")
return
}
// initial database connection
handler.InitDB()
// modify routers
router := gin.New()
// set trusted reverse proxies
if viper.GetBool("proxy") {
trustedProxies := viper.GetStringSlice("trusted-proxies")
err := router.SetTrustedProxies(trustedProxies)
if err != nil {
fmt.Println(err.Error())
return
}
}
router.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
// log format
return fmt.Sprintf("%s |%s %d %s| %s |%s %s %s %s | %s | size: %d | %s | %s\n",
param.TimeStamp.Format(time.RFC1123),
param.StatusCodeColor(),
param.StatusCode,
param.ResetColor(),
param.ClientIP,
param.MethodColor(),
param.Method,
param.ResetColor(),
param.Path,
param.Latency,
param.BodySize,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
router.Use(gin.Recovery())
v1 := router.Group("/api/v1")
{
v1.POST("/info", controller.GetInfo)
v1.POST("/dorm", controller.GetDorm)
v1.OPTIONS("/info", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
v1.OPTIONS("/dorm", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
}
// start server
var srv *http.Server
if port := viper.GetString("server-port"); port != "" {
log.Println("Info Server " + VERSION + " starting at " + port)
srv = &http.Server{
Addr: port,
Handler: router,
}
} else {
log.Println("Info Server " + VERSION + " starting at :8080")
srv = &http.Server{
Addr: ":8080",
Handler: router,
}
}
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}()
// Wait for interrupt signal to gracefully shut down the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
log.Println("Server exiting")
}