2021-09-12 18:39:09 -04:00
package handler
import (
"net/http"
2021-10-07 21:28:04 -04:00
"github.com/TwiN/gatus/v3/config/ui"
"github.com/TwiN/gatus/v3/security"
"github.com/TwiN/health"
2021-09-12 18:39:09 -04:00
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
2021-09-22 00:47:51 -04:00
func CreateRouter ( staticFolder string , securityConfig * security . Config , uiConfig * ui . Config , enabledMetrics bool ) * mux . Router {
2021-09-12 18:39:09 -04:00
router := mux . NewRouter ()
if enabledMetrics {
router . Handle ( "/metrics" , promhttp . Handler ()). Methods ( "GET" )
}
2021-12-31 00:10:54 -05:00
api := router . PathPrefix ( "/api" ). Subrouter ()
protected := api . PathPrefix ( "/" ). Subrouter ()
unprotected := api . PathPrefix ( "/" ). Subrouter ()
2021-12-14 23:20:43 -05:00
if securityConfig != nil {
if err := securityConfig . RegisterHandlers ( router ); err != nil {
panic ( err )
}
2022-01-08 19:52:11 -05:00
if err := securityConfig . ApplySecurityMiddleware ( protected ); err != nil {
panic ( err )
}
2021-12-14 23:20:43 -05:00
}
2021-09-12 18:39:09 -04:00
// Endpoints
2022-01-02 18:29:34 -05:00
unprotected . Handle ( "/v1/config" , ConfigHandler { securityConfig : securityConfig }). Methods ( "GET" )
2021-12-31 00:10:54 -05:00
protected . HandleFunc ( "/v1/endpoints/statuses" , EndpointStatuses ). Methods ( "GET" ) // No GzipHandler for this one, because we cache the content as Gzipped already
protected . HandleFunc ( "/v1/endpoints/{key}/statuses" , GzipHandlerFunc ( EndpointStatus )). Methods ( "GET" )
unprotected . HandleFunc ( "/v1/endpoints/{key}/uptimes/{duration}/badge.svg" , UptimeBadge ). Methods ( "GET" )
unprotected . HandleFunc ( "/v1/endpoints/{key}/response-times/{duration}/badge.svg" , ResponseTimeBadge ). Methods ( "GET" )
unprotected . HandleFunc ( "/v1/endpoints/{key}/response-times/{duration}/chart.svg" , ResponseTimeChart ). Methods ( "GET" )
2021-10-23 16:47:12 -04:00
// XXX: Remove the lines between this and the next XXX comment in v4.0.0
2021-12-31 00:10:54 -05:00
protected . HandleFunc ( "/v1/services/statuses" , EndpointStatuses ). Methods ( "GET" ) // No GzipHandler for this one, because we cache the content as Gzipped already
protected . HandleFunc ( "/v1/services/{key}/statuses" , GzipHandlerFunc ( EndpointStatus )). Methods ( "GET" )
unprotected . HandleFunc ( "/v1/services/{key}/uptimes/{duration}/badge.svg" , UptimeBadge ). Methods ( "GET" )
unprotected . HandleFunc ( "/v1/services/{key}/response-times/{duration}/badge.svg" , ResponseTimeBadge ). Methods ( "GET" )
unprotected . HandleFunc ( "/v1/services/{key}/response-times/{duration}/chart.svg" , ResponseTimeChart ). Methods ( "GET" )
2021-10-23 16:47:12 -04:00
// XXX: Remove the lines between this and the previous XXX comment in v4.0.0
2021-12-31 00:10:54 -05:00
// Misc
router . Handle ( "/health" , health . Handler (). WithJSON ( true )). Methods ( "GET" )
router . HandleFunc ( "/favicon.ico" , FavIcon ( staticFolder )). Methods ( "GET" )
2021-09-12 18:39:09 -04:00
// SPA
2021-10-23 16:47:12 -04:00
router . HandleFunc ( "/services/{name}" , SinglePageApplication ( staticFolder , uiConfig )). Methods ( "GET" ) // XXX: Remove this in v4.0.0
router . HandleFunc ( "/endpoints/{name}" , SinglePageApplication ( staticFolder , uiConfig )). Methods ( "GET" )
2021-09-12 18:39:09 -04:00
router . HandleFunc ( "/" , SinglePageApplication ( staticFolder , uiConfig )). Methods ( "GET" )
// Everything else falls back on static content
router . PathPrefix ( "/" ). Handler ( GzipHandler ( http . FileServer ( http . Dir ( staticFolder ))))
return router
}