2023-07-08 20:37:41 -04:00
package api
import (
_ "embed"
"html/template"
"github.com/TwiN/gatus/v5/config/ui"
static "github.com/TwiN/gatus/v5/web"
2024-11-13 23:54:00 -05:00
"github.com/TwiN/logr"
2023-07-08 20:37:41 -04:00
"github.com/gofiber/fiber/v2"
)
2025-03-08 04:32:05 +03:00
func SinglePageApplication ( uiConfig * ui . Config ) fiber . Handler {
2023-07-08 20:37:41 -04:00
return func ( c * fiber . Ctx ) error {
2025-03-08 04:32:05 +03:00
vd := ui . ViewData { UI : uiConfig }
{
themeFromCookie := string ( c . Request (). Header . Cookie ( "theme" ))
if len ( themeFromCookie ) > 0 {
if themeFromCookie == "dark" {
vd . Theme = "dark"
}
} else if uiConfig . IsDarkMode () { // Since there's no theme cookie, we'll rely on ui.DarkMode
vd . Theme = "dark"
}
}
2023-07-08 20:37:41 -04:00
t , err := template . ParseFS ( static . FileSystem , static . IndexPath )
if err != nil {
// This should never happen, because ui.ValidateAndSetDefaults validates that the template works.
2024-11-13 23:54:00 -05:00
logr . Errorf ( "[api.SinglePageApplication] Failed to parse template. This should never happen, because the template is validated on start. Error: %s" , err . Error ())
2023-07-08 20:37:41 -04:00
return c . Status ( 500 ). SendString ( "Failed to parse template. This should never happen, because the template is validated on start." )
}
c . Set ( "Content-Type" , "text/html" )
2025-03-08 04:32:05 +03:00
err = t . Execute ( c , vd )
2023-07-08 20:37:41 -04:00
if err != nil {
// This should never happen, because ui.ValidateAndSetDefaults validates that the template works.
2024-11-13 23:54:00 -05:00
logr . Errorf ( "[api.SinglePageApplication] Failed to execute template. This should never happen, because the template is validated on start. Error: %s" , err . Error ())
2023-07-08 20:37:41 -04:00
return c . Status ( 500 ). SendString ( "Failed to parse template. This should never happen, because the template is validated on start." )
}
return c . SendStatus ( 200 )
}
}