burning.moe/internal/config/config.go
2024-01-30 10:58:25 -08:00

80 lines
2.2 KiB
Go

package config
import (
"os"
"time"
"github.com/charmbracelet/log"
"github.com/ilyakaznacheev/cleanenv"
"git.burning.moe/celediel/burning.moe/internal/models"
)
// AppConfig contains data to be accessed across the app.
type AppConfig struct {
CacheTimer time.Duration
ListenPort uint16
Logger *log.Logger
LogLevel log.Level
TemplateCache models.TemplateCache
UseCache bool
}
// defaluts contains default settings that are used if no environmental variables are set
var defaults = &AppConfig{
CacheTimer: time.Hour * 12,
ListenPort: 9001,
LogLevel: log.InfoLevel,
UseCache: true,
}
// ConfigDatabase contains data to be loaded from environmental variables
type ConfigDatabase struct {
CacheTimer time.Duration `env:"CACHETIMER" env-description:"How often to automatically regenerate template cache."`
LogLevel string `env:"LOGLEVEL" env-description:"Logging level. Default: warn, Possible values: debug info warn error fatal none"`
Port uint16 `env:"PORT" env-description:"server port"`
UseCache bool `env:"CACHE" env-description:"Use template cache"`
}
// Initialises the app wide AppConfig, loads values from environment, and set up the Logger
func Initialise() AppConfig {
app := *defaults
// Setup logger
app.Logger = log.NewWithOptions(os.Stderr, log.Options{
ReportTimestamp: true,
TimeFormat: time.TimeOnly,
})
// load values from config
if cfg, err := loadConfig(); err == nil {
app.ListenPort = cfg.Port
app.UseCache = cfg.UseCache
app.CacheTimer = cfg.CacheTimer
app.LogLevel, err = log.ParseLevel(cfg.LogLevel)
if err != nil {
app.LogLevel = defaults.LogLevel
}
} else {
app.Logger.Print("Failed loading config from environment", "err", err)
}
app.Logger.SetLevel(app.LogLevel)
app.Logger.Debug("Loaded config from environment:",
"port", app.ListenPort, "useCache", app.UseCache,
"log_level", app.LogLevel, "cacheTimer", app.CacheTimer,
)
return app
}
// loadConfig utilises cleanenv to load config values from the environment
func loadConfig() (ConfigDatabase, error) {
var cfg ConfigDatabase
if err := cleanenv.ReadEnv(&cfg); err != nil {
return ConfigDatabase{}, err
} else {
return cfg, nil
}
}