mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 08:31:55 +08:00
feat(routing): Enhance main handler with admin route management and debug logging
- Refactor routing logic to use a more modular handler approach - Add dedicated admin route handler with explicit path matching - Implement comprehensive debug logging for request handling - Simplify main handler by removing nested admin route logic - Add fallback NotFound handler for unmatched routes
This commit is contained in:
parent
6c7bc2bfb8
commit
ca79cc7dac
56
main.go
56
main.go
@ -45,6 +45,32 @@ func main() {
|
|||||||
matcher func(*http.Request) bool
|
matcher func(*http.Request) bool
|
||||||
handler http.Handler
|
handler http.Handler
|
||||||
}{
|
}{
|
||||||
|
// 管理路由处理器
|
||||||
|
{
|
||||||
|
matcher: func(r *http.Request) bool {
|
||||||
|
return strings.HasPrefix(r.URL.Path, "/admin/")
|
||||||
|
},
|
||||||
|
handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("[Debug] 处理管理路由: %s", r.URL.Path)
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/admin/login":
|
||||||
|
log.Printf("[Debug] 提供登录页面")
|
||||||
|
http.ServeFile(w, r, "web/templates/admin/login.html")
|
||||||
|
case "/admin/metrics":
|
||||||
|
proxyHandler.AuthMiddleware(proxyHandler.MetricsHandler)(w, r)
|
||||||
|
case "/admin/config":
|
||||||
|
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
||||||
|
case "/admin/config/get":
|
||||||
|
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
||||||
|
case "/admin/config/save":
|
||||||
|
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
||||||
|
case "/admin/auth":
|
||||||
|
proxyHandler.AuthHandler(w, r)
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
},
|
||||||
// Mirror代理处理器
|
// Mirror代理处理器
|
||||||
{
|
{
|
||||||
matcher: func(r *http.Request) bool {
|
matcher: func(r *http.Request) bool {
|
||||||
@ -75,36 +101,15 @@ func main() {
|
|||||||
|
|
||||||
// 创建主处理器
|
// 创建主处理器
|
||||||
mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
mainHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
log.Printf("[Debug] 收到请求: %s %s", r.Method, r.URL.Path)
|
||||||
|
|
||||||
// 处理静态文件
|
// 处理静态文件
|
||||||
if strings.HasPrefix(r.URL.Path, "/web/static/") {
|
if strings.HasPrefix(r.URL.Path, "/web/static/") {
|
||||||
|
log.Printf("[Debug] 处理静态文件: %s", r.URL.Path)
|
||||||
http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static"))).ServeHTTP(w, r)
|
http.StripPrefix("/web/static/", http.FileServer(http.Dir("web/static"))).ServeHTTP(w, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理管理路由
|
|
||||||
if strings.HasPrefix(r.URL.Path, "/admin/") {
|
|
||||||
switch r.URL.Path {
|
|
||||||
case "/admin/login":
|
|
||||||
http.ServeFile(w, r, "web/templates/admin/login.html")
|
|
||||||
return
|
|
||||||
case "/admin/metrics":
|
|
||||||
proxyHandler.AuthMiddleware(proxyHandler.MetricsHandler)(w, r)
|
|
||||||
return
|
|
||||||
case "/admin/config":
|
|
||||||
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
|
||||||
return
|
|
||||||
case "/admin/config/get":
|
|
||||||
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
|
||||||
return
|
|
||||||
case "/admin/config/save":
|
|
||||||
proxyHandler.AuthMiddleware(handler.NewConfigHandler(cfg).ServeHTTP)(w, r)
|
|
||||||
return
|
|
||||||
case "/admin/auth":
|
|
||||||
proxyHandler.AuthHandler(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 遍历所有处理器
|
// 遍历所有处理器
|
||||||
for _, h := range handlers {
|
for _, h := range handlers {
|
||||||
if h.matcher(r) {
|
if h.matcher(r) {
|
||||||
@ -112,6 +117,9 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Printf("[Debug] 未找到处理器: %s", r.URL.Path)
|
||||||
|
http.NotFound(w, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
// 添加压缩中间件
|
// 添加压缩中间件
|
||||||
|
Loading…
x
Reference in New Issue
Block a user