Compare commits

..

No commits in common. "f1fb183935e9805674646e1a13e0370955084142" and "b9356ae0e147ee62cedebf802510c27175e6128b" have entirely different histories.

2 changed files with 81 additions and 68 deletions

View File

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

View File

@ -15,12 +15,12 @@ type Mailer struct {
smtpPass string smtpPass string
} }
func New() Mailer { func New(smtpHost string, smtpPort string, smtpUser string, smtpPass string) Mailer {
return Mailer{ return Mailer{
smtpHost: os.Getenv("SMTP_HOST"), smtpHost: smtpHost,
smtpPort: os.Getenv("SMTP_PORT"), smtpPort: smtpPort,
smtpUser: os.Getenv("SMTP_USER"), smtpUser: smtpUser,
smtpPass: os.Getenv("SMTP_PASS"), smtpPass: smtpPass,
} }
} }
@ -45,7 +45,7 @@ func (m *Mailer) SendMail(to []string, templateName string, data interface{}) er
} }
func getTemplate(templateName string) string { func getTemplate(templateName string) string {
templatePath := "templates/mail/" + templateName + ".gotmpl" templatePath := "templates/" + templateName + ".gotmpl"
content, err := os.ReadFile(templatePath) content, err := os.ReadFile(templatePath)
if err != nil { if err != nil {
fmt.Printf("Error leyendo plantilla: %v\n", err) fmt.Printf("Error leyendo plantilla: %v\n", err)