Skip to content

Commit 3846bcb

Browse files
committed
refactor: update static file serving and SPA routing in main.go
- Adjust static file serving for Next.js assets, including favicon and SVGs - Implement main page serving and fallback for unmatched routes - Clean up comments for clarity and organization
1 parent d4acafa commit 3846bcb

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

main.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: Vincent Yang
33
* @Date: 2024-04-09 03:35:57
44
* @LastEditors: Vincent Yang
5-
* @LastEditTime: 2024-04-09 20:28:44
5+
* @LastEditTime: 2025-07-12 04:55:18
66
* @FilePath: /discord-image/main.go
77
* @Telegram: https://t.me/missuo
88
* @GitHub: https://github.com/missuo
@@ -81,7 +81,6 @@ func main() {
8181
r := gin.Default()
8282
r.Use(cors.Default())
8383

84-
// API routes must be defined before static file serving
8584
// Upload image API
8685
r.POST("/upload", func(c *gin.Context) {
8786
host := c.Request.Host
@@ -151,11 +150,21 @@ func main() {
151150
c.Redirect(http.StatusFound, url)
152151
})
153152

154-
// Serve static files from public directory (Next.js export)
155-
// This must come after all API routes to avoid conflicts
156-
r.Static("/", "./public")
157-
158-
// Fallback for SPA routing - serve index.html for any route that doesn't match static files or API routes
153+
// Serve Next.js static assets
154+
r.Static("/_next", "./public/_next")
155+
r.StaticFile("/favicon.ico", "./public/favicon.ico")
156+
r.StaticFile("/next.svg", "./public/next.svg")
157+
r.StaticFile("/vercel.svg", "./public/vercel.svg")
158+
r.StaticFile("/file.svg", "./public/file.svg")
159+
r.StaticFile("/globe.svg", "./public/globe.svg")
160+
r.StaticFile("/window.svg", "./public/window.svg")
161+
162+
// Serve main page and handle SPA routing
163+
r.GET("/", func(c *gin.Context) {
164+
c.File("./public/index.html")
165+
})
166+
167+
// Fallback for SPA routing - serve index.html for any unmatched routes
159168
r.NoRoute(func(c *gin.Context) {
160169
c.File("./public/index.html")
161170
})

0 commit comments

Comments
 (0)