add consts for environment and logs

This commit is contained in:
Pedro Pérez 2025-03-17 15:24:10 +01:00
parent 3cadf06599
commit bbbb4a28d3

View File

@ -66,6 +66,23 @@ var (
logLevel string
)
type Environment string
const (
EnvironmentTesting Environment = "testing"
EnvironmentDevelopment Environment = "development"
EnvironmentProduction Environment = "production"
)
type LogLevel string
const (
LogLevelDebug LogLevel = "debug"
LogLevelInfo LogLevel = "info"
LogLevelWarn LogLevel = "warn"
LogLevelError LogLevel = "error"
)
type Config struct {
// default ""
Name string
@ -77,10 +94,10 @@ type Config struct {
EnvDirectory string
// default "development"
EnvMode string
EnvMode Environment
// default "debug"
LogLevel string
LogLevel LogLevel
// default "UTC"
Timezone string
@ -177,12 +194,12 @@ func New(config ...Config) *App {
}
if cfg.EnvMode == "" && os.Getenv("ENV_MODE") != "" {
cfg.EnvMode = os.Getenv("ENV_MODE")
cfg.EnvMode = Environment(os.Getenv("ENV_MODE"))
}
if cfg.LogLevel == "" && os.Getenv("LOG_LEVEL") != "" {
cfg.LogLevel = os.Getenv("LOG_LEVEL")
logLevel = cfg.LogLevel
cfg.LogLevel = LogLevel(os.Getenv("LOG_LEVEL"))
logLevel = string(cfg.LogLevel)
}
if cfg.Timezone == "" && os.Getenv("TIMEZONE") != "" {
@ -306,11 +323,11 @@ func (a *App) Version() string {
return a.config.Version
}
func (a *App) EnvMode() string {
func (a *App) EnvMode() Environment {
return a.config.EnvMode
}
func (a *App) LogLevel() string {
func (a *App) LogLevel() LogLevel {
return a.config.LogLevel
}
@ -337,7 +354,7 @@ func (a *App) Timezone() string {
//
// cmd/database/migrations/*.sql
func (a *App) Migrate(database embed.FS) {
if a.config.DatabaseMigrate == false {
if !a.config.DatabaseMigrate {
slog.Info("migration disabled")
return
}