Initial commit
This commit is contained in:
43
internal/webhandlers/auth_handler.go
Normal file
43
internal/webhandlers/auth_handler.go
Normal file
@@ -0,0 +1,43 @@
|
||||
// Filename: internal/webhandlers/auth_handler.go (最终现代化改造版)
|
||||
package webhandlers
|
||||
|
||||
import (
|
||||
"gemini-balancer/internal/middleware"
|
||||
"gemini-balancer/internal/service" // [核心改造] 依赖service层
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// WebAuthHandler [核心改造] 依赖关系净化,注入SecurityService
|
||||
type WebAuthHandler struct {
|
||||
securityService *service.SecurityService
|
||||
}
|
||||
|
||||
// NewWebAuthHandler [核心改造] 构造函数更新
|
||||
func NewWebAuthHandler(securityService *service.SecurityService) *WebAuthHandler {
|
||||
return &WebAuthHandler{
|
||||
securityService: securityService,
|
||||
}
|
||||
}
|
||||
|
||||
// ShowLoginPage 保持不变
|
||||
func (h *WebAuthHandler) ShowLoginPage(c *gin.Context) {
|
||||
errMsg := c.Query("error")
|
||||
from := c.Query("from") // 可以从登录失败的页面返回
|
||||
c.HTML(http.StatusOK, "auth.html", gin.H{
|
||||
"error": errMsg,
|
||||
"from": from,
|
||||
})
|
||||
}
|
||||
|
||||
// HandleLogin [核心改造] 认证逻辑完全委托给SecurityService
|
||||
func (h *WebAuthHandler) HandleLogin(c *gin.Context) {
|
||||
c.Redirect(http.StatusFound, "/login?error=DEPRECATED_LOGIN_METHOD")
|
||||
}
|
||||
|
||||
// HandleLogout 保持不变
|
||||
func (h *WebAuthHandler) HandleLogout(c *gin.Context) {
|
||||
middleware.ClearAdminSessionCookie(c)
|
||||
c.Redirect(http.StatusFound, "/login")
|
||||
}
|
||||
119
internal/webhandlers/page_handler.go
Normal file
119
internal/webhandlers/page_handler.go
Normal file
@@ -0,0 +1,119 @@
|
||||
// Filename: internal/webhandlers/page_handler.go (测试前版本)
|
||||
|
||||
package webhandlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"gemini-balancer/internal/service"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// PageHandler
|
||||
type PageHandler struct {
|
||||
queryService *service.DashboardQueryService
|
||||
}
|
||||
|
||||
// 构造函数注入新依赖
|
||||
func NewPageHandler(queryService *service.DashboardQueryService) *PageHandler {
|
||||
return &PageHandler{queryService: queryService}
|
||||
}
|
||||
|
||||
// ShowDashboardPage 渲染现代化的监控面板页面
|
||||
func (h *PageHandler) ShowDashboardPage(c *gin.Context) {
|
||||
// [核心改造] 调用高速缓存读取方法,为模板准备数据
|
||||
overviewData, err := h.queryService.GetDashboardOverviewData()
|
||||
if err != nil {
|
||||
// 在SSR模式下,如果缓存未就绪,我们可以传递一个空对象或默认值
|
||||
// 这样页面至少可以渲染出骨架,而不是直接报错
|
||||
c.HTML(http.StatusOK, "dashboard.html", gin.H{
|
||||
"PageID": "dashboard",
|
||||
"pageTitle": "监控面板",
|
||||
"overview": nil, // 或者一个默认的空结构体实例
|
||||
"error": "Dashboard data is currently being generated. Please refresh in a moment.",
|
||||
"ssr_error": err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
c.HTML(http.StatusOK, "dashboard.html", gin.H{
|
||||
"PageID": "dashboard",
|
||||
"pageTitle": "监控面板",
|
||||
"overview": overviewData,
|
||||
"ssr_error": nil,
|
||||
})
|
||||
}
|
||||
|
||||
// ShowConfigEditorPage 渲染配置编辑页面。
|
||||
func (h *PageHandler) ShowConfigEditorPage(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "settings.html", gin.H{
|
||||
"PageID": "settings",
|
||||
"pageTitle": "配置编辑",
|
||||
"currentPage": "settings",
|
||||
})
|
||||
}
|
||||
|
||||
// ShowErrorLogsPage 渲染错误日志页面。
|
||||
func (h *PageHandler) ShowErrorLogsPage(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "logs.html", gin.H{
|
||||
"PageID": "logs",
|
||||
"pageTitle": "错误日志",
|
||||
})
|
||||
}
|
||||
|
||||
// ShowKeysPage 渲染密钥管理页面。
|
||||
func (h *PageHandler) ShowKeysPage(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "keys.html", gin.H{
|
||||
"PageID": "keys",
|
||||
"pageTitle": "密钥管理",
|
||||
})
|
||||
}
|
||||
|
||||
// ShowTasksPage renders the M:N testing page.
|
||||
func (h *PageHandler) ShowTasksPage(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "tasks.html", gin.H{
|
||||
"PageID": "tasks",
|
||||
"pageTitle": "计划任务",
|
||||
})
|
||||
}
|
||||
|
||||
// ShowTasksPage renders the M:N testing page.
|
||||
func (h *PageHandler) ShowChatPage(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "chat.html", gin.H{
|
||||
"PageID": "chat",
|
||||
"pageTitle": "Webchat",
|
||||
})
|
||||
}
|
||||
|
||||
// 它只负责从 DashboardQueryService 获取数据,并将其作为JSON字符串渲染出来。
|
||||
func (h *PageHandler) ShowDashboardDebugPage(c *gin.Context) {
|
||||
// 1. 调用与正式Dashboard完全相同的数据获取方法
|
||||
overviewData, err := h.queryService.GetDashboardOverviewData()
|
||||
if err != nil {
|
||||
// 如果出错,直接将错误信息打印出来
|
||||
c.HTML(http.StatusInternalServerError, "dashboard_debug.html", gin.H{
|
||||
"PageID": "dashboard_debug",
|
||||
"pageTitle": "Dashboard 诊断 - 错误",
|
||||
"overview_json": "获取数据时发生错误: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 2. 将获取到的Go数据结构,格式化(美化)成JSON字符串
|
||||
// 使用 MarshalIndent 是为了让输出的JSON带缩进,便于阅读
|
||||
jsonData, err := json.MarshalIndent(overviewData, "", " ")
|
||||
if err != nil {
|
||||
// 如果JSON序列化出错,也直接打印错误
|
||||
c.HTML(http.StatusInternalServerError, "dashboard_debug.html", gin.H{
|
||||
"PageID": "dashboard_debug",
|
||||
"pageTitle": "Dashboard 诊断 - 错误",
|
||||
"overview_json": "序列化JSON时发生错误: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
// 3. 将这个JSON字符串,直接传递给一个极简的HTML模板
|
||||
c.HTML(http.StatusOK, "dashboard_debug.html", gin.H{
|
||||
"PageID": "dashboard-debug",
|
||||
"pageTitle": "Dashboard 诊断 - 成功",
|
||||
"overview_json": string(jsonData), // 将 []byte 转换为 string
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user