study_xxqg/web/router.go

183 lines
3.9 KiB
Go
Raw Normal View History

// Package web
// @Description: 封装了所以web相关的内容
2022-04-20 13:31:46 +00:00
package web
import (
"embed"
"net/http"
"strings"
2022-04-20 13:31:46 +00:00
"github.com/gin-gonic/gin"
2022-04-25 10:39:54 +00:00
2022-08-03 10:04:52 +00:00
"github.com/johlanse/study_xxqg/conf"
"github.com/johlanse/study_xxqg/utils"
2022-09-06 14:56:18 +00:00
"github.com/johlanse/study_xxqg/utils/update"
2022-04-20 13:31:46 +00:00
)
// 将静态文件嵌入到可执行程序中来
2022-04-20 13:31:46 +00:00
//go:embed xxqg/build
var static embed.FS
// RouterInit
// @Description:
// @return *gin.Engine
func RouterInit() *gin.Engine {
2022-07-24 07:56:12 +00:00
router := gin.Default()
router.RemoveExtraSlash = true
router.Use(cors())
// 挂载静态文件
2022-04-20 13:31:46 +00:00
router.StaticFS("/static", http.FS(static))
// 访问首页时跳转到对应页面
router.GET("/", func(ctx *gin.Context) {
ctx.Redirect(301, "/static/xxqg/build/home.html")
})
2022-04-25 10:39:54 +00:00
router.GET("/about", func(context *gin.Context) {
context.JSON(200, Resp{
Code: 200,
Message: "",
Data: utils.GetAbout(),
Success: true,
Error: "",
})
})
2022-09-06 14:56:18 +00:00
router.POST("/restart", check(), func(ctx *gin.Context) {
if ctx.GetInt("level") == 1 {
ctx.JSON(200, Resp{
Code: 200,
Message: "",
Data: nil,
Success: true,
Error: "",
})
utils.Restart()
} else {
ctx.JSON(200, Resp{
Code: 401,
Message: "",
Data: nil,
Success: false,
Error: "",
})
}
})
router.POST("/update", check(), func(ctx *gin.Context) {
if ctx.GetInt("level") == 1 {
update.SelfUpdate("", conf.GetVersion())
ctx.JSON(200, Resp{
Code: 200,
Message: "",
Data: nil,
Success: true,
Error: "",
})
utils.Restart()
} else {
ctx.JSON(200, Resp{
Code: 401,
Message: "",
Data: nil,
Success: false,
Error: "",
})
}
})
2022-08-15 07:07:03 +00:00
if utils.FileIsExist("./config/flutter_xxqg/") {
router.StaticFS("/flutter_xxqg", http.Dir("./config/flutter_xxqg/"))
}
// 对权限的管理组
2022-04-25 10:39:54 +00:00
auth := router.Group("/auth")
// 用户登录的接口
auth.POST("/login", userLogin())
// 检查登录状态的token是否正确
auth.POST("/check/:token", checkToken())
2022-04-25 10:39:54 +00:00
// 对于用户可自定义挂载文件的目录
if utils.FileIsExist("./config/dist/") {
router.StaticFS("/dist", http.Dir("./config/dist/"))
2022-04-25 10:39:54 +00:00
}
2022-09-06 14:56:18 +00:00
config := router.Group("/config", check())
config.GET("", configGet())
config.POST("", configSet())
2022-09-25 15:58:04 +00:00
config.GET("/file", configFileGet())
config.POST("/file", configFileSet())
2022-09-06 14:56:18 +00:00
// 对用户管理的组
2022-04-25 10:39:54 +00:00
user := router.Group("/user", check())
2022-04-20 13:31:46 +00:00
// 添加用户
2022-07-24 07:56:12 +00:00
user.POST("", addUser())
// 获取所以已登陆的用户
2022-08-04 10:03:53 +00:00
user.GET("", getUsers())
2022-09-10 13:43:29 +00:00
user.GET("/expired", getExpiredUser())
// 删除用户
2022-08-04 10:03:53 +00:00
user.DELETE("", deleteUser())
2022-07-29 07:55:42 +00:00
// 获取用户成绩
2022-04-20 13:31:46 +00:00
router.GET("/score", getScore())
// 让一个用户开始学习
2022-04-20 13:31:46 +00:00
router.POST("/study", study())
// 让一个用户停止学习
2022-04-25 10:39:54 +00:00
router.POST("/stop_study", check(), stopStudy())
// 获取程序当天的运行日志
2022-04-25 10:39:54 +00:00
router.GET("/log", check(), getLog())
2022-04-20 13:31:46 +00:00
// 登录xxqg的三个接口
router.GET("/sign/", sign())
router.GET("/login/*proxyPath", generate())
2022-04-25 10:39:54 +00:00
router.POST("/login/*proxyPath", check(), generate())
2022-04-20 13:31:46 +00:00
return router
}
2022-04-25 10:39:54 +00:00
func check() gin.HandlerFunc {
2022-07-27 10:21:49 +00:00
config := conf.GetConfig()
2022-04-25 10:39:54 +00:00
return func(ctx *gin.Context) {
token := ctx.GetHeader("Authorization")
token = strings.Split(token, " ")[1]
if token == "" {
ctx.JSON(401, Resp{
Code: 401,
2022-04-25 10:39:54 +00:00
Message: "the auth fail",
Data: nil,
Success: false,
Error: "",
})
ctx.Abort()
} else if utils.StrMd5(config.Web.Account+config.Web.Password) == token {
ctx.Set("level", 1)
ctx.Set("token", token)
ctx.Next()
} else if checkCommonUser(token) {
ctx.Set("level", 2)
ctx.Set("token", token)
2022-04-25 10:39:54 +00:00
ctx.Next()
} else {
ctx.JSON(401, Resp{
Code: 401,
Message: "the auth fail",
Data: nil,
Success: false,
Error: "",
})
ctx.Abort()
}
}
}
func checkCommonUser(token string) bool {
config := conf.GetConfig()
for key, value := range config.Web.CommonUser {
if token == utils.StrMd5(key+value) {
return true
2022-04-25 10:39:54 +00:00
}
}
return false
2022-04-25 10:39:54 +00:00
}