Files
nyaKazuha 4ed81f0030
bat-rust / Build and test Rust (push) Canceled after 0s
bat-rust / Build and test Go API (push) Canceled after 0s
feat(bat-api): 实现内嵌 dashboard
Closes #46
2026-08-31 23:17:23 +08:00

38 lines
1.0 KiB
Go

package api
import (
"net/http"
"strings"
dashboard "bat-api/web"
)
const adminDashboardPath = "/admin/dashboard"
var adminDashboardFileServer = http.FileServer(http.FS(dashboard.Assets))
func (s *Server) handleAdminDashboard(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
writeErrorJSON(w, http.StatusMethodNotAllowed, "method_not_allowed", "method not allowed")
return
}
w.Header().Set("Cache-Control", "no-store")
if r.URL.Path == adminDashboardPath {
http.Redirect(w, r, adminDashboardPath+"/", http.StatusMovedPermanently)
return
}
if !strings.HasPrefix(r.URL.Path, adminDashboardPath+"/") {
http.NotFound(w, r)
return
}
http.StripPrefix(adminDashboardPath+"/", adminDashboardFileServer).ServeHTTP(w, r)
}
func dashboardAuthExemptPaths() []string {
return []string{adminDashboardPath, adminDashboardPath + "/"}
}
func isAdminDashboardPath(path string) bool {
return path == adminDashboardPath || strings.HasPrefix(path, adminDashboardPath+"/")
}