graphicek/server/config/config.go

55 lines
1.3 KiB
Go
Raw Normal View History

2022-08-13 23:33:50 +02:00
package config
import (
2024-04-01 10:33:20 +02:00
"fmt"
2022-08-13 23:33:50 +02:00
"os"
"strconv"
2022-08-13 23:33:50 +02:00
)
type Config struct {
2024-04-01 10:33:20 +02:00
Mode string
DatabaseUrl string
Port int
Ip string
AuthEnabled bool
AuthUsername string
AuthPassword string
AuthKey string
DataRetentionInDays int64
2022-08-13 23:33:50 +02:00
}
func LoadConfig() *Config {
port, err := strconv.Atoi(os.Getenv("PORT"))
2022-08-13 23:33:50 +02:00
if err != nil {
2024-04-01 10:33:20 +02:00
panic(fmt.Errorf("PORT must be an integer: %v", err))
}
dataRetentionInDaysStr := os.Getenv("DATA_RETENTION_IN_DAYS")
dataRetentionInDays := 0
if dataRetentionInDaysStr != "" {
dataRetentionInDays, err = strconv.Atoi(dataRetentionInDaysStr)
if err != nil {
panic(fmt.Errorf("DATA_RETENTION_IN_DAYS must be an integer: %v", err))
}
2022-08-13 23:33:50 +02:00
}
config := Config{
2024-04-01 10:33:20 +02:00
Mode: os.Getenv("GIN_MODE"),
DatabaseUrl: os.Getenv("DATABASE_URL"),
Port: port,
Ip: os.Getenv("BIND_IP"),
AuthEnabled: os.Getenv("AUTH_ENABLED") != "false",
AuthUsername: os.Getenv("AUTH_USERNAME"),
AuthPassword: os.Getenv("AUTH_PASSWORD"),
AuthKey: os.Getenv("AUTH_KEY"),
DataRetentionInDays: int64(dataRetentionInDays),
2022-08-13 23:33:50 +02:00
}
// TODO: Crash when any auth* param is empty
2022-08-13 23:33:50 +02:00
return &config
}