refactor(admin): Enhance template rendering and routing for admin pages

- Update template rendering to use a dynamic layout with configurable title
- Modify template parsing to use template.Must for simplified error handling
- Update static file serving path from /web/static to /admin/static
- Improve template execution with dynamic content mapping
- Simplify template loading and error management in main.go
This commit is contained in:
wood chen 2025-02-15 09:45:36 +08:00
parent 8a2207bd29
commit ab8684d4c5
2 changed files with 21 additions and 14 deletions

29
main.go
View File

@ -24,15 +24,13 @@ func main() {
}
// 加载模板
tmpl, err := template.ParseFiles(
tmpl := template.New("layout.html")
tmpl = template.Must(tmpl.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)
@ -74,16 +72,22 @@ func main() {
switch r.URL.Path {
case "/admin/login":
log.Printf("[Debug] 提供登录页面,文件路径: /app/web/templates/admin/login.html")
log.Printf("[Debug] 提供登录页面")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if err := tmpl.ExecuteTemplate(w, "login.html", nil); err != nil {
if err := tmpl.ExecuteTemplate(w, "layout.html", map[string]interface{}{
"Title": "管理员登录",
"Content": "login.html",
}); err != nil {
log.Printf("[Error] 渲染登录页面失败: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
case "/admin/metrics":
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 {
if err := tmpl.ExecuteTemplate(w, "layout.html", map[string]interface{}{
"Title": "监控面板",
"Content": "metrics.html",
}); err != nil {
log.Printf("[Error] 渲染监控页面失败: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
@ -91,7 +95,10 @@ func main() {
case "/admin/config":
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 {
if err := tmpl.ExecuteTemplate(w, "layout.html", map[string]interface{}{
"Title": "配置管理",
"Content": "config.html",
}); err != nil {
log.Printf("[Error] 渲染配置页面失败: %v", err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
@ -141,9 +148,9 @@ func main() {
log.Printf("[Debug] 收到请求: %s %s", r.Method, r.URL.Path)
// 处理静态文件
if strings.HasPrefix(r.URL.Path, "/web/static/") {
if strings.HasPrefix(r.URL.Path, "/admin/static/") {
log.Printf("[Debug] 处理静态文件: %s", r.URL.Path)
http.StripPrefix("/web/static/", http.FileServer(http.Dir("/app/web/static"))).ServeHTTP(w, r)
http.StripPrefix("/admin/static/", http.FileServer(http.Dir("/app/web/static"))).ServeHTTP(w, r)
return
}

View File

@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>代理服务管理后台</title>
<title>{{.Title}} - 代理服务管理后台</title>
<link href="https://cdn.jsdelivr.net/npm/daisyui@3.9.4/dist/full.css" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
</head>
@ -22,10 +22,10 @@
</div>
<div class="container mx-auto px-4">
{{template "Content" .}}
{{template .Content .}}
</div>
<script src="/web/static/js/auth.js"></script>
<script src="/admin/static/js/auth.js"></script>
<script>
// 检查是否已登录(除了登录页面)
if (!window.location.pathname.includes('/login')) {