2021-09-12 18:39:09 -04:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
2022-10-09 21:33:31 -04:00
|
|
|
"io/fs"
|
2021-09-12 18:39:09 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
2022-12-06 01:41:09 -05:00
|
|
|
"github.com/TwiN/gatus/v5/config"
|
|
|
|
|
static "github.com/TwiN/gatus/v5/web"
|
2021-10-07 21:28:04 -04:00
|
|
|
"github.com/TwiN/health"
|
2021-09-12 18:39:09 -04:00
|
|
|
"github.com/gorilla/mux"
|
2023-01-19 23:06:38 -05:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-09-12 18:39:09 -04:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
|
)
|
|
|
|
|
|
2022-10-09 21:33:31 -04:00
|
|
|
func CreateRouter(cfg *config.Config) *mux.Router {
|
2021-09-12 18:39:09 -04:00
|
|
|
router := mux.NewRouter()
|
2022-07-28 20:07:53 -04:00
|
|
|
if cfg.Metrics {
|
2023-01-19 23:06:38 -05:00
|
|
|
router.Handle("/metrics", promhttp.InstrumentMetricHandler(prometheus.DefaultRegisterer, promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{
|
|
|
|
|
DisableCompression: true,
|
|
|
|
|
}))).Methods("GET")
|
2021-09-12 18:39:09 -04:00
|
|
|
}
|
2022-12-30 21:37:52 -05:00
|
|
|
router.Use(GzipHandler)
|
2021-12-31 00:10:54 -05:00
|
|
|
api := router.PathPrefix("/api").Subrouter()
|
|
|
|
|
protected := api.PathPrefix("/").Subrouter()
|
|
|
|
|
unprotected := api.PathPrefix("/").Subrouter()
|
2022-07-28 20:07:53 -04:00
|
|
|
if cfg.Security != nil {
|
|
|
|
|
if err := cfg.Security.RegisterHandlers(router); err != nil {
|
2021-12-14 23:20:43 -05:00
|
|
|
panic(err)
|
|
|
|
|
}
|
2022-07-28 20:07:53 -04:00
|
|
|
if err := cfg.Security.ApplySecurityMiddleware(protected); err != nil {
|
2022-01-08 19:52:11 -05:00
|
|
|
panic(err)
|
|
|
|
|
}
|
2021-12-14 23:20:43 -05:00
|
|
|
}
|
2021-09-12 18:39:09 -04:00
|
|
|
// Endpoints
|
2022-07-28 20:07:53 -04:00
|
|
|
unprotected.Handle("/v1/config", ConfigHandler{securityConfig: cfg.Security}).Methods("GET")
|
2022-12-30 21:37:52 -05:00
|
|
|
protected.HandleFunc("/v1/endpoints/statuses", EndpointStatuses(cfg)).Methods("GET")
|
|
|
|
|
protected.HandleFunc("/v1/endpoints/{key}/statuses", EndpointStatus).Methods("GET")
|
2022-06-20 22:59:45 +05:00
|
|
|
unprotected.HandleFunc("/v1/endpoints/{key}/health/badge.svg", HealthBadge).Methods("GET")
|
2021-12-31 00:10:54 -05:00
|
|
|
unprotected.HandleFunc("/v1/endpoints/{key}/uptimes/{duration}/badge.svg", UptimeBadge).Methods("GET")
|
2022-08-11 03:05:34 +02:00
|
|
|
unprotected.HandleFunc("/v1/endpoints/{key}/response-times/{duration}/badge.svg", ResponseTimeBadge(cfg)).Methods("GET")
|
2021-12-31 00:10:54 -05:00
|
|
|
unprotected.HandleFunc("/v1/endpoints/{key}/response-times/{duration}/chart.svg", ResponseTimeChart).Methods("GET")
|
|
|
|
|
// Misc
|
|
|
|
|
router.Handle("/health", health.Handler().WithJSON(true)).Methods("GET")
|
2021-09-12 18:39:09 -04:00
|
|
|
// SPA
|
2022-10-09 21:33:31 -04:00
|
|
|
router.HandleFunc("/endpoints/{name}", SinglePageApplication(cfg.UI)).Methods("GET")
|
|
|
|
|
router.HandleFunc("/", SinglePageApplication(cfg.UI)).Methods("GET")
|
2021-09-12 18:39:09 -04:00
|
|
|
// Everything else falls back on static content
|
2022-10-09 21:33:31 -04:00
|
|
|
staticFileSystem, err := fs.Sub(static.FileSystem, static.RootPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
2022-12-30 21:37:52 -05:00
|
|
|
router.PathPrefix("/").Handler(http.FileServer(http.FS(staticFileSystem)))
|
2021-09-12 18:39:09 -04:00
|
|
|
return router
|
|
|
|
|
}
|