matrix-prometheus/internal/config/env.go

84 lines
1.6 KiB
Go
Raw Normal View History

2023-01-13 21:24:39 +01:00
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 {
deviceName := requireEnv("DEVICE_NAME")
homeserver := requireEnv("HOMESERVER")
userId := requireEnv("USER_ID")
password := requireEnv("PASSWORD")
pickleKey := requireEnv("PICKLE_KEY")
allowedRooms := os.Getenv("ALLOWED_ROOMS")
db := os.Getenv("DATABASE")
if db == "" {
db = "/data/db.sqlite3"
}
2023-01-13 21:24:39 +01:00
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
}