-
Notifications
You must be signed in to change notification settings - Fork 16
/
router.go
55 lines (46 loc) · 1.44 KB
/
router.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
package api
import (
"embed"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/itihey/tikuAdapter/internal/controller"
"github.com/itihey/tikuAdapter/internal/middleware"
"io/fs"
"net/http"
)
// SetAPIRouter 设置API路由
func SetAPIRouter(router *gin.Engine) {
router.Any("/sqp/*path", controller.Proxy)
apiRouter := router.Group("/adapter-service")
apiRouter.Use(middleware.GlobalAPIRateLimit) // 全局限流
apiRouter.POST("/search", controller.Search)
apiRouter.POST("/parser", controller.Parser)
apiRouter.POST("/questions/search", controller.GetQuestions)
apiRouter.POST("/questions", controller.CreateQuestion)
apiRouter.PUT("/questions/:id", controller.UpdateQuestions)
apiRouter.DELETE("/questions/:id", controller.DeleteQuestion)
}
type embedFileSystem struct {
http.FileSystem
}
// Exists 判断文件是否存在
func (e embedFileSystem) Exists(_ string, path string) bool {
_, err := e.Open(path)
return err == nil
}
func embedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
efs, err := fs.Sub(fsEmbed, targetPath)
if err != nil {
panic(err)
}
return embedFileSystem{
FileSystem: http.FS(efs),
}
}
// SetWebRouter 设置web路由
func SetWebRouter(buildFS embed.FS, indexPage []byte, router *gin.Engine) {
router.Use(static.Serve("/", embedFolder(buildFS, "dist")))
router.NoRoute(func(c *gin.Context) {
c.Data(http.StatusOK, "text/html; charset=utf-8", indexPage)
})
}