diff --git a/app/app.go b/app/app.go index ae7f0ff..abdaf9d 100644 --- a/app/app.go +++ b/app/app.go @@ -23,46 +23,48 @@ import ( // TODO: review consts const ( // Handlers keys - InvalidRequest string = "invalid_request" - MalformedJSON string = "malformed_json" - TokenBlacklisted string = "token_blacklisted" - TokenInvalid string = "token_invalid" - ValidationFailed string = "validation_failed" - UntilBeforeTo string = "until_before_to" - InternalError string = "internal_error" - NotFound string = "not_found" - Created string = "created" - Updated string = "updated" - Deleted string = "deleted" - Enabled string = "enabled" - Disabled string = "disabled" - Retrieved string = "retrieved" - ErrorCreating string = "error_creating" - ErrorUpdating string = "error_updating" - ErrorEnabling string = "error_enabling" - ErrorDisabling string = "error_disabling" - ErrorGetting string = "error_getting" - ErrorGettingAll string = "error_getting_all" - ErrorMailing string = "error_mailing" - InvalidEntityID string = "invalid_entity_id" - NotImplemented string = "not_implemented" - NotPassValidation string = "not_pass_validation" - NotEnoughBalance string = "not_enough_balance" + InvalidRequest = "invalid_request" + MalformedJSON = "malformed_json" + TokenBlacklisted = "token_blacklisted" + TokenInvalid = "token_invalid" + ValidationFailed = "validation_failed" + UntilBeforeTo = "until_before_to" + InternalError = "internal_error" + NotFound = "not_found" + Created = "created" + Updated = "updated" + Deleted = "deleted" + Enabled = "enabled" + Disabled = "disabled" + Retrieved = "retrieved" + ErrorCreating = "error_creating" + ErrorUpdating = "error_updating" + ErrorEnabling = "error_enabling" + ErrorDisabling = "error_disabling" + ErrorGetting = "error_getting" + ErrorGettingAll = "error_getting_all" + ErrorMailing = "error_mailing" + InvalidEntityID = "invalid_entity_id" + NotImplemented = "not_implemented" + NotPassValidation = "not_pass_validation" + NotEnoughBalance = "not_enough_balance" + InvalidIdentifier = "invalid_identifier" // User keys (DB) - UserUsernameKey string = "username_key" - UserEmailKey string = "email_key" - UsernameAlreadyExists string = "username_already_exists" - UserSessionKey string = "user_session_key" - EmailAlreadyExists string = "email_already_exists" - PhoneNumberKey string = "phone_number_key" - PhoneAlreadyExists string = "phone_already_exists" + UserUsernameKey = "username_key" + UserEmailKey = "email_key" + UsernameAlreadyExists = "username_already_exists" + UserSessionKey = "user_session_key" + EmailAlreadyExists = "email_already_exists" + PhoneNumberKey = "phone_number_key" + PhoneAlreadyExists = "phone_already_exists" + NoRowsAffected = "no rows in result set" // Auth - TokenPayload string = "token_payload" - LoggedIn string = "logged_in" - IncorrectPassword string = "incorrect_password" - ErrorGeneratingToken string = "error_generating_token" + TokenPayload = "token_payload" + LoggedIn = "logged_in" + IncorrectPassword = "incorrect_password" + ErrorGeneratingToken = "error_generating_token" ) var ( @@ -78,14 +80,7 @@ const ( EnvironmentProduction Environment = "production" ) -type LogLevel string - -const ( - LogLevelDebug LogLevel = "debug" - LogLevelInfo LogLevel = "info" - LogLevelWarn LogLevel = "warn" - LogLevelError LogLevel = "error" -) +type LogLevel slog.Level type Config struct { // default "" @@ -101,7 +96,7 @@ type Config struct { EnvMode Environment // default "debug" - LogLevel LogLevel + LogLevel slog.Level // default "UTC" Timezone string @@ -147,7 +142,7 @@ func New(config ...Config) *App { Version: "", EnvDirectory: ".env", EnvMode: EnvironmentDevelopment, - LogLevel: LogLevelDebug, + LogLevel: slog.LevelDebug, Timezone: "UTC", Paseto: nil, SMTPHost: "", @@ -168,8 +163,8 @@ func New(config ...Config) *App { if cfg.EnvMode == EnvironmentTesting { cfg.EnvDirectory = "./../../.env" } - if cfg.LogLevel == "" { - cfg.LogLevel = "debug" + if cfg.LogLevel == slog.LevelDebug { + cfg.LogLevel = slog.LevelDebug } if cfg.Timezone == "" { cfg.Timezone = "UTC" @@ -201,9 +196,20 @@ func New(config ...Config) *App { cfg.EnvMode = Environment(os.Getenv("ENV_MODE")) } - if cfg.LogLevel == "" && os.Getenv("LOG_LEVEL") != "" { - cfg.LogLevel = LogLevel(os.Getenv("LOG_LEVEL")) - logLevel = string(cfg.LogLevel) + if os.Getenv("LOG_LEVEL") != "" { + logLevel = os.Getenv("LOG_LEVEL") + switch logLevel { + case "debug": + cfg.LogLevel = slog.LevelDebug + case "info": + cfg.LogLevel = slog.LevelInfo + case "warn": + cfg.LogLevel = slog.LevelWarn + case "error": + cfg.LogLevel = slog.LevelError + default: + cfg.LogLevel = slog.LevelInfo + } } if cfg.Timezone == "" && os.Getenv("TIMEZONE") != "" { @@ -217,7 +223,7 @@ func New(config ...Config) *App { } time.Local = loc - startRotativeLogger() + startRotativeLogger(cfg.LogLevel) if cfg.Paseto == nil { var ak paseto.V4AsymmetricSecretKey @@ -335,7 +341,7 @@ func (a *App) EnvMode() Environment { return a.config.EnvMode } -func (a *App) LogLevel() LogLevel { +func (a *App) LogLevel() slog.Level { return a.config.LogLevel } @@ -422,25 +428,6 @@ func loadEnvFile(envDirectory string) error { return scanner.Err() } -func slogLevelType(level string) slog.Level { - var logLevel slog.Level - - switch strings.ToLower(level) { - case "debug": - logLevel = slog.LevelDebug - case "info": - logLevel = slog.LevelInfo - case "warn": - logLevel = slog.LevelWarn - case "error": - logLevel = slog.LevelError - default: - logLevel = slog.LevelInfo - } - - return logLevel -} - func newLogger(level slog.Level) { if err := os.MkdirAll("logs", 0755); err != nil { fmt.Println("error creating logs directory:", err) @@ -468,13 +455,13 @@ func newLogger(level slog.Level) { slog.SetDefault(logger) } -func startRotativeLogger() { - newLogger(slogLevelType(logLevel)) +func startRotativeLogger(level slog.Level) { + newLogger(level) ticker := time.NewTicker(time.Hour * 24) go func() { for range ticker.C { - newLogger(slogLevelType(logLevel)) + newLogger(level) } }() }