This commit is contained in:
2026-03-20 22:26:01 +08:00
parent 5a8518f6ab
commit 28039368cb
6 changed files with 53 additions and 246 deletions

View File

@@ -29,13 +29,22 @@ func main() {
// 全局中间件
r.Use(middleware.Cors())
// 设置路由(必须在静态文件服务之前)
routes.SetupRouter(r)
// 静态文件服务和前端路由回退处理
distPath := "frontend/dist"
if _, err := os.Stat(distPath); err == nil {
// dist 目录存在,提供静态文件服务
r.Static("/static", distPath)
// 使用 StaticFS 直接映射根路径到 dist 目录
r.StaticFS("/static", http.Dir(distPath+"/static"))
// 前端路由回退处理SPA 支持)
// 显式处理根路径 - 返回 index.html
r.GET("/", func(c *gin.Context) {
c.File(distPath + "/index.html")
})
// 前端路由回退处理SPA 支持)- 放在最后
r.NoRoute(func(c *gin.Context) {
// API 路由未找到,返回 404
if len(c.Request.URL.Path) >= 4 && c.Request.URL.Path[:4] == "/api" {
@@ -51,9 +60,6 @@ func main() {
fmt.Println(" Run: cd frontend && npm install && npm run build")
}
// 设置路由
routes.SetupRouter(r)
// 启动服务器
port := config.Config.GetString("server.port")
if err := r.Run(":" + port); err != nil {