@@ -2,6 +2,7 @@ package api
22
33import (
44 "net/http"
5+ "strings"
56
67 "github.com/lich0821/ccNexus/internal/config"
78 "github.com/lich0821/ccNexus/internal/proxy"
@@ -13,6 +14,7 @@ type Handler struct {
1314 config * config.Config
1415 proxy * proxy.Proxy
1516 storage * storage.SQLiteStorage
17+ auth AuthConfig
1618}
1719
1820// NewHandler creates a new API handler
@@ -21,31 +23,61 @@ func NewHandler(cfg *config.Config, p *proxy.Proxy, s *storage.SQLiteStorage) *H
2123 config : cfg ,
2224 proxy : p ,
2325 storage : s ,
26+ auth : AuthConfig {
27+ Enabled : cfg .BasicAuthEnabled ,
28+ Username : cfg .BasicAuthUsername ,
29+ Password : cfg .BasicAuthPassword ,
30+ },
2431 }
2532}
2633
27- // RegisterRoutes registers all API routes
28- func (h * Handler ) RegisterRoutes (mux * http.ServeMux ) {
29- // Endpoint management
30- mux .HandleFunc ("/api/endpoints" , h .handleEndpoints )
31- mux .HandleFunc ("/api/endpoints/" , h .handleEndpointByName )
32- mux .HandleFunc ("/api/endpoints/current" , h .handleCurrentEndpoint )
33- mux .HandleFunc ("/api/endpoints/switch" , h .handleSwitchEndpoint )
34- mux .HandleFunc ("/api/endpoints/reorder" , h .handleReorderEndpoints )
35- mux .HandleFunc ("/api/endpoints/fetch-models" , h .handleFetchModels )
36-
37- // Statistics
38- mux .HandleFunc ("/api/stats/summary" , h .handleStatsSummary )
39- mux .HandleFunc ("/api/stats/daily" , h .handleStatsDaily )
40- mux .HandleFunc ("/api/stats/weekly" , h .handleStatsWeekly )
41- mux .HandleFunc ("/api/stats/monthly" , h .handleStatsMonthly )
42- mux .HandleFunc ("/api/stats/trends" , h .handleStatsTrends )
34+ // ServeHTTP implements http.Handler interface
35+ func (h * Handler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
36+ path := r .URL .Path
37+ if ! strings .HasPrefix (path , "/" ) {
38+ path = "/" + path
39+ }
4340
44- // Configuration
45- mux .HandleFunc ("/api/config" , h .handleConfig )
46- mux .HandleFunc ("/api/config/port" , h .handleConfigPort )
47- mux .HandleFunc ("/api/config/log-level" , h .handleConfigLogLevel )
41+ authMiddleware := BasicAuthMiddleware (h .auth )
4842
49- // Real-time events
50- mux .HandleFunc ("/api/events" , h .handleEvents )
51- }
43+ switch path {
44+ case "/api/endpoints" :
45+ authMiddleware (http .HandlerFunc (h .handleEndpoints )).ServeHTTP (w , r )
46+ case "/api/endpoints/current" :
47+ authMiddleware (http .HandlerFunc (h .handleCurrentEndpoint )).ServeHTTP (w , r )
48+ case "/api/endpoints/switch" :
49+ authMiddleware (http .HandlerFunc (h .handleSwitchEndpoint )).ServeHTTP (w , r )
50+ case "/api/endpoints/reorder" :
51+ authMiddleware (http .HandlerFunc (h .handleReorderEndpoints )).ServeHTTP (w , r )
52+ case "/api/endpoints/fetch-models" :
53+ authMiddleware (http .HandlerFunc (h .handleFetchModels )).ServeHTTP (w , r )
54+ case "/api/stats/summary" :
55+ authMiddleware (http .HandlerFunc (h .handleStatsSummary )).ServeHTTP (w , r )
56+ case "/api/stats/daily" :
57+ authMiddleware (http .HandlerFunc (h .handleStatsDaily )).ServeHTTP (w , r )
58+ case "/api/stats/weekly" :
59+ authMiddleware (http .HandlerFunc (h .handleStatsWeekly )).ServeHTTP (w , r )
60+ case "/api/stats/monthly" :
61+ authMiddleware (http .HandlerFunc (h .handleStatsMonthly )).ServeHTTP (w , r )
62+ case "/api/stats/trends" :
63+ authMiddleware (http .HandlerFunc (h .handleStatsTrends )).ServeHTTP (w , r )
64+ case "/api/config" :
65+ authMiddleware (http .HandlerFunc (h .handleConfig )).ServeHTTP (w , r )
66+ case "/api/config/port" :
67+ authMiddleware (http .HandlerFunc (h .handleConfigPort )).ServeHTTP (w , r )
68+ case "/api/config/log-level" :
69+ authMiddleware (http .HandlerFunc (h .handleConfigLogLevel )).ServeHTTP (w , r )
70+ case "/api/config/basic-auth" :
71+ authMiddleware (http .HandlerFunc (h .handleBasicAuthConfig )).ServeHTTP (w , r )
72+ case "/api/config/basic-auth/reset-password" :
73+ authMiddleware (http .HandlerFunc (h .handleResetBasicAuthPassword )).ServeHTTP (w , r )
74+ case "/api/events" :
75+ authMiddleware (http .HandlerFunc (h .handleEvents )).ServeHTTP (w , r )
76+ default :
77+ if strings .HasPrefix (path , "/api/endpoints/" ) {
78+ authMiddleware (http .HandlerFunc (h .handleEndpointByName )).ServeHTTP (w , r )
79+ return
80+ }
81+ http .NotFound (w , r )
82+ }
83+ }
0 commit comments