88 lines
3.3 KiB
Go
88 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/bams/backend/internal/logger"
|
|
"github.com/bams/backend/internal/services"
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
func NewRouter(sm *services.ServiceManager, log *logger.Logger) *mux.Router {
|
|
router := mux.NewRouter()
|
|
|
|
// Middleware
|
|
router.Use(loggingMiddleware(log))
|
|
router.Use(corsMiddleware())
|
|
|
|
// API v1 routes
|
|
v1 := router.PathPrefix("/api/v1").Subrouter()
|
|
|
|
// Dashboard
|
|
v1.HandleFunc("/dashboard", handleDashboard(sm)).Methods("GET")
|
|
|
|
// Disk repository management
|
|
v1.HandleFunc("/disk/repositories", handleListRepositories(sm)).Methods("GET")
|
|
v1.HandleFunc("/disk/repositories", handleCreateRepository(sm)).Methods("POST")
|
|
v1.HandleFunc("/disk/repositories/{id}", handleGetRepository(sm)).Methods("GET")
|
|
v1.HandleFunc("/disk/repositories/{id}", handleDeleteRepository(sm)).Methods("DELETE")
|
|
|
|
// Tape library management
|
|
v1.HandleFunc("/tape/library", handleGetLibrary(sm)).Methods("GET")
|
|
v1.HandleFunc("/tape/inventory", handleInventory(sm)).Methods("POST")
|
|
v1.HandleFunc("/tape/drives", handleListDrives(sm)).Methods("GET")
|
|
v1.HandleFunc("/tape/drives/{id}/load", handleLoadTape(sm)).Methods("POST")
|
|
v1.HandleFunc("/tape/drives/{id}/unload", handleUnloadTape(sm)).Methods("POST")
|
|
v1.HandleFunc("/tape/slots", handleListSlots(sm)).Methods("GET")
|
|
|
|
// iSCSI target management
|
|
v1.HandleFunc("/iscsi/targets", handleListTargets(sm)).Methods("GET")
|
|
v1.HandleFunc("/iscsi/targets", handleCreateTarget(sm)).Methods("POST")
|
|
v1.HandleFunc("/iscsi/targets/{id}", handleGetTarget(sm)).Methods("GET")
|
|
v1.HandleFunc("/iscsi/targets/{id}", handleUpdateTarget(sm)).Methods("PUT")
|
|
v1.HandleFunc("/iscsi/targets/{id}", handleDeleteTarget(sm)).Methods("DELETE")
|
|
v1.HandleFunc("/iscsi/targets/{id}/apply", handleApplyTarget(sm)).Methods("POST")
|
|
v1.HandleFunc("/iscsi/sessions", handleListSessions(sm)).Methods("GET")
|
|
|
|
// Bacula integration
|
|
v1.HandleFunc("/bacula/status", handleBaculaStatus(sm)).Methods("GET")
|
|
v1.HandleFunc("/bacula/config", handleGetBaculaConfig(sm)).Methods("GET")
|
|
v1.HandleFunc("/bacula/config", handleGenerateBaculaConfig(sm)).Methods("POST")
|
|
v1.HandleFunc("/bacula/inventory", handleBaculaInventory(sm)).Methods("POST")
|
|
v1.HandleFunc("/bacula/restart", handleBaculaRestart(sm)).Methods("POST")
|
|
|
|
// Logs and diagnostics
|
|
v1.HandleFunc("/logs/{service}", handleGetLogs(sm)).Methods("GET")
|
|
v1.HandleFunc("/logs/{service}/stream", handleStreamLogs(sm)).Methods("GET")
|
|
v1.HandleFunc("/diagnostics/bundle", handleDownloadBundle(sm)).Methods("GET")
|
|
|
|
// Health check
|
|
router.HandleFunc("/health", handleHealth()).Methods("GET")
|
|
|
|
return router
|
|
}
|
|
|
|
func loggingMiddleware(log *logger.Logger) mux.MiddlewareFunc {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
log.Info("HTTP request", "method", r.Method, "path", r.URL.Path)
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|
|
|
|
func corsMiddleware() mux.MiddlewareFunc {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
|
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
|
if r.Method == "OPTIONS" {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|