Files
gin-gonic-prepack/config/config.go
2025-10-13 21:16:20 +02:00

81 lines
1.7 KiB
Go

package config
import (
"io"
"log"
"os"
"github.com/BurntSushi/toml"
)
type DbConfig struct {
Dialect string `toml:"dialect"`
Username string `toml:"username"`
PasswordSecret string `toml:"password-secret"`
Url string `toml:"url"`
Port string `toml:"port"`
Name string `toml:"name"`
}
type ServerConfig struct {
Port string `toml:"port"`
}
type RateLimitConfig struct {
AuthedLimits string `toml:"authed"`
UnauthedLimits string `toml:"unauthed"`
}
type SecurityConfig struct {
AllowFreshAdminGeneration bool `toml:"gen-fresh-admin"`
AdminEmails []string `toml:"admin-emails"`
AdminHmacEnv string `toml:"admin-hmac-env"`
UserHmacEnv string `toml:"user-hmac-env"`
}
type StackConfiguration struct {
ConfigLoaded bool
Server ServerConfig `toml:"server"`
Db DbConfig `toml:"db"`
RateLimits RateLimitConfig `toml:"rate-limits"`
Security SecurityConfig `toml:"security"`
}
var Environment = os.Getenv("STACK_ENVIRONMENT")
var configInternal = StackConfiguration{}
func Config() StackConfiguration {
return configInternal
}
func GetConfigPath(filename string) string {
if Environment == "" {
Environment = "dev"
}
return "config/" + Environment + "/" + filename
}
func LoadConfig() {
file, err := os.Open(GetConfigPath("conf.toml"))
if err != nil {
panic(err)
}
defer file.Close()
b, err := io.ReadAll(file)
if err != nil {
panic(err)
}
err = toml.Unmarshal(b, &configInternal)
if err != nil {
panic(err)
}
configInternal.ConfigLoaded = true
log.Printf("Loaded Config for stack '%s':\n%+v\n", Environment, configInternal)
}