-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
45 lines (38 loc) · 825 Bytes
/
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
package main
import (
"EmptyClassroom/logs"
"embed"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"io/fs"
"net/http"
)
//go:embed frontend/dist
var f embed.FS
type embedFileSystem struct {
http.FileSystem
}
func (e embedFileSystem) Exists(prefix string, path string) bool {
_, err := e.Open(path)
if err != nil {
return false
}
return true
}
func EmbedFolder(fsEmbed embed.FS, targetPath string) static.ServeFileSystem {
fsys, err := fs.Sub(fsEmbed, targetPath)
if err != nil {
panic(err)
}
return embedFileSystem{
FileSystem: http.FS(fsys),
}
}
func SetRouter(r *gin.Engine) {
r.Use(static.Serve("/", EmbedFolder(f, "frontend/dist")))
apiGroup := r.Group("/api").Use(logs.SetNewContextForGinContext)
{
apiGroup.GET("/get_data", GetData)
apiGroup.POST("/report", Report)
}
}