54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
package api
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"proxyrotator/internal/config"
|
||
"proxyrotator/internal/importer"
|
||
"proxyrotator/internal/selector"
|
||
"proxyrotator/internal/store"
|
||
"proxyrotator/internal/tester"
|
||
)
|
||
|
||
// NewRouter 创建 HTTP 路由
|
||
func NewRouter(
|
||
store store.ProxyStore,
|
||
importer *importer.Importer,
|
||
tester *tester.HTTPTester,
|
||
selector *selector.Selector,
|
||
cfg *config.Config,
|
||
) http.Handler {
|
||
handlers := NewHandlers(store, importer, tester, selector, cfg)
|
||
|
||
mux := http.NewServeMux()
|
||
|
||
// 注册路由(Go 1.22+ 支持 METHOD /path 模式)
|
||
mux.HandleFunc("POST /v1/proxies/import/text", handlers.HandleImportText)
|
||
mux.HandleFunc("POST /v1/proxies/import/file", handlers.HandleImportFile)
|
||
mux.HandleFunc("POST /v1/proxies/test", handlers.HandleTest)
|
||
mux.HandleFunc("GET /v1/proxies/next", handlers.HandleNext)
|
||
mux.HandleFunc("POST /v1/proxies/report", handlers.HandleReport)
|
||
|
||
// CRUD 路由(注意:/stats 需在 /{id} 之前注册)
|
||
mux.HandleFunc("GET /v1/proxies/stats", handlers.HandleGetStats)
|
||
mux.HandleFunc("GET /v1/proxies", handlers.HandleListProxies)
|
||
mux.HandleFunc("GET /v1/proxies/{id}", handlers.HandleGetProxy)
|
||
mux.HandleFunc("DELETE /v1/proxies/{id}", handlers.HandleDeleteProxy)
|
||
mux.HandleFunc("DELETE /v1/proxies", handlers.HandleBulkDeleteProxies)
|
||
mux.HandleFunc("PATCH /v1/proxies/{id}", handlers.HandleUpdateProxy)
|
||
mux.HandleFunc("POST /v1/proxies/{id}/test", handlers.HandleTestSingleProxy)
|
||
|
||
// 健康检查
|
||
mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) {
|
||
w.WriteHeader(http.StatusOK)
|
||
w.Write([]byte(`{"status":"ok"}`))
|
||
})
|
||
|
||
// 应用中间件
|
||
var handler http.Handler = mux
|
||
handler = AuthMiddleware(handler, cfg.APIKey)
|
||
handler = LoggingMiddleware(handler)
|
||
|
||
return handler
|
||
}
|