matrix-prometheus/internal/config/env.go

80 lines
1.5 KiB
Go

package config
import (
"log"
"os"
"strings"
"git.luj0ga.de/franconian/matrix"
)
const (
defaultGreeting = "Hello! I'm matrix-prometheus, a bot that receives webhooks from a Prometheus Alertmanager and broadcasts them to configured rooms."
defaultListen = ":8000"
defaultLogLevel = 2 // warning
logError = "error"
logWarning = "warning"
logDebug = "debug"
logTrace = "trace"
)
func FromEnv() Config {
db := requireEnv("DATABASE")
deviceName := requireEnv("DEVICE_NAME")
homeserver := requireEnv("HOMESERVER")
userId := requireEnv("USER_ID")
password := requireEnv("PASSWORD")
pickleKey := requireEnv("PICKLE_KEY")
allowedRooms := os.Getenv("ALLOWED_ROOMS")
greeting := os.Getenv("GREETING")
if greeting == "" {
greeting = defaultGreeting
}
listen := os.Getenv("LISTEN")
if listen == "" {
listen = defaultListen
}
var logLevel uint
switch os.Getenv("LOG_LEVEL") {
case logError:
logLevel = 1
case logWarning:
logLevel = 2
case logDebug:
logLevel = 3
case logTrace:
logLevel = 4
default:
logLevel = defaultLogLevel
}
return Config{
Db: db,
Listen: listen,
Matrix: matrix.Config{
AllowedRooms: strings.Split(allowedRooms, ","),
DeviceName: deviceName,
Greeting: greeting,
LogLevel: logLevel,
HomeserverURL: homeserver,
UserIdentifier: userId,
Password: password,
PickleKey: pickleKey,
},
}
}
func requireEnv(key string) string {
value, ok := os.LookupEnv(key)
if !ok {
log.Fatal("required environment variable ", key, " is missing")
}
return value
}