This repository was archived by the owner on Jun 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
141 lines (120 loc) · 3.51 KB
/
main.go
File metadata and controls
141 lines (120 loc) · 3.51 KB
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
package main
import (
"io"
"log"
"net/http"
"time"
"a2os/behavior/common"
"a2os/behavior/controller/event"
"a2os/behavior/controller/misc"
_ "a2os/behavior/docs"
"a2os/behavior/model"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"github.com/spf13/viper"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/gin-swagger/swaggerFiles"
csrf "github.com/utrack/gin-csrf"
)
// @title A2OS Behavior
// @version 1.0.0-alpha
// @description A2OS Behavior API Documentation.
// @contact.name A2OS Dev Team
// @contact.url https://groups.google.com/group/a2os-general
// @contact.email a2os-general@googlegroups.com
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host api.behavior.a2os.club
func migrate(db *gorm.DB) {
db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_bin auto_increment=1").AutoMigrate(&model.Event{})
}
func init() {
// init config
common.DefaultConfig()
common.SetConfig()
common.WatchConfig()
// init sentry error tracking service
common.InitSentry()
// init logger
common.InitLogger()
// init Database
db := common.InitDB()
migrate(db)
}
func main() {
// Before init router
if viper.GetBool("basic.debug") {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
// Redirect log to file
gin.DisableConsoleColor()
logFile := common.GetLogFile()
defer logFile.Close()
gin.DefaultWriter = io.MultiWriter(logFile)
common.GetDB().SetLogger(log.New(logFile, "\r\n", 0))
}
r := gin.Default()
// 错误处理
r.Use(common.ErrorHandling())
r.Use(common.MaintenanceHandling())
// After init router
// CORS
if viper.GetBool("basic.debug") {
r.Use(cors.New(cors.Config{
// The value of the 'Access-Control-Allow-Origin' header in the
// response must not be the wildcard '*' when the request's
// credentials mode is 'include'.
AllowOrigins: common.CORS_ALLOW_DEBUG_ORIGINS,
AllowMethods: common.CORS_ALLOW_METHODS,
AllowHeaders: common.CORS_ALLOW_HEADERS,
ExposeHeaders: common.CORS_EXPOSE_HEADERS,
AllowCredentials: true,
AllowWildcard: true,
MaxAge: 12 * time.Hour,
}))
//r.Use(CORS())
} else {
// RELEASE Mode
r.Use(cors.New(cors.Config{
AllowOrigins: common.CORS_ALLOW_ORIGINS,
AllowMethods: common.CORS_ALLOW_METHODS,
AllowHeaders: common.CORS_ALLOW_HEADERS,
ExposeHeaders: common.CORS_EXPOSE_HEADERS,
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
}
// CSRF
store := cookie.NewStore([]byte(viper.GetString("csrf.cookie_secret")))
r.Use(sessions.Sessions(viper.GetString("csrf.session_name"), store))
CSRF := csrf.Middleware(csrf.Options{
Secret: viper.GetString("csrf.secret"),
ErrorFunc: func(c *gin.Context) {
//c.String(http.StatusBadRequest, "CSRF token mismatch")
c.JSON(http.StatusBadRequest, gin.H{
"err_code": 10007,
"message": common.Errors[10007],
})
log.Println(c.ClientIP(), "CSRF token mismatch")
c.Abort()
},
})
// ONLY FOR DEBUGGING
// swagger router
if viper.GetBool("basic.debug") {
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
// misc operations
r.GET("/ping", misc.Ping)
r.GET("/csrf", CSRF, misc.Csrf)
// the API with CSRF middleware
v1Csrf := r.Group("/v1", CSRF)
{
v1Csrf.POST("/event", event.Create)
}
r.Run(":" + viper.GetString("basic.port")) // listen and serve on 0.0.0.0:PORT
}