study_xxqg/web/router.go

110 lines
2.6 KiB
Go
Raw Normal View History

// Package web
// @Description: 封装了所以web相关的内容
2022-04-20 13:31:46 +00:00
package web
import (
"embed"
"net/http"
2022-08-04 10:03:53 +00:00
"strings"
2022-04-20 13:31:46 +00:00
"github.com/gin-gonic/gin"
2022-08-04 10:03:53 +00:00
log "github.com/sirupsen/logrus"
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-04-20 13:31:46 +00:00
)
// 将静态文件嵌入到可执行程序中来
2022-04-20 13:31:46 +00:00
//go:embed xxqg/build
var static embed.FS
2022-08-04 10:03:53 +00:00
//go:embed app
var newUI embed.FS
2022-04-20 13:31:46 +00:00
// 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, utils.GetAbout())
})
2022-08-04 10:03:53 +00:00
router.GET("/new/*file", func(ctx *gin.Context) {
if strings.HasSuffix(ctx.Request.URL.Path, "js") {
ctx.Header("Content-Type", "application/javascript; charset=utf-8")
}
f, err := newUI.ReadFile(strings.TrimLeft(ctx.Param("file"), "/"))
if err != nil {
log.Errorln(err.Error())
f, _ = newUI.ReadFile("app/index.html")
2022-08-04 10:03:53 +00:00
}
2022-08-05 09:25:12 +00:00
_, _ = ctx.Writer.Write(f)
2022-08-04 10:03:53 +00:00
ctx.Status(200)
})
// 对权限的管理组
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-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-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/", check(), sign())
2022-04-25 10:39:54 +00:00
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()
}
}
}