Do not rely on environment variables
Signed-off-by: Shin'ya Minazuki <shinyoukai@laidback.moe>
This commit is contained in:
32
main.go
32
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
@@ -15,14 +16,19 @@ import (
|
||||
"github.com/TwiN/logr"
|
||||
)
|
||||
|
||||
const (
|
||||
GatusConfigPathEnvVar = "GATUS_CONFIG_PATH"
|
||||
GatusConfigFileEnvVar = "GATUS_CONFIG_FILE" // Deprecated in favor of GatusConfigPathEnvVar
|
||||
GatusLogLevelEnvVar = "GATUS_LOG_LEVEL"
|
||||
var (
|
||||
configPath string
|
||||
delay string
|
||||
logLevel string
|
||||
)
|
||||
|
||||
func main() {
|
||||
if delayInSeconds, _ := strconv.Atoi(os.Getenv("GATUS_DELAY_START_SECONDS")); delayInSeconds > 0 {
|
||||
flag.StringVar(&configPath, "config", "config.yaml", "Path to the configuration file")
|
||||
flag.StringVar(&delay, "delay", "0", "How many seconds to wait before monitoring begins")
|
||||
flag.StringVar(&logLevel, "loglevel", "INFO", "Verbosity of logs")
|
||||
flag.Parse()
|
||||
|
||||
if delayInSeconds, _ := strconv.Atoi(delay); delayInSeconds > 0 {
|
||||
logr.Infof("Delaying start by %d seconds", delayInSeconds)
|
||||
time.Sleep(time.Duration(delayInSeconds) * time.Second)
|
||||
}
|
||||
@@ -69,28 +75,20 @@ func save() {
|
||||
}
|
||||
|
||||
func configureLogging() {
|
||||
logLevelAsString := os.Getenv(GatusLogLevelEnvVar)
|
||||
if logLevel, err := logr.LevelFromString(logLevelAsString); err != nil {
|
||||
if loglevel, err := logr.LevelFromString(logLevel); err != nil {
|
||||
logr.SetThreshold(logr.LevelInfo)
|
||||
if len(logLevelAsString) == 0 {
|
||||
if len(loglevel) == 0 {
|
||||
logr.Infof("[main.configureLogging] Defaulting log level to %s", logr.LevelInfo)
|
||||
} else {
|
||||
logr.Warnf("[main.configureLogging] Invalid log level '%s', defaulting to %s", logLevelAsString, logr.LevelInfo)
|
||||
logr.Warnf("[main.configureLogging] Invalid log level '%s', defaulting to %s", logLevel, logr.LevelInfo)
|
||||
}
|
||||
} else {
|
||||
logr.SetThreshold(logLevel)
|
||||
logr.SetThreshold(loglevel)
|
||||
logr.Infof("[main.configureLogging] Log Level is set to %s", logr.GetThreshold())
|
||||
}
|
||||
}
|
||||
|
||||
func loadConfiguration() (*config.Config, error) {
|
||||
configPath := os.Getenv(GatusConfigPathEnvVar)
|
||||
// Backwards compatibility
|
||||
if len(configPath) == 0 {
|
||||
if configPath = os.Getenv(GatusConfigFileEnvVar); len(configPath) > 0 {
|
||||
logr.Warnf("WARNING: %s is deprecated. Please use %s instead.", GatusConfigFileEnvVar, GatusConfigPathEnvVar)
|
||||
}
|
||||
}
|
||||
return config.LoadConfiguration(configPath)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user