study_xxqg/web/router.go

74 lines
1.5 KiB
Go
Raw Normal View History

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
)
//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()
2022-04-20 13:31:46 +00:00
router.Use(Cors())
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
auth := router.Group("/auth")
auth.POST("/login", Login())
auth.POST("/check/:token", CheckToken())
if utils.FileIsExist("dist") {
router.StaticFS("/dist", gin.Dir("./dist/", true))
}
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())
router.GET("/score", getScore())
router.POST("/study", study())
2022-04-25 10:39:54 +00:00
router.POST("/stop_study", check(), stopStudy())
2022-04-20 13:31:46 +00:00
2022-04-25 10:39:54 +00:00
router.GET("/log", check(), getLog())
2022-04-20 13:31:46 +00:00
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()
}
}
}