All checks were successful
continuous-integration/drone/push Build is passing
105 lines
4.0 KiB
Go
105 lines
4.0 KiB
Go
package router
|
|
|
|
import (
|
|
"io/fs"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/pprof"
|
|
"os"
|
|
"path/filepath"
|
|
"vctp/db"
|
|
"vctp/dist"
|
|
"vctp/internal/secrets"
|
|
"vctp/internal/settings"
|
|
"vctp/internal/vcenter"
|
|
"vctp/server/handler"
|
|
"vctp/server/middleware"
|
|
)
|
|
|
|
func New(logger *slog.Logger, database db.Database, buildTime string, sha1ver string, goVersion string, creds *vcenter.VcenterLogin, secret *secrets.Secrets, settings *settings.Settings) http.Handler {
|
|
h := &handler.Handler{
|
|
Logger: logger,
|
|
Database: database,
|
|
BuildTime: buildTime,
|
|
SHA1Ver: sha1ver,
|
|
GoVersion: goVersion,
|
|
VcCreds: creds,
|
|
Secret: secret,
|
|
Settings: settings,
|
|
}
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
reportsDir := settings.Values.Settings.ReportsDir
|
|
if reportsDir == "" {
|
|
reportsDir = "/var/lib/vctp/reports"
|
|
}
|
|
if err := os.MkdirAll(reportsDir, 0o755); err != nil {
|
|
logger.Warn("failed to create reports directory", "error", err, "path", reportsDir)
|
|
}
|
|
|
|
mux.Handle("/assets/", middleware.CacheMiddleware(http.FileServer(http.FS(dist.AssetsDir))))
|
|
mux.Handle("/favicon.ico", middleware.CacheMiddleware(http.FileServer(http.FS(dist.AssetsDir))))
|
|
mux.Handle("/favicon-16x16.png", middleware.CacheMiddleware(http.FileServer(http.FS(dist.AssetsDir))))
|
|
mux.Handle("/favicon-32x32.png", middleware.CacheMiddleware(http.FileServer(http.FS(dist.AssetsDir))))
|
|
mux.Handle("/reports/", http.StripPrefix("/reports/", http.FileServer(http.Dir(filepath.Clean(reportsDir)))))
|
|
mux.HandleFunc("/", h.Home)
|
|
mux.HandleFunc("/api/event/vm/create", h.VmCreateEvent)
|
|
mux.HandleFunc("/api/event/vm/modify", h.VmModifyEvent)
|
|
mux.HandleFunc("/api/event/vm/move", h.VmMoveEvent)
|
|
mux.HandleFunc("/api/event/vm/delete", h.VmDeleteEvent)
|
|
mux.HandleFunc("/api/import/vm", h.VmImport)
|
|
// Use this when we need to manually remove a VM from the database to clean up
|
|
mux.HandleFunc("/api/inventory/vm/delete", h.VmCleanup)
|
|
|
|
// add missing data to VMs
|
|
//mux.HandleFunc("/api/inventory/vm/update", h.VmUpdateDetails)
|
|
|
|
// temporary endpoint
|
|
mux.HandleFunc("/api/cleanup/updates", h.UpdateCleanup)
|
|
//mux.HandleFunc("/api/cleanup/vcenter", h.VcCleanup)
|
|
|
|
mux.HandleFunc("/api/report/inventory", h.InventoryReportDownload)
|
|
mux.HandleFunc("/api/report/updates", h.UpdateReportDownload)
|
|
mux.HandleFunc("/api/report/snapshot", h.SnapshotReportDownload)
|
|
mux.HandleFunc("/api/snapshots/aggregate", h.SnapshotAggregateForce)
|
|
mux.HandleFunc("/api/snapshots/hourly/force", h.SnapshotForceHourly)
|
|
mux.HandleFunc("/api/snapshots/migrate", h.SnapshotMigrate)
|
|
mux.HandleFunc("/api/snapshots/regenerate-hourly-reports", h.SnapshotRegenerateHourlyReports)
|
|
mux.HandleFunc("/vcenters", h.VcenterList)
|
|
mux.HandleFunc("/vcenters/totals", h.VcenterTotals)
|
|
mux.HandleFunc("/metrics", h.Metrics)
|
|
|
|
mux.HandleFunc("/snapshots/hourly", h.SnapshotHourlyList)
|
|
mux.HandleFunc("/snapshots/daily", h.SnapshotDailyList)
|
|
mux.HandleFunc("/snapshots/monthly", h.SnapshotMonthlyList)
|
|
|
|
// endpoint for encrypting vcenter credential
|
|
mux.HandleFunc("/api/encrypt", h.EncryptData)
|
|
|
|
// serve swagger related components from the embedded fs
|
|
swaggerSub, err := fs.Sub(swaggerUI, "swagger-ui-dist")
|
|
if err != nil {
|
|
logger.Error("failed to load swagger ui assets", "error", err)
|
|
} else {
|
|
mux.Handle("/swagger/", middleware.CacheMiddleware(http.StripPrefix("/swagger/", http.FileServer(http.FS(swaggerSub)))))
|
|
}
|
|
mux.HandleFunc("/swagger", func(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, "/swagger/", http.StatusPermanentRedirect)
|
|
})
|
|
mux.Handle("/swagger.json", middleware.CacheMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write(swaggerSpec)
|
|
})))
|
|
|
|
// Register pprof handlers
|
|
mux.HandleFunc("/debug/pprof/", pprof.Index)
|
|
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
|
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
|
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
|
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
|
|
|
|
return middleware.NewLoggingMiddleware(logger, mux)
|
|
}
|