-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRouters.go
146 lines (126 loc) · 3.34 KB
/
Routers.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
146
/*
* @Author: NyanCatda
* @Date: 2022-06-20 13:12:12
* @LastEditTime: 2022-09-21 12:16:52
* @LastEditors: NyanCatda
* @Description: 路由注册
* @FilePath: \MCBE-Server-Motd\Routers.go
*/
package main
import (
"io/ioutil"
"net/http"
"path"
"time"
"github.com/BlackBEDevelopment/MCBE-Server-Motd/MotdBEAPI"
"github.com/BlackBEDevelopment/MCBE-Server-Motd/StatusImg"
cache "github.com/chenyahui/gin-cache"
"github.com/chenyahui/gin-cache/persist"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/nyancatda/AyaLog"
)
/**
* @description: 路由注册
* @param {*gin.Engine} r gin引擎
* @return {*gin.Engine} gin引擎
*/
func SetupRouter(r *gin.Engine) *gin.Engine {
// 500错误处理
r.Use(ServerError)
// 注册静态资源
r.Static("/static", "fronend/dist/static")
r.StaticFile("/favicon.ico", "fronend/dist/static/favicon.ico")
// 注册前端页面
FrontendRouter(r)
// 注册跨域请求头
CorsConfig := cors.DefaultConfig()
CorsConfig.AllowOrigins = []string{"*"}
CorsConfig.AllowHeaders = []string{"*"}
r.Use(cors.New(CorsConfig))
// 基岩版查询API
r.GET("/api", func(c *gin.Context) {
Host := c.Query("host")
data, err := MotdBEAPI.MotdBE(Host)
if err != nil {
AyaLog.DeBug("Request", err)
}
c.JSON(http.StatusOK, data)
})
// Java版查询API
// 不要问为什么MotdBE可以请求Java
r.GET("/api/java", func(c *gin.Context) {
Host := c.Query("host")
data, err := MotdBEAPI.MotdJava(Host)
if err != nil {
AyaLog.DeBug("Request", err)
}
c.JSON(http.StatusOK, data)
})
//初始化缓存
memoryStore := persist.NewMemoryStore(1 * time.Minute)
// 基岩版服务器状态图片
r.GET("/status_img", cache.CacheByRequestURI(memoryStore, 10*time.Second), func(c *gin.Context) {
Host := c.Query("host")
Img, err := StatusImg.ServerStatusImg(Host)
if err != nil {
AyaLog.Error("Request", err)
c.String(http.StatusInternalServerError, "Server Error")
return
}
c.String(http.StatusOK, Img.String())
})
// Java版服务器状态图片
r.GET("/status_img/java", cache.CacheByRequestURI(memoryStore, 10*time.Second), func(c *gin.Context) {
Host := c.Query("host")
Img, err := StatusImg.ServerStatusImgJava(Host)
if err != nil {
AyaLog.Error("Request", err)
c.String(http.StatusInternalServerError, "Server Error")
return
}
c.String(http.StatusOK, Img.String())
})
return r
}
/**
* @description: 500错误处理
* @param {*gin.Context} c
* @return {*}
*/
func ServerError(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
// 打印错误堆栈信息
AyaLog.Error("Request", r.(error))
// 500返回
c.String(http.StatusInternalServerError, "Server Error")
c.Abort()
return
}
}()
c.Next()
}
/**
* @description: 前端文件路由注册
* @param {*gin.Engine} r 路由引擎
* @return {*gin.Engine} 路由引擎
*/
func FrontendRouter(r *gin.Engine) *gin.Engine {
// 遍历目录下的所有HTML文件
HTMLFiles, err := ioutil.ReadDir("fronend/dist/static")
if err != nil {
AyaLog.Error("System", err)
}
// 注册index.html
r.StaticFile("/", "fronend/dist/static/index.html")
// 循环注册其他HTML文件
for _, File := range HTMLFiles {
// 判断是否为HTML文件
FileName := File.Name()
if path.Ext(FileName) == ".html" {
r.StaticFile(FileName, "fronend/dist/static/"+FileName)
}
}
return r
}