feat(gateway): 新增通用静态文件路由

- 在 main.go 中添加了通用静态文件路由
- 路由会检查请求路径对应的静态文件是否存在,若存在则返回文件,否则返回 404
- 此功能便于后续添加前端路由和实现 PWA(渐进式 Web 应用)
This commit is contained in:
高手 2025-02-15 14:03:21 +08:00
parent b1e31d86ed
commit 3014f8acf6

View File

@ -100,6 +100,17 @@ func main() {
c.Redirect(http.StatusSeeOther, "/login")
})
// 新增通用静态文件路由(放在其他路由之后)
r.NoRoute(func(c *gin.Context) {
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)
}
})
// 启动服务
r.Run(":7070")
}