fix: 从源头解决下划线文件名问题并优化代码
- Vite配置chunkFileNames去除开头的下划线,避免Go embed/http.FS过滤 - 使用mime.TypeByExtension自动推断Content-Type,替代手动映射 - 移除调试日志(文件列表、全局中间件打印)
This commit is contained in:
+7
-52
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"mime"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
@@ -228,11 +229,6 @@ func startServer() {
|
||||
r.Use(middleware.Recovery())
|
||||
r.Use(middleware.Cors())
|
||||
|
||||
r.Use(func(c *gin.Context) {
|
||||
fmt.Printf("全局中间件: %s %s\n", c.Request.Method, c.Request.URL.Path)
|
||||
c.Next()
|
||||
})
|
||||
|
||||
r.Static("/uploads", "uploads")
|
||||
router.SetupRoutes(r)
|
||||
setupEmbeddedFrontend(r)
|
||||
@@ -255,17 +251,6 @@ func setupEmbeddedFrontend(r *gin.Engine) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("[Embed] Listing embedded files in assets/:")
|
||||
fs.WalkDir(distFS, "assets", func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !d.IsDir() {
|
||||
log.Printf("[Embed] %s", path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
indexHTML, err := fs.ReadFile(distFS, "index.html")
|
||||
if err != nil {
|
||||
log.Printf("Warning: index.html not found in embedded dist")
|
||||
@@ -284,36 +269,15 @@ func setupEmbeddedFrontend(r *gin.Engine) {
|
||||
|
||||
data, err := fs.ReadFile(distFS, fullPath)
|
||||
if err != nil {
|
||||
log.Printf("[Assets] File not found in embedded FS: %s, error: %v", fullPath, err)
|
||||
c.Status(404)
|
||||
return
|
||||
}
|
||||
|
||||
contentType := "application/octet-stream"
|
||||
switch {
|
||||
case strings.HasSuffix(decodedPath, ".js"):
|
||||
contentType = "application/javascript"
|
||||
case strings.HasSuffix(decodedPath, ".css"):
|
||||
contentType = "text/css"
|
||||
case strings.HasSuffix(decodedPath, ".html"):
|
||||
contentType = "text/html"
|
||||
case strings.HasSuffix(decodedPath, ".json"):
|
||||
contentType = "application/json"
|
||||
case strings.HasSuffix(decodedPath, ".png"):
|
||||
contentType = "image/png"
|
||||
case strings.HasSuffix(decodedPath, ".jpg") || strings.HasSuffix(decodedPath, ".jpeg"):
|
||||
contentType = "image/jpeg"
|
||||
case strings.HasSuffix(decodedPath, ".svg"):
|
||||
contentType = "image/svg+xml"
|
||||
case strings.HasSuffix(decodedPath, ".ico"):
|
||||
contentType = "image/x-icon"
|
||||
case strings.HasSuffix(decodedPath, ".woff"):
|
||||
contentType = "font/woff"
|
||||
case strings.HasSuffix(decodedPath, ".woff2"):
|
||||
contentType = "font/woff2"
|
||||
contentType := mime.TypeByExtension(decodedPath)
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
|
||||
c.Header("Content-Type", contentType)
|
||||
c.Header("Cache-Control", "public, max-age=31536000, immutable")
|
||||
c.Data(200, contentType, data)
|
||||
})
|
||||
@@ -330,18 +294,9 @@ func setupEmbeddedFrontend(r *gin.Engine) {
|
||||
decodedPath = path
|
||||
}
|
||||
if data, err := fs.ReadFile(distFS, decodedPath); err == nil {
|
||||
contentType := "application/octet-stream"
|
||||
switch {
|
||||
case strings.HasSuffix(decodedPath, ".js"):
|
||||
contentType = "application/javascript"
|
||||
case strings.HasSuffix(decodedPath, ".css"):
|
||||
contentType = "text/css"
|
||||
case strings.HasSuffix(decodedPath, ".html"):
|
||||
contentType = "text/html"
|
||||
case strings.HasSuffix(decodedPath, ".png"):
|
||||
contentType = "image/png"
|
||||
case strings.HasSuffix(decodedPath, ".svg"):
|
||||
contentType = "image/svg+xml"
|
||||
contentType := mime.TypeByExtension(decodedPath)
|
||||
if contentType == "" {
|
||||
contentType = "application/octet-stream"
|
||||
}
|
||||
c.Data(200, contentType, data)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user