修改入口文件main.go,更新了用户处理器和考试服务的逻辑,同时修改了成绩服务的实现。请注意,config.yaml文件有未暂存的更改,需要先将其添加到暂存区才能提交。
This commit is contained in:
+24
@@ -8,6 +8,8 @@ import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -27,6 +29,28 @@ func main() {
|
||||
// 全局中间件
|
||||
r.Use(middleware.Cors())
|
||||
|
||||
// 静态文件服务和前端路由回退处理
|
||||
distPath := "frontend/dist"
|
||||
if _, err := os.Stat(distPath); err == nil {
|
||||
// dist 目录存在,提供静态文件服务
|
||||
r.Static("/static", distPath)
|
||||
|
||||
// 前端路由回退处理(SPA 支持)
|
||||
r.NoRoute(func(c *gin.Context) {
|
||||
// API 路由未找到,返回 404
|
||||
if len(c.Request.URL.Path) >= 4 && c.Request.URL.Path[:4] == "/api" {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "API not found"})
|
||||
return
|
||||
}
|
||||
// 其他路由,返回前端 index.html(由 vue-router 处理)
|
||||
c.File(distPath + "/index.html")
|
||||
})
|
||||
} else {
|
||||
// dist 目录不存在,提示需要构建前端
|
||||
fmt.Println("⚠️ Frontend dist directory not found. Please build frontend first.")
|
||||
fmt.Println(" Run: cd frontend && npm install && npm run build")
|
||||
}
|
||||
|
||||
// 设置路由
|
||||
routes.SetupRouter(r)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user