2021-09-22 04:47:51 +00:00
package ui
import (
"bytes"
2022-04-26 00:20:32 +00:00
"errors"
2021-09-22 04:47:51 +00:00
"html/template"
2022-10-10 01:33:31 +00:00
2022-12-06 06:41:09 +00:00
static "github.com/TwiN/gatus/v5/web"
2021-09-22 04:47:51 +00:00
)
const (
2022-11-01 04:33:19 +00: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 = "Health Status"
defaultLogo = ""
defaultLink = ""
2021-09-22 04:47:51 +00:00
)
var (
2022-04-26 00:20:32 +00:00
ErrButtonValidationFailed = errors . New ( "invalid button configuration: missing required name or link" )
2021-09-22 04:47:51 +00:00
)
// Config is the configuration for the UI of Gatus
type Config struct {
2022-11-01 04:33:19 +00:00
Title string ` yaml:"title,omitempty" ` // Title of the page
Description string ` yaml:"description,omitempty" ` // Meta description of the page
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
Buttons [ ] Button ` yaml:"buttons,omitempty" ` // Buttons to display below the header
2022-04-26 00:20:32 +00: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 04:47:51 +00:00
}
// GetDefaultConfig returns a Config struct with the default values
func GetDefaultConfig ( ) * Config {
return & Config {
2022-11-01 04:33:19 +00:00
Title : defaultTitle ,
Description : defaultDescription ,
Header : defaultHeader ,
Logo : defaultLogo ,
Link : defaultLink ,
2021-09-22 04:47:51 +00: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 04:33:19 +00:00
if len ( cfg . Description ) == 0 {
cfg . Description = defaultDescription
}
2022-01-08 19:55:40 +00:00
if len ( cfg . Header ) == 0 {
cfg . Header = defaultHeader
}
if len ( cfg . Header ) == 0 {
cfg . Header = defaultLink
}
2022-04-26 00:20:32 +00:00
for _ , btn := range cfg . Buttons {
if err := btn . Validate ( ) ; err != nil {
return err
}
}
// Validate that the template works
2022-10-10 01:33:31 +00:00
t , err := template . ParseFS ( static . FileSystem , static . IndexPath )
2021-09-22 04:47:51 +00:00
if err != nil {
return err
}
var buffer bytes . Buffer
err = t . Execute ( & buffer , cfg )
if err != nil {
return err
}
return nil
}