-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (95 loc) · 2.45 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
package main
import (
"fmt"
"github.com/Zhenghao-Liu/OAuth_demo/common"
"github.com/Zhenghao-Liu/OAuth_demo/config"
"github.com/Zhenghao-Liu/OAuth_demo/handler"
"github.com/Zhenghao-Liu/OAuth_demo/model"
"github.com/Zhenghao-Liu/OAuth_demo/service"
"github.com/Zhenghao-Liu/OAuth_demo/utils"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
)
var (
ginInstance *gin.Engine
)
func main() {
initUtils()
initConfig()
initModel()
initGin()
initHandler()
initService()
ginInstance.Run(common.HomePage)
}
func initUtils() {
utils.Init()
}
func initConfig() {
if err := config.NewConfigInstance(); err != nil {
panic(err)
}
}
func initModel() {
model.InitDatabase()
model.InitRedis()
}
func initService() {
service.Init()
}
func CORS() gin.HandlerFunc {
return func(context *gin.Context) {
// 允许 Origin 字段中的域发送请求
context.Writer.Header().Add("Access-Control-Allow-Origin", "*")
// 设置预验请求有效期为 86400 秒
context.Writer.Header().Set("Access-Control-Max-Age", "86400")
// 设置允许请求的方法
context.Writer.Header().Set("Access-Control-Allow-Methods", "*")
// 设置允许请求的 Header
context.Writer.Header().Set("Access-Control-Allow-Headers", "*")
// 设置拿到除基本字段外的其他字段,如上面的Apitoken, 这里通过引用Access-Control-Expose-Headers,进行配置,效果是一样的。
context.Writer.Header().Set("Access-Control-Expose-Headers", "*")
// 配置是否可以带认证信息
context.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
// OPTIONS请求返回200
if context.Request.Method == "OPTIONS" {
context.AbortWithStatus(200)
} else {
context.Next()
}
}
}
func initUse() {
logFile, err := os.Create(common.LogFile)
if err != nil {
panic(err)
}
gin.DefaultWriter = io.MultiWriter(logFile, os.Stdout)
ginInstance.Use(gin.LoggerWithFormatter(func(param gin.LogFormatterParams) string {
return fmt.Sprintf("%s - %s \"%s %s %s %d %s \"%s\" %s\"\n",
param.TimeStamp.Format("2006-01-02 03:04:05"),
param.ClientIP,
param.Method,
param.Path,
param.Request.Proto,
param.StatusCode,
param.Latency,
param.Request.UserAgent(),
param.ErrorMessage,
)
}))
ginInstance.Use(CORS())
}
func initGin() {
ginInstance = gin.Default()
initUse()
ginInstance.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
}
func initHandler() {
handler.Init()
handler.RegisterHandlers(ginInstance)
}