mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 13:52:02 +08:00
重构路由设置逻辑,统一配置所有路由,新增公开首页配置处理方法,更新前端API调用路径以匹配新路由结构。
This commit is contained in:
parent
5176cc85db
commit
bcc333b142
@ -6,8 +6,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"random-api-go/database"
|
||||||
"random-api-go/monitoring"
|
"random-api-go/monitoring"
|
||||||
"random-api-go/router"
|
|
||||||
"random-api-go/services"
|
"random-api-go/services"
|
||||||
"random-api-go/stats"
|
"random-api-go/stats"
|
||||||
"random-api-go/utils"
|
"random-api-go/utils"
|
||||||
@ -270,15 +270,25 @@ func (h *Handlers) HandleMetrics(w http.ResponseWriter, r *http.Request) {
|
|||||||
json.NewEncoder(w).Encode(metrics)
|
json.NewEncoder(w).Encode(metrics)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) Setup(r *router.Router) {
|
// HandlePublicHomeConfig 处理公开的首页配置请求
|
||||||
// 通用路由处理 - 匹配所有路径
|
func (h *Handlers) HandlePublicHomeConfig(w http.ResponseWriter, r *http.Request) {
|
||||||
r.HandleFunc("/", h.HandleAPIRequest)
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
// API 统计和监控
|
return
|
||||||
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 := handlers.NewHandlers(a.Stats)
|
handlers := handlers.NewHandlers(a.Stats)
|
||||||
|
|
||||||
// 设置路由
|
// 统一设置所有路由
|
||||||
a.router.Setup(handlers)
|
a.router.SetupAllRoutes(handlers, a.adminHandler, a.staticHandler)
|
||||||
a.router.SetupAdminRoutes(a.adminHandler)
|
|
||||||
|
|
||||||
// 设置静态文件路由(如果静态文件处理器存在)
|
|
||||||
if a.staticHandler != nil {
|
|
||||||
a.router.SetupStaticRoutes(a.staticHandler)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 创建 HTTP 服务器
|
// 创建 HTTP 服务器
|
||||||
cfg := config.Get().Server
|
cfg := config.Get().Server
|
||||||
|
@ -14,7 +14,16 @@ type Router struct {
|
|||||||
|
|
||||||
// Handler 接口定义处理器需要的方法
|
// Handler 接口定义处理器需要的方法
|
||||||
type Handler interface {
|
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 接口定义静态文件处理器需要的方法
|
// StaticHandler 接口定义静态文件处理器需要的方法
|
||||||
@ -62,20 +71,30 @@ func New() *Router {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) Setup(h Handler) {
|
// SetupAllRoutes 统一设置所有路由
|
||||||
// 设置API路由
|
func (r *Router) SetupAllRoutes(handler Handler, adminHandler AdminHandler, staticHandler StaticHandler) {
|
||||||
h.Setup(r)
|
// 设置公开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)
|
||||||
|
|
||||||
// SetupStaticRoutes 设置静态文件路由
|
// 设置公开的OAuth配置路由
|
||||||
func (r *Router) SetupStaticRoutes(staticHandler StaticHandler) {
|
r.HandleFunc("/api/oauth-config", adminHandler.GetOAuthConfig)
|
||||||
|
|
||||||
|
// 设置管理后台路由
|
||||||
|
r.setupAdminRoutes(adminHandler)
|
||||||
|
|
||||||
|
// 设置静态文件路由
|
||||||
|
if staticHandler != nil {
|
||||||
r.staticHandler = staticHandler
|
r.staticHandler = staticHandler
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SetupAdminRoutes 设置管理后台路由
|
// setupAdminRoutes 设置管理后台路由(私有方法)
|
||||||
func (r *Router) SetupAdminRoutes(adminHandler AdminHandler) {
|
func (r *Router) setupAdminRoutes(adminHandler AdminHandler) {
|
||||||
// OAuth配置API(前端需要获取client_id等信息)- 不需要认证
|
|
||||||
r.HandleFunc("/api/admin/oauth-config", adminHandler.GetOAuthConfig)
|
|
||||||
// OAuth令牌验证API(保留,以防需要)- 不需要认证
|
// OAuth令牌验证API(保留,以防需要)- 不需要认证
|
||||||
r.HandleFunc("/api/admin/oauth-verify", adminHandler.VerifyOAuthToken)
|
r.HandleFunc("/api/admin/oauth-verify", adminHandler.VerifyOAuthToken)
|
||||||
// OAuth回调处理(使用API前缀以便区分前后端)- 不需要认证
|
// OAuth回调处理(使用API前缀以便区分前后端)- 不需要认证
|
||||||
|
@ -33,7 +33,7 @@ interface SystemMetrics {
|
|||||||
|
|
||||||
async function getHomePageConfig() {
|
async function getHomePageConfig() {
|
||||||
try {
|
try {
|
||||||
const res = await apiFetch('/api/admin/home-config')
|
const res = await apiFetch('/api/home-config')
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
throw new Error('Failed to fetch home page config')
|
throw new Error('Failed to fetch home page config')
|
||||||
}
|
}
|
||||||
|
@ -83,7 +83,7 @@ export default function LoginPage({ onLoginSuccess }: LoginPageProps) {
|
|||||||
const loadOAuthConfig = async () => {
|
const loadOAuthConfig = async () => {
|
||||||
try {
|
try {
|
||||||
console.log('Loading OAuth config...')
|
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)
|
console.log('OAuth config response status:', response.status)
|
||||||
|
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user