study_xxqg/web/router.go

88 lines
2.0 KiB
Go
Raw Normal View History

// Package web
// @Description: 封装了所以web相关的内容
2022-04-20 13:31:46 +00:00
package web
import (
"embed"
"net/http"
"github.com/gin-gonic/gin"
2022-04-25 10:39:54 +00:00
2022-07-27 10:21:49 +00:00
"github.com/huoxue1/study_xxqg/conf"
2022-04-25 10:39:54 +00:00
"github.com/huoxue1/study_xxqg/utils"
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.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
// 对权限的管理组
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
// 对于用户可自定义挂载文件的目录
2022-04-25 10:39:54 +00:00
if utils.FileIsExist("dist") {
router.StaticFS("/dist", gin.Dir("./dist/", true))
}
// 对用户管理的组
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-04-20 13:31:46 +00:00
user.GET("/", getUsers())
// 删除用户
2022-07-29 07:55:42 +00:00
user.DELETE("/", deleteUser())
// 获取用户成绩
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的三个接口
2022-04-25 10:39:54 +00:00
router.GET("/sign/*proxyPath", check(), sign())
router.GET("/login/*proxyPath", check(), generate())
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("xxqg_token")
if token == "" || (utils.StrMd5(config.Web.Account+config.Web.Password) != token) {
ctx.JSON(403, Resp{
Code: 403,
Message: "the auth fail",
Data: nil,
Success: false,
Error: "",
})
ctx.Abort()
} else {
ctx.Next()
}
}
}