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
const (
// Handlers keys
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"
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"
// User keys (DB)
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"
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"
// Auth
TokenPayload = "token_payload"
LoggedIn = "logged_in"
IncorrectPassword = "incorrect_password"
ErrorGeneratingToken = "error_generating_token"
TokenPayload string = "token_payload"
LoggedIn string = "logged_in"
IncorrectPassword string = "incorrect_password"
ErrorGeneratingToken string = "error_generating_token"
)
var (
@ -80,7 +78,14 @@ const (
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 {
// default ""
@ -96,7 +101,7 @@ type Config struct {
EnvMode Environment
// default "debug"
LogLevel slog.Level
LogLevel LogLevel
// default "UTC"
Timezone string
@ -142,7 +147,7 @@ func New(config ...Config) *App {
Version: "",
EnvDirectory: ".env",
EnvMode: EnvironmentDevelopment,
LogLevel: slog.LevelDebug,
LogLevel: LogLevelDebug,
Timezone: "UTC",
Paseto: nil,
SMTPHost: "",
@ -163,8 +168,8 @@ func New(config ...Config) *App {
if cfg.EnvMode == EnvironmentTesting {
cfg.EnvDirectory = "./../../.env"
}
if cfg.LogLevel == slog.LevelDebug {
cfg.LogLevel = slog.LevelDebug
if cfg.LogLevel == "" {
cfg.LogLevel = "debug"
}
if cfg.Timezone == "" {
cfg.Timezone = "UTC"
@ -196,20 +201,9 @@ func New(config ...Config) *App {
cfg.EnvMode = Environment(os.Getenv("ENV_MODE"))
}
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.LogLevel == "" && os.Getenv("LOG_LEVEL") != "" {
cfg.LogLevel = LogLevel(os.Getenv("LOG_LEVEL"))
logLevel = string(cfg.LogLevel)
}
if cfg.Timezone == "" && os.Getenv("TIMEZONE") != "" {
@ -223,7 +217,7 @@ func New(config ...Config) *App {
}
time.Local = loc
startRotativeLogger(cfg.LogLevel)
startRotativeLogger()
if cfg.Paseto == nil {
var ak paseto.V4AsymmetricSecretKey
@ -341,7 +335,7 @@ func (a *App) EnvMode() Environment {
return a.config.EnvMode
}
func (a *App) LogLevel() slog.Level {
func (a *App) LogLevel() LogLevel {
return a.config.LogLevel
}
@ -428,6 +422,25 @@ 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)
@ -455,13 +468,13 @@ func newLogger(level slog.Level) {
slog.SetDefault(logger)
}
func startRotativeLogger(level slog.Level) {
newLogger(level)
func startRotativeLogger() {
newLogger(slogLevelType(logLevel))
ticker := time.NewTicker(time.Hour * 24)
go func() {
for range ticker.C {
newLogger(level)
newLogger(slogLevelType(logLevel))
}
}()
}

View File

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