2021-09-22 00:47:51 -04:00
package ui
import (
"bytes"
2022-04-25 20:20:32 -04:00
"errors"
2021-09-22 00:47:51 -04:00
"html/template"
2022-10-09 21:33:31 -04:00
2025-05-17 16:10:28 -04:00
"github.com/TwiN/gatus/v5/storage"
2022-12-06 01:41:09 -05:00
static "github.com/TwiN/gatus/v5/web"
2021-09-22 00:47:51 -04:00
)
const (
2025-11-16 21:33:26 +01:00
defaultTitle = "Health Dashboard | Gatus"
defaultDescription = "Gatus is an advanced automated status page that lets you monitor your applications and configure alerts to notify you if there's an issue"
defaultHeader = "Gatus"
defaultDashboardHeading = "Health Dashboard"
defaultDashboardSubheading = "Monitor the health of your endpoints in real-time"
defaultLogo = ""
defaultLink = ""
2026-01-16 23:27:39 +01:00
defaultFavicon = "/favicon.ico"
defaultFavicon16 = "/favicon-16x16.png"
defaultFavicon32 = "/favicon-32x32.png"
2025-11-16 21:33:26 +01:00
defaultCustomCSS = ""
defaultSortBy = "name"
defaultFilterBy = "none"
2026-04-02 02:56:47 +02:00
defaultLoginSubtitle = "System Monitoring Dashboard"
2021-09-22 00:47:51 -04:00
)
var (
2025-03-08 04:32:05 +03:00
defaultDarkMode = true
2022-04-25 20:20:32 -04:00
ErrButtonValidationFailed = errors . New ( "invalid button configuration: missing required name or link" )
2025-08-14 09:15:34 -04:00
ErrInvalidDefaultSortBy = errors . New ( "invalid default-sort-by value: must be 'name', 'group', or 'health'" )
2025-08-14 16:44:17 -04:00
ErrInvalidDefaultFilterBy = errors . New ( "invalid default-filter-by value: must be 'none', 'failing', or 'unstable'" )
2021-09-22 00:47:51 -04:00
)
// Config is the configuration for the UI of Gatus
type Config struct {
2025-11-16 21:33:26 +01:00
Title string `yaml:"title,omitempty"` // Title of the page
Description string `yaml:"description,omitempty"` // Meta description of the page
DashboardHeading string `yaml:"dashboard-heading,omitempty"` // Dashboard Title between header and endpoints
DashboardSubheading string `yaml:"dashboard-subheading,omitempty"` // Dashboard Description between header and endpoints
Header string `yaml:"header,omitempty"` // Header is the text at the top of the page
Logo string `yaml:"logo,omitempty"` // Logo to display on the page
Link string `yaml:"link,omitempty"` // Link to open when clicking on the logo
2026-01-16 23:27:39 +01:00
Favicon Favicon `yaml:"favicon,omitempty"` // Favourite icon to display in web browser tab or address bar
2025-11-16 21:33:26 +01:00
Buttons [] Button `yaml:"buttons,omitempty"` // Buttons to display below the header
CustomCSS string `yaml:"custom-css,omitempty"` // Custom CSS to include in the page
DarkMode * bool `yaml:"dark-mode,omitempty"` // DarkMode is a flag to enable dark mode by default
DefaultSortBy string `yaml:"default-sort-by,omitempty"` // DefaultSortBy is the default sort option ('name', 'group', 'health')
DefaultFilterBy string `yaml:"default-filter-by,omitempty"` // DefaultFilterBy is the default filter option ('none', 'failing', 'unstable')
2026-04-02 02:56:47 +02:00
LoginSubtitle string `yaml:"login-subtitle,omitempty"` // LoginSubtitle is the subtitle displayed on the OIDC login page
2025-07-01 18:04:37 -04:00
//////////////////////////////////////////////
// Non-configurable - used for UI rendering //
//////////////////////////////////////////////
MaximumNumberOfResults int `yaml:"-"` // MaximumNumberOfResults to display on the page, it's not configurable because we're passing it from the storage config
2025-03-08 04:32:05 +03:00
}
func ( cfg * Config ) IsDarkMode () bool {
if cfg . DarkMode != nil {
return * cfg . DarkMode
}
return defaultDarkMode
2022-04-25 20:20:32 -04:00
}
// Button is the configuration for a button on the UI
type Button struct {
Name string `yaml:"name,omitempty"` // Name is the text to display on the button
Link string `yaml:"link,omitempty"` // Link to open when the button is clicked.
}
// Validate validates the button configuration
func ( btn * Button ) Validate () error {
if len ( btn . Name ) == 0 || len ( btn . Link ) == 0 {
return ErrButtonValidationFailed
}
return nil
2021-09-22 00:47:51 -04:00
}
2026-01-16 23:27:39 +01:00
type Favicon struct {
Default string `yaml:"default,omitempty"` // URL or path to default favourite icon.
Size16x16 string `yaml:"size16x16,omitempty"` // URL or path to favourite icon for 16x16 size.
Size32x32 string `yaml:"size32x32,omitempty"` // URL or path to favourite icon for 32x32 size.
}
2021-09-22 00:47:51 -04:00
// GetDefaultConfig returns a Config struct with the default values
func GetDefaultConfig () * Config {
return & Config {
2025-05-17 16:10:28 -04:00
Title : defaultTitle ,
Description : defaultDescription ,
2025-11-16 21:33:26 +01:00
DashboardHeading : defaultDashboardHeading ,
DashboardSubheading : defaultDashboardSubheading ,
2025-05-17 16:10:28 -04:00
Header : defaultHeader ,
Logo : defaultLogo ,
Link : defaultLink ,
CustomCSS : defaultCustomCSS ,
DarkMode : & defaultDarkMode ,
2025-08-14 09:15:34 -04:00
DefaultSortBy : defaultSortBy ,
DefaultFilterBy : defaultFilterBy ,
2026-04-02 02:56:47 +02:00
LoginSubtitle : defaultLoginSubtitle ,
2025-05-17 16:10:28 -04:00
MaximumNumberOfResults : storage . DefaultMaximumNumberOfResults ,
2026-01-16 23:27:39 +01:00
Favicon : Favicon {
Default : defaultFavicon ,
Size16x16 : defaultFavicon16 ,
Size32x32 : defaultFavicon32 ,
},
2021-09-22 00:47:51 -04:00
}
}
// ValidateAndSetDefaults validates the UI configuration and sets the default values if necessary.
func ( cfg * Config ) ValidateAndSetDefaults () error {
if len ( cfg . Title ) == 0 {
cfg . Title = defaultTitle
}
2022-11-01 05:33:19 +01:00
if len ( cfg . Description ) == 0 {
cfg . Description = defaultDescription
}
2025-11-16 21:33:26 +01:00
if len ( cfg . DashboardHeading ) == 0 {
cfg . DashboardHeading = defaultDashboardHeading
}
if len ( cfg . DashboardSubheading ) == 0 {
cfg . DashboardSubheading = defaultDashboardSubheading
}
2022-01-08 14:55:40 -05:00
if len ( cfg . Header ) == 0 {
cfg . Header = defaultHeader
}
2024-12-26 23:08:16 -05:00
if len ( cfg . Logo ) == 0 {
cfg . Logo = defaultLogo
}
if len ( cfg . Link ) == 0 {
cfg . Link = defaultLink
}
if len ( cfg . CustomCSS ) == 0 {
cfg . CustomCSS = defaultCustomCSS
2022-01-08 14:55:40 -05:00
}
2025-03-08 04:32:05 +03:00
if cfg . DarkMode == nil {
cfg . DarkMode = & defaultDarkMode
}
2025-08-14 09:15:34 -04:00
if len ( cfg . DefaultSortBy ) == 0 {
cfg . DefaultSortBy = defaultSortBy
} else if cfg . DefaultSortBy != "name" && cfg . DefaultSortBy != "group" && cfg . DefaultSortBy != "health" {
return ErrInvalidDefaultSortBy
}
if len ( cfg . DefaultFilterBy ) == 0 {
cfg . DefaultFilterBy = defaultFilterBy
2025-08-14 16:44:17 -04:00
} else if cfg . DefaultFilterBy != "none" && cfg . DefaultFilterBy != "failing" && cfg . DefaultFilterBy != "unstable" {
2025-08-14 09:15:34 -04:00
return ErrInvalidDefaultFilterBy
}
2026-04-02 02:56:47 +02:00
if len ( cfg . LoginSubtitle ) == 0 {
cfg . LoginSubtitle = defaultLoginSubtitle
}
2026-01-16 23:27:39 +01:00
if len ( cfg . Favicon . Default ) == 0 {
cfg . Favicon . Default = defaultFavicon
}
if len ( cfg . Favicon . Size16x16 ) == 0 {
cfg . Favicon . Size16x16 = defaultFavicon16
}
if len ( cfg . Favicon . Size32x32 ) == 0 {
cfg . Favicon . Size32x32 = defaultFavicon32
}
2022-04-25 20:20:32 -04:00
for _ , btn := range cfg . Buttons {
if err := btn . Validate (); err != nil {
return err
}
}
// Validate that the template works
2022-10-09 21:33:31 -04:00
t , err := template . ParseFS ( static . FileSystem , static . IndexPath )
2021-09-22 00:47:51 -04:00
if err != nil {
return err
}
var buffer bytes . Buffer
2025-03-08 04:32:05 +03:00
return t . Execute ( & buffer , ViewData { UI : cfg , Theme : "dark" })
}
type ViewData struct {
UI * Config
Theme string
2021-09-22 00:47:51 -04:00
}