More stuff

This commit is contained in:
Melody Becker 2024-09-15 15:18:05 +02:00
parent 71beed7eb3
commit 9a7e420c83
7 changed files with 63 additions and 33 deletions

View file

@ -48,12 +48,21 @@ type ConfigAdmin struct {
}
type ConfigStorage struct {
// Url to the postgres database. Must contain credentials and stuff
DatabaseUrl string `toml:"database_url"`
Host string `toml:"host"`
Username string `toml:"username"`
Password string `toml:"password"`
DbName string `toml:"db_name"`
Port int `toml:"port"`
SslMode *string `toml:"ssl_mode"`
TimeZone *string `toml:"time_zone"`
// Url to redis server. If empty, no redis is used
RedisUrl *string `toml:"redis_url,omitempty"`
// The maximum size of the in-memory cache in bytes
MaxInMemoryCacheSize int64 `toml:"max_in_memory_cache_size"`
// The time to live for in app in memory cache items, in seconds
MaxInMemoryCacheTTL int
// The time to live for items in redis, in seconds
MaxRedisCacheTTL *int
}
type ConfigMail struct {
@ -74,7 +83,7 @@ type Config struct {
Admin ConfigAdmin `toml:"admin"`
Webauthn ConfigWebAuthn `toml:"webauthn"`
Storage ConfigStorage `toml:"storage"`
Mail ConfigMail `toml:"mail"`
Mail ConfigMail `toml:"mail"`
}
var GlobalConfig Config
@ -102,9 +111,17 @@ var defaultConfig Config = Config{
HashingSecret: "some super secure secret that should never be changed or else password storage breaks",
},
Storage: ConfigStorage{
DatabaseUrl: "db.sqlite",
Host: "localhost",
Username: "linstrom",
Password: "linstrom",
DbName: "linstrom",
Port: 5432,
SslMode: other.IntoPointer("disable"),
TimeZone: other.IntoPointer("Europe/Berlin"),
RedisUrl: nil,
MaxInMemoryCacheSize: 1e6, // 1 Megabyte
MaxInMemoryCacheTTL: 5,
MaxRedisCacheTTL: nil,
},
}
@ -125,6 +142,25 @@ func (gc *ConfigGeneral) GetFullPublicUrl() string {
return str
}
func (sc *ConfigStorage) BuildPostgresDSN() string {
dsn := fmt.Sprintf(
"host=%s user=%s password=%s dbname=%s port=%d",
sc.Host,
sc.Username,
sc.Password,
sc.DbName,
sc.Port,
)
if sc.SslMode != nil {
dsn += fmt.Sprintf(" sslmode=%s", *sc.SslMode)
}
if sc.TimeZone != nil {
dsn += fmt.Sprintf(" TimeZone=%s", *sc.TimeZone)
}
return dsn
}
func WriteDefaultConfig(toFile string) error {
log.Trace().Caller().Send()
log.Info().Str("config-file", toFile).Msg("Writing default config to file")