This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.go
127 lines (117 loc) · 3.22 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
package main
import (
"fmt"
"github.com/balrogsxt/genshin-auto-sign/app"
"github.com/balrogsxt/genshin-auto-sign/controller"
"github.com/balrogsxt/genshin-auto-sign/controller/middleware"
"github.com/balrogsxt/genshin-auto-sign/helper"
_ "github.com/balrogsxt/genshin-auto-sign/helper"
"github.com/balrogsxt/genshin-auto-sign/helper/log"
"github.com/balrogsxt/genshin-auto-sign/task"
"github.com/gin-gonic/gin"
"github.com/robfig/cron"
"net/http"
)
const Version = "1.0.4"
var _cron *cron.Cron
func main() {
_cron = cron.New()
defer func() {
_cron.Stop()
if err := recover(); err != nil {
log.Info("运行异常: %#v", err)
}
}()
log.Info("CurrentVersion: %s", Version)
conf := initConfig()
//载入配置文件
registerTask()
gin.SetMode(conf.RunMode)
r := gin.New()
registerRouter(r)
uri := fmt.Sprintf("%s:%d", conf.HttpHost, conf.HttpPort)
log.Info("服务运行在: %s", uri)
r.Run(uri)
}
func initConfig() *helper.Config {
conf := helper.GetConfig()
app.GetRDB().Del(app.GetCtx(), "remoteApiPool")
conf.CurlApi = append(conf.CurlApi, "live")
//填装远程api队列到redis
app.GetRDB().LPush(app.GetCtx(), "remoteApiPool", conf.CurlApi)
for _, u := range conf.CurlApi {
log.Info("[远程API] 远程API接口: [ %s ]", u)
}
log.Info("载入远程API完成,可调用的远程API数量: %d ", len(conf.CurlApi))
return conf
}
//注册任务计划
func registerTask() {
conf := helper.GetConfig()
for index, t := range conf.Task {
isFirst := false
if index == 0 {
isFirst = true
}
log.Info("[任务计划] Corn任务计划已启动:[ %s ] -> [ First:%#v ]", t, isFirst)
_cron.AddFunc(t, func() {
task.RunSignTask(isFirst)
})
}
task.RunSignTask(false)
_cron.Start()
}
func registerRouter(r *gin.Engine) {
//注册路由中间件
r.Use(tryCatch())
//注册跨域允许
r.Use(CorsAllow())
//QQ登录
r.GET("/login", controller.Login)
r.GET("/loginVerify", controller.LoginVerify)
r.GET("/getToken", controller.GetToken)
auth := r.Group("/", middleware.AuthMiddleware)
{
auth.GET("/logout", controller.Logout)
auth.GET("/info", controller.GetInfo)
auth.POST("/bind", controller.BindPlayer)
auth.POST("/unbind", controller.UnBindPlayer)
auth.POST("/bindEmail", controller.BindEmail)
}
}
func CorsAllow() gin.HandlerFunc {
return func(c *gin.Context) {
method := c.Request.Method
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token")
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
//放行所有OPTIONS方法
if method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
}
}
}
func tryCatch() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
ex, flag := r.(app.ApiException)
if flag {
c.JSON(ex.Code, gin.H{
"status": ex.Status,
"msg": ex.Msg,
})
} else {
log.Info("%s", r)
c.JSON(500, gin.H{
"status": 1,
"msg": "未知错误",
})
}
}
}()
c.Next()
}
}