Serve admin console at root path
ci-cd / verify (push) Successful in 1m48s
ci-cd / deploy (push) Skipped

This commit is contained in:
2026-10-01 14:13:04 +08:00
parent e9828d6149
commit 8de48cb44d
12 changed files with 75 additions and 99 deletions
+10 -6
View File
@@ -12,7 +12,7 @@ import (
//go:embed all:dist
var distFS embed.FS
// Register 把内嵌的后台 SPA 挂到 /admin 下,index.html 做 SPA fallback
// Register 把内嵌的后台 SPA 挂到根路径,保留 /api 给业务接口。
func Register(r *gin.Engine) {
sub, err := fs.Sub(distFS, "dist")
if err != nil {
@@ -21,9 +21,7 @@ func Register(r *gin.Engine) {
fileServer := http.FileServer(http.FS(sub))
handler := func(c *gin.Context) {
// 去掉前缀 /admin,交给内嵌文件服务器
reqPath := strings.TrimPrefix(c.Request.URL.Path, "/admin")
reqPath = strings.TrimPrefix(reqPath, "/")
reqPath := strings.TrimPrefix(c.Request.URL.Path, "/")
// 若请求的是真实存在的静态资源,直出;否则回退 index.html(前端路由)
if reqPath != "" {
@@ -43,6 +41,12 @@ func Register(r *gin.Engine) {
c.Data(http.StatusOK, "text/html; charset=utf-8", data)
}
r.GET("/admin", func(c *gin.Context) { c.Redirect(http.StatusFound, "/admin/") })
r.GET("/admin/*filepath", handler)
r.GET("/", handler)
r.NoRoute(func(c *gin.Context) {
if c.Request.Method != http.MethodGet || strings.HasPrefix(c.Request.URL.Path, "/api/") || c.Request.URL.Path == "/api" || strings.HasPrefix(c.Request.URL.Path, "/assets/") {
c.Status(http.StatusNotFound)
return
}
handler(c)
})
}