mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 05:42:01 +08:00
重构路由设置逻辑,统一配置所有路由,新增公开首页配置处理方法,更新前端API调用路径以匹配新路由结构。
This commit is contained in:
parent
5176cc85db
commit
bcc333b142
@ -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
|
||||
}
|
||||
}
|
||||
|
10
main.go
10
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
|
||||
|
@ -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前缀以便区分前后端)- 不需要认证
|
||||
|
@ -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')
|
||||
}
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user