feat(web): Update file paths for static and template serving in Docker environment

- Modify Dockerfile to copy web directory into /app
- Update main.go to serve static and template files from absolute paths (/app/web)
- Add debug logging for static file and template route handling
- Ensure consistent file serving across admin and web routes
This commit is contained in:
wood chen 2025-02-15 09:29:55 +08:00
parent 0446eb1c53
commit fa5cbff486
2 changed files with 13 additions and 3 deletions

View File

@ -4,6 +4,7 @@ ARG TARGETARCH
WORKDIR /app
COPY proxy-go.${TARGETARCH} /app/proxy-go
COPY web /app/web
RUN mkdir -p /app/data && \
chmod +x /app/proxy-go && \

15
main.go
View File

@ -52,10 +52,18 @@ func main() {
},
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("[Debug] 处理管理路由: %s", r.URL.Path)
// 处理静态文件
if strings.HasPrefix(r.URL.Path, "/admin/static/") {
log.Printf("[Debug] 处理静态文件: %s", r.URL.Path)
http.StripPrefix("/admin/static/", http.FileServer(http.Dir("/app/web/static"))).ServeHTTP(w, r)
return
}
switch r.URL.Path {
case "/admin/login":
log.Printf("[Debug] 提供登录页面")
http.ServeFile(w, r, "web/templates/admin/login.html")
log.Printf("[Debug] 提供登录页面,文件路径: /app/web/templates/admin/login.html")
http.ServeFile(w, r, "/app/web/templates/admin/login.html")
case "/admin/metrics":
proxyHandler.AuthMiddleware(proxyHandler.MetricsHandler)(w, r)
case "/admin/config":
@ -67,6 +75,7 @@ func main() {
case "/admin/auth":
proxyHandler.AuthHandler(w, r)
default:
log.Printf("[Debug] 未找到管理路由: %s", r.URL.Path)
http.NotFound(w, r)
}
}),
@ -106,7 +115,7 @@ func main() {
// 处理静态文件
if strings.HasPrefix(r.URL.Path, "/web/static/") {
log.Printf("[Debug] 处理静态文件: %s", r.URL.Path)
http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static"))).ServeHTTP(w, r)
http.StripPrefix("/web/static/", http.FileServer(http.Dir("/app/web/static"))).ServeHTTP(w, r)
return
}