Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 80 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package main

import (
"flag"
"fmt"
"os"
"os/signal"
"reflect"
"strconv"
"syscall"
"time"
Expand All @@ -21,7 +24,19 @@ const (
GatusLogLevelEnvVar = "GATUS_LOG_LEVEL"
)

var (
validateConfig = flag.Bool("validate", false, "Validate configuration file and exit")
configPath = flag.String("config", "", "Path to configuration file (overrides GATUS_CONFIG_PATH)")
)

func main() {
flag.Parse()

if *validateConfig {
validateConfigurationAndExit()
return
}

if delayInSeconds, _ := strconv.Atoi(os.Getenv("GATUS_DELAY_START_SECONDS")); delayInSeconds > 0 {
logr.Infof("Delaying start by %d seconds", delayInSeconds)
time.Sleep(time.Duration(delayInSeconds) * time.Second)
Expand All @@ -48,6 +63,70 @@ func main() {
logr.Info("Shutting down")
}

func validateConfigurationAndExit() {
configureLogging()

path := *configPath
if len(path) == 0 {
path = os.Getenv(GatusConfigPathEnvVar)
if len(path) == 0 {
path = os.Getenv(GatusConfigFileEnvVar)
}
}

if len(path) == 0 {
fmt.Fprintf(os.Stderr, "No configuration file specified\n")
fmt.Fprintf(os.Stderr, "Use -config flag or set GATUS_CONFIG_PATH environment variable\n")
os.Exit(1)
}

fmt.Printf("Validating configuration: %s\n", path)

cfg, err := config.LoadConfiguration(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Configuration validation failed: %s\n", err)
os.Exit(1)
}

fmt.Printf("Configuration validation passed!\n")
fmt.Printf(" - Endpoints: %d\n", len(cfg.Endpoints))

if cfg.ExternalEndpoints != nil && len(cfg.ExternalEndpoints) > 0 {
fmt.Printf(" - External endpoints: %d\n", len(cfg.ExternalEndpoints))
}

if cfg.Alerting != nil {
providers := countAlertingProviders(cfg.Alerting)
if providers > 0 {
fmt.Printf(" - Alerting providers: %d\n", providers)
}
}

if cfg.Storage != nil {
fmt.Printf(" - Storage: %s\n", cfg.Storage.Type)
}

fmt.Printf("Configuration is valid and ready to use\n")
os.Exit(0)
}

func countAlertingProviders(alertingConfig interface{}) int {
count := 0
v := reflect.ValueOf(alertingConfig)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}

for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
if field.Kind() == reflect.Ptr && !field.IsNil() {
count++
}
}

return count
}

func start(cfg *config.Config) {
go controller.Handle(cfg)
metrics.InitializePrometheusMetrics(cfg, nil)
Expand Down Expand Up @@ -197,4 +276,4 @@ func listenToConfigurationFileChanges(cfg *config.Config) {
return
}
}
}
}