-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (89 loc) · 2.54 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
package main
import (
"io"
"time"
base_common "base_service/common"
"contract_service/common"
contract_common "contract_service/common"
"contract_service/model"
"contract_service/controller"
"contract_service/middleware"
_ "contract_service/docs"
"github.com/gin-contrib/cors"
"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"
)
func migrate(db *gorm.DB) {
db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4 COLLATE=utf8mb4_bin auto_increment=1")
db.AutoMigrate(&model.Contract{})
}
// init 在 main 之前执行
func init() {
// init config
contract_common.DefaultConfig()
contract_common.SetConfig()
contract_common.WatchConfig()
// init logger
base_common.InitLogger()
// init Database
db := base_common.InitMySQL()
// 禁止在表名后面加s
db.SingularTable(true)
migrate(db)
}
// @title YANFEI-CONTRACT API
// @version 0.0.1
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 := base_common.GetLogFile()
defer logFile.Close()
gin.DefaultWriter = io.MultiWriter(logFile)
}
r := gin.Default()
// 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,
}))
} 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,
}))
}
// middleware
r.Use(middleware.ErrorHandling())
r.Use(middleware.MaintenanceHandling())
r.Use(middleware.TokenHandling())
// swagger router
if viper.GetBool("basic.debug") {
r.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
// 路由
r.POST("/contract/new", controller.NewContract)
r.GET("/oss/signture", controller.CreateSignture)
r.Run("0.0.0.0:" + viper.GetString("basic.port"))
}