- 移除日志初始化代码,改为使用 utils.InitLogger() - 删除用户模型定义,移至 models 包 - 抽离路由处理逻辑到 handlers 包 - 使用 middleware 包中的 AuthRequired 中间件 - 优化数据库连接和迁移逻辑 - 简化 main 函数,提高代码可读性和维护性
28 lines
610 B
Go
28 lines
610 B
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
|
|
"gateway/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// 处理首页请求
|
|
func ServeIndex(c *gin.Context) {
|
|
utils.LogAccess(c.ClientIP(), c.Request.URL.Path, c.Request.Method)
|
|
http.ServeFile(c.Writer, c.Request, "./static/index.html")
|
|
}
|
|
|
|
// 处理静态文件请求
|
|
func ServeStatic(c *gin.Context) {
|
|
utils.LogAccess(c.ClientIP(), c.Request.URL.Path, c.Request.Method)
|
|
filePath := "./static" + c.Request.URL.Path
|
|
if _, err := os.Stat(filePath); err == nil {
|
|
http.ServeFile(c.Writer, c.Request, filePath)
|
|
} else {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
}
|
|
}
|