licensing-cotton/internal/handlers/routes.go
2025-11-01 15:19:24 +08:00

56 lines
1.9 KiB
Go

package handlers
import (
"net/http"
"path/filepath"
"strings"
)
func RegisterRoutes() *http.ServeMux {
mux := http.NewServeMux()
// Serve static files for frontend
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" || r.URL.Path == "" {
http.ServeFile(w, r, "web/index.html")
} else if strings.HasPrefix(r.URL.Path, "/static/") {
// Serve CSS, JS, images
http.ServeFile(w, r, filepath.Join("web", r.URL.Path))
} else {
http.NotFound(w, r)
}
})
// Auth - support both old and new routes for compatibility
mux.HandleFunc("/api/login", HandleLogin)
mux.HandleFunc("/login", HandleLogin)
mux.HandleFunc("/api/register", HandleRegisterUser)
mux.HandleFunc("/register", HandleRegisterUser)
// Device
mux.HandleFunc("/api/device/manage", HandleCreateOrUpdateDevice)
mux.HandleFunc("/device/manage", HandleCreateOrUpdateDevice)
mux.HandleFunc("/api/device/list", HandleListDevices)
mux.HandleFunc("/device/list", HandleListDevices)
// License
mux.HandleFunc("/api/license/sign", HandleSignLicense)
mux.HandleFunc("/license/sign", HandleSignLicense)
mux.HandleFunc("/api/license/verify", HandleVerifyLicense)
mux.HandleFunc("/license/verify", HandleVerifyLicense)
mux.HandleFunc("/api/license/renew", HandleRenewLicense)
mux.HandleFunc("/license/renew", HandleRenewLicense)
mux.HandleFunc("/api/license/public-key", HandleGetPublicKey)
mux.HandleFunc("/license/public-key", HandleGetPublicKey)
// Admin
mux.HandleFunc("/api/admin/pending_requests", HandleListPendingRequests)
mux.HandleFunc("/admin/pending_requests", HandleListPendingRequests)
mux.HandleFunc("/api/admin/handle_request", HandleDeviceRequest)
mux.HandleFunc("/admin/handle_request", HandleDeviceRequest)
mux.HandleFunc("/api/admin/allow_auto_renew", SetAutoRenewAllDevices)
mux.HandleFunc("/admin/allow_auto_renew", SetAutoRenewAllDevices)
return mux
}