From bcc333b142c0fc94928e429ea2d9497c84d7a51b Mon Sep 17 00:00:00 2001 From: wood chen Date: Sat, 14 Jun 2025 20:17:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=B7=AF=E7=94=B1=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E9=80=BB=E8=BE=91=EF=BC=8C=E7=BB=9F=E4=B8=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=89=80=E6=9C=89=E8=B7=AF=E7=94=B1=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=85=AC=E5=BC=80=E9=A6=96=E9=A1=B5=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=96=B9=E6=B3=95=EF=BC=8C=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E5=89=8D=E7=AB=AFAPI=E8=B0=83=E7=94=A8=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=BB=A5=E5=8C=B9=E9=85=8D=E6=96=B0=E8=B7=AF=E7=94=B1=E7=BB=93?= =?UTF-8?q?=E6=9E=84=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handlers/handlers.go | 30 +++++++++++++------- main.go | 10 ++----- router/router.go | 45 +++++++++++++++++++++--------- web/app/page.tsx | 2 +- web/components/admin/LoginPage.tsx | 2 +- 5 files changed, 56 insertions(+), 33 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index f6d4ce4..eee7a30 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -6,8 +6,8 @@ import ( "fmt" "log" "net/http" + "random-api-go/database" "random-api-go/monitoring" - "random-api-go/router" "random-api-go/services" "random-api-go/stats" "random-api-go/utils" @@ -270,15 +270,25 @@ func (h *Handlers) HandleMetrics(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(metrics) } -func (h *Handlers) Setup(r *router.Router) { - // 通用路由处理 - 匹配所有路径 - r.HandleFunc("/", h.HandleAPIRequest) +// HandlePublicHomeConfig 处理公开的首页配置请求 +func (h *Handlers) HandlePublicHomeConfig(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } - // API 统计和监控 - r.HandleFunc("/api/stats", h.HandleStats) - r.HandleFunc("/api/urlstats", h.HandleURLStats) - r.HandleFunc("/api/metrics", h.HandleMetrics) + w.Header().Set("Content-Type", "application/json") - // 公开的端点信息接口 - r.HandleFunc("/api/endpoints", h.HandlePublicEndpoints) + // 从数据库获取首页配置 + content := database.GetConfig("homepage_content", "# 欢迎使用随机API服务\n\n这是一个可配置的随机API服务。") + + response := map[string]interface{}{ + "success": true, + "data": map[string]string{"content": content}, + } + + if err := json.NewEncoder(w).Encode(response); err != nil { + http.Error(w, "Error encoding response", http.StatusInternalServerError) + return + } } diff --git a/main.go b/main.go index d5f7c74..1f66c4e 100644 --- a/main.go +++ b/main.go @@ -83,14 +83,8 @@ func (a *App) Initialize() error { // 创建 handlers handlers := handlers.NewHandlers(a.Stats) - // 设置路由 - a.router.Setup(handlers) - a.router.SetupAdminRoutes(a.adminHandler) - - // 设置静态文件路由(如果静态文件处理器存在) - if a.staticHandler != nil { - a.router.SetupStaticRoutes(a.staticHandler) - } + // 统一设置所有路由 + a.router.SetupAllRoutes(handlers, a.adminHandler, a.staticHandler) // 创建 HTTP 服务器 cfg := config.Get().Server diff --git a/router/router.go b/router/router.go index 2f09677..ec2fcd9 100644 --- a/router/router.go +++ b/router/router.go @@ -14,7 +14,16 @@ type Router struct { // Handler 接口定义处理器需要的方法 type Handler interface { - Setup(r *Router) + // API请求处理 + HandleAPIRequest(w http.ResponseWriter, r *http.Request) + // 统计相关 + HandleStats(w http.ResponseWriter, r *http.Request) + HandleURLStats(w http.ResponseWriter, r *http.Request) + HandleMetrics(w http.ResponseWriter, r *http.Request) + // 公开端点 + HandlePublicEndpoints(w http.ResponseWriter, r *http.Request) + // 公开首页配置 + HandlePublicHomeConfig(w http.ResponseWriter, r *http.Request) } // StaticHandler 接口定义静态文件处理器需要的方法 @@ -62,20 +71,30 @@ func New() *Router { } } -func (r *Router) Setup(h Handler) { - // 设置API路由 - h.Setup(r) +// SetupAllRoutes 统一设置所有路由 +func (r *Router) SetupAllRoutes(handler Handler, adminHandler AdminHandler, staticHandler StaticHandler) { + // 设置公开API路由 + r.HandleFunc("/", handler.HandleAPIRequest) + r.HandleFunc("/api/stats", handler.HandleStats) + r.HandleFunc("/api/urlstats", handler.HandleURLStats) + r.HandleFunc("/api/metrics", handler.HandleMetrics) + r.HandleFunc("/api/endpoints", handler.HandlePublicEndpoints) + r.HandleFunc("/api/home-config", handler.HandlePublicHomeConfig) + + // 设置公开的OAuth配置路由 + r.HandleFunc("/api/oauth-config", adminHandler.GetOAuthConfig) + + // 设置管理后台路由 + r.setupAdminRoutes(adminHandler) + + // 设置静态文件路由 + if staticHandler != nil { + r.staticHandler = staticHandler + } } -// SetupStaticRoutes 设置静态文件路由 -func (r *Router) SetupStaticRoutes(staticHandler StaticHandler) { - r.staticHandler = staticHandler -} - -// SetupAdminRoutes 设置管理后台路由 -func (r *Router) SetupAdminRoutes(adminHandler AdminHandler) { - // OAuth配置API(前端需要获取client_id等信息)- 不需要认证 - r.HandleFunc("/api/admin/oauth-config", adminHandler.GetOAuthConfig) +// setupAdminRoutes 设置管理后台路由(私有方法) +func (r *Router) setupAdminRoutes(adminHandler AdminHandler) { // OAuth令牌验证API(保留,以防需要)- 不需要认证 r.HandleFunc("/api/admin/oauth-verify", adminHandler.VerifyOAuthToken) // OAuth回调处理(使用API前缀以便区分前后端)- 不需要认证 diff --git a/web/app/page.tsx b/web/app/page.tsx index 5332eb5..de0d4a7 100644 --- a/web/app/page.tsx +++ b/web/app/page.tsx @@ -33,7 +33,7 @@ interface SystemMetrics { async function getHomePageConfig() { try { - const res = await apiFetch('/api/admin/home-config') + const res = await apiFetch('/api/home-config') if (!res.ok) { throw new Error('Failed to fetch home page config') } diff --git a/web/components/admin/LoginPage.tsx b/web/components/admin/LoginPage.tsx index defbabe..240b1f3 100644 --- a/web/components/admin/LoginPage.tsx +++ b/web/components/admin/LoginPage.tsx @@ -83,7 +83,7 @@ export default function LoginPage({ onLoginSuccess }: LoginPageProps) { const loadOAuthConfig = async () => { try { console.log('Loading OAuth config...') - const response = await fetch('/api/admin/oauth-config') + const response = await fetch('/api/oauth-config') console.log('OAuth config response status:', response.status) if (response.ok) {