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"
|
2022-08-14 23:51:32 +02:00
|
|
|
"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 {
|
2022-08-14 23:51:32 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2022-08-14 23:51:32 +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
|
|
|
}
|
|
|
|
|
|
2022-08-21 22:48:18 +02:00
|
|
|
// TODO: Crash when any auth* param is empty
|
|
|
|
|
|
2022-08-13 23:33:50 +02:00
|
|
|
return &config
|
|
|
|
|
}
|