mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
feat(templates): Implement template rendering for admin routes
- Replace static file serving with Go template rendering - Add template parsing for login, metrics, and config pages - Set appropriate content type headers for HTML responses - Improve error handling for template rendering - Enhance logging for template-related errors
This commit is contained in:
parent
dca6a8a362
commit
8a2207bd29
34
main.go
34
main.go
@ -13,6 +13,7 @@ import (
|
|||||||
"proxy-go/internal/middleware"
|
"proxy-go/internal/middleware"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -22,6 +23,17 @@ func main() {
|
|||||||
log.Fatal("Error loading config:", err)
|
log.Fatal("Error loading config:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 加载模板
|
||||||
|
tmpl, err := template.ParseFiles(
|
||||||
|
"/app/web/templates/admin/layout.html",
|
||||||
|
"/app/web/templates/admin/login.html",
|
||||||
|
"/app/web/templates/admin/metrics.html",
|
||||||
|
"/app/web/templates/admin/config.html",
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Error parsing templates:", err)
|
||||||
|
}
|
||||||
|
|
||||||
// 更新常量配置
|
// 更新常量配置
|
||||||
constants.UpdateFromConfig(cfg)
|
constants.UpdateFromConfig(cfg)
|
||||||
|
|
||||||
@ -63,11 +75,27 @@ func main() {
|
|||||||
switch r.URL.Path {
|
switch r.URL.Path {
|
||||||
case "/admin/login":
|
case "/admin/login":
|
||||||
log.Printf("[Debug] 提供登录页面,文件路径: /app/web/templates/admin/login.html")
|
log.Printf("[Debug] 提供登录页面,文件路径: /app/web/templates/admin/login.html")
|
||||||
http.ServeFile(w, r, "/app/web/templates/admin/login.html")
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
if err := tmpl.ExecuteTemplate(w, "login.html", nil); err != nil {
|
||||||
|
log.Printf("[Error] 渲染登录页面失败: %v", err)
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
case "/admin/metrics":
|
case "/admin/metrics":
|
||||||
proxyHandler.AuthMiddleware(proxyHandler.MetricsHandler)(w, r)
|
proxyHandler.AuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
if err := tmpl.ExecuteTemplate(w, "metrics.html", nil); err != nil {
|
||||||
|
log.Printf("[Error] 渲染监控页面失败: %v", err)
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}))(w, r)
|
||||||
case "/admin/config":
|
case "/admin/config":
|
||||||
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
proxyHandler.AuthMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||||
|
if err := tmpl.ExecuteTemplate(w, "config.html", nil); err != nil {
|
||||||
|
log.Printf("[Error] 渲染配置页面失败: %v", err)
|
||||||
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}))(w, r)
|
||||||
case "/admin/config/get":
|
case "/admin/config/get":
|
||||||
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
||||||
case "/admin/config/save":
|
case "/admin/config/save":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user