mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 05:42:01 +08:00
重构静态文件处理逻辑,新增tryFiles方法以实现类似Nginx的try_files功能,优化路径处理和调试日志;移除不再使用的resolveFilePath方法,简化ServeStatic逻辑。
This commit is contained in:
parent
2394ef7f15
commit
797a941172
1
.gitignore
vendored
1
.gitignore
vendored
@ -4,3 +4,4 @@
|
|||||||
data/data.db
|
data/data.db
|
||||||
data/server.log
|
data/server.log
|
||||||
data/stats.json
|
data/stats.json
|
||||||
|
server.exe
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -17,31 +18,17 @@ func NewStaticHandler(staticDir string) *StaticHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeStatic 处理静态文件请求
|
// ServeStatic 处理静态文件请求,实现类似 Nginx try_files 的逻辑
|
||||||
func (s *StaticHandler) ServeStatic(w http.ResponseWriter, r *http.Request) {
|
func (s *StaticHandler) ServeStatic(w http.ResponseWriter, r *http.Request) {
|
||||||
// 获取请求路径
|
|
||||||
path := r.URL.Path
|
path := r.URL.Path
|
||||||
|
|
||||||
// 如果是根路径,重定向到 index.html
|
// 添加调试日志
|
||||||
if path == "/" {
|
fmt.Printf("DEBUG: 请求路径: %s\n", path)
|
||||||
path = "/index.html"
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理 Next.js 静态导出的路由问题
|
// 实现 try_files $uri $uri/ @router 逻辑
|
||||||
filePath := s.resolveFilePath(path)
|
filePath := s.tryFiles(path)
|
||||||
|
|
||||||
// 检查文件是否存在
|
fmt.Printf("DEBUG: 最终文件路径: %s\n", filePath)
|
||||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
|
||||||
// 如果文件不存在,检查是否是前端路由
|
|
||||||
if s.isFrontendRoute(path) {
|
|
||||||
// 对于前端路由,返回 index.html
|
|
||||||
filePath = filepath.Join(s.staticDir, "index.html")
|
|
||||||
} else {
|
|
||||||
// 不是前端路由,返回 404
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 设置正确的 Content-Type
|
// 设置正确的 Content-Type
|
||||||
s.setContentType(w, filePath)
|
s.setContentType(w, filePath)
|
||||||
@ -50,63 +37,63 @@ func (s *StaticHandler) ServeStatic(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.ServeFile(w, r, filePath)
|
http.ServeFile(w, r, filePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// resolveFilePath 解析文件路径,处理 Next.js 静态导出的路由问题
|
// tryFiles 实现类似 Nginx try_files 的逻辑
|
||||||
func (s *StaticHandler) resolveFilePath(path string) string {
|
func (s *StaticHandler) tryFiles(requestPath string) string {
|
||||||
// 移除查询参数和锚点
|
// 清理路径
|
||||||
if idx := strings.Index(path, "?"); idx != -1 {
|
if requestPath == "" {
|
||||||
path = path[:idx]
|
requestPath = "/"
|
||||||
}
|
|
||||||
if idx := strings.Index(path, "#"); idx != -1 {
|
|
||||||
path = path[:idx]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构建初始文件路径
|
// 移除查询参数
|
||||||
filePath := filepath.Join(s.staticDir, path)
|
if idx := strings.Index(requestPath, "?"); idx != -1 {
|
||||||
|
requestPath = requestPath[:idx]
|
||||||
|
}
|
||||||
|
|
||||||
// 如果路径以斜杠结尾,尝试查找 index.html
|
// 1. 尝试 $uri - 直接文件路径
|
||||||
if strings.HasSuffix(path, "/") {
|
directPath := filepath.Join(s.staticDir, requestPath)
|
||||||
indexPath := filepath.Join(filePath, "index.html")
|
if s.fileExists(directPath) && !s.isDirectory(directPath) {
|
||||||
if _, err := os.Stat(indexPath); err == nil {
|
fmt.Printf("DEBUG: 找到直接文件: %s\n", directPath)
|
||||||
|
return directPath
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 尝试 $uri/ - 目录下的 index.html
|
||||||
|
if requestPath != "/" {
|
||||||
|
dirPath := filepath.Join(s.staticDir, requestPath)
|
||||||
|
if s.isDirectory(dirPath) {
|
||||||
|
indexPath := filepath.Join(dirPath, "index.html")
|
||||||
|
if s.fileExists(indexPath) {
|
||||||
|
fmt.Printf("DEBUG: 找到目录下的 index.html: %s\n", indexPath)
|
||||||
return indexPath
|
return indexPath
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 如果路径不以斜杠结尾,先检查是否存在对应的文件
|
|
||||||
if _, err := os.Stat(filePath); err == nil {
|
|
||||||
return filePath
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果文件不存在,尝试查找对应目录下的 index.html
|
// 也尝试添加 .html 扩展名
|
||||||
indexPath := filepath.Join(filePath, "index.html")
|
htmlPath := directPath + ".html"
|
||||||
if _, err := os.Stat(indexPath); err == nil {
|
if s.fileExists(htmlPath) {
|
||||||
return indexPath
|
fmt.Printf("DEBUG: 找到 HTML 文件: %s\n", htmlPath)
|
||||||
}
|
|
||||||
|
|
||||||
// 尝试添加 .html 扩展名
|
|
||||||
htmlPath := filePath + ".html"
|
|
||||||
if _, err := os.Stat(htmlPath); err == nil {
|
|
||||||
return htmlPath
|
return htmlPath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return filePath
|
// 3. @router - 回退到根目录的 index.html (SPA 路由处理)
|
||||||
|
fallbackPath := filepath.Join(s.staticDir, "index.html")
|
||||||
|
fmt.Printf("DEBUG: 回退到根 index.html: %s\n", fallbackPath)
|
||||||
|
return fallbackPath
|
||||||
}
|
}
|
||||||
|
|
||||||
// isFrontendRoute 判断是否是前端路由
|
// fileExists 检查文件是否存在
|
||||||
func (s *StaticHandler) isFrontendRoute(path string) bool {
|
func (s *StaticHandler) fileExists(path string) bool {
|
||||||
// 前端路由通常以 /admin 开头
|
_, err := os.Stat(path)
|
||||||
if strings.HasPrefix(path, "/admin") {
|
return err == nil
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 排除 API 路径和静态资源
|
// isDirectory 检查路径是否为目录
|
||||||
if strings.HasPrefix(path, "/api/") ||
|
func (s *StaticHandler) isDirectory(path string) bool {
|
||||||
strings.HasPrefix(path, "/_next/") ||
|
info, err := os.Stat(path)
|
||||||
strings.HasPrefix(path, "/static/") ||
|
if err != nil {
|
||||||
strings.Contains(path, ".") {
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
return info.IsDir()
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setContentType 设置正确的 Content-Type
|
// setContentType 设置正确的 Content-Type
|
||||||
|
@ -143,20 +143,6 @@ func (r *Router) HandleFunc(pattern string, handler func(http.ResponseWriter, *h
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
// 规范化路径,处理尾斜杠问题
|
|
||||||
path := req.URL.Path
|
|
||||||
|
|
||||||
// 对于前端路由,统一处理尾斜杠
|
|
||||||
if strings.HasPrefix(path, "/admin") && path != "/admin" && strings.HasSuffix(path, "/") {
|
|
||||||
// 移除尾斜杠并重定向
|
|
||||||
newPath := strings.TrimSuffix(path, "/")
|
|
||||||
if req.URL.RawQuery != "" {
|
|
||||||
newPath += "?" + req.URL.RawQuery
|
|
||||||
}
|
|
||||||
http.Redirect(w, req, newPath, http.StatusMovedPermanently)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 首先检查是否是静态文件请求或前端路由
|
// 首先检查是否是静态文件请求或前端路由
|
||||||
if r.staticHandler != nil && r.shouldServeStatic(req.URL.Path) {
|
if r.staticHandler != nil && r.shouldServeStatic(req.URL.Path) {
|
||||||
r.staticHandler.ServeStatic(w, req)
|
r.staticHandler.ServeStatic(w, req)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user