From 3014f8acf659f2829bc2bd18878c0a96b3cda63e Mon Sep 17 00:00:00 2001 From: jdysya <1912377458@qq.com> Date: Sat, 15 Feb 2025 14:03:21 +0800 Subject: [PATCH] =?UTF-8?q?feat(gateway):=20=E6=96=B0=E5=A2=9E=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E9=9D=99=E6=80=81=E6=96=87=E4=BB=B6=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 main.go 中添加了通用静态文件路由 - 路由会检查请求路径对应的静态文件是否存在,若存在则返回文件,否则返回 404 - 此功能便于后续添加前端路由和实现 PWA(渐进式 Web 应用) --- gateway/main.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/gateway/main.go b/gateway/main.go index 7a0d5b2..1ce55c3 100644 --- a/gateway/main.go +++ b/gateway/main.go @@ -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") }