X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=api%2Fapi.go;h=e01181123633e4a7112dd1124375ae8d7f5e680b;hb=2da5b12c31074591eaf16929b760322b98f189e8;hp=7b7be49257a2949796b5435ccaddc6abd63893bf;hpb=7a9e5112eaaea58d55f181d3e5296e4ff839921c;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FFront.git diff --git a/api/api.go b/api/api.go index 7b7be49..e011811 100644 --- a/api/api.go +++ b/api/api.go @@ -7,6 +7,41 @@ import ( "github.com/gin-gonic/gin" ) +var CONFIG Config +var MAIL_CONFIG MailConfig + +type MailConfig struct { + IsEnabled bool + SmtpAddress string `toml:"smtp_address"` + AddressFrom string `toml:"address_from"` + Login string `toml:"login"` + Password string `toml:"password"` +} + +type Config struct { + Domain string `toml:"domain"` + JwtSecret string `toml:"jwt_secret"` + PasswordResetSecret string `toml:"password_reset_secret"` + FreeSMSUser string `toml:"free_sms_user"` + FreeSMSPass string `toml:"free_sms_pass"` +} + +func SetConfig(config Config) { + CONFIG = config + + JWT_SECRET = []byte(config.JwtSecret) + PASSWORD_RESET_SECRET = []byte(config.PasswordResetSecret) +} + +func SetMailConfig(config MailConfig) { + MAIL_CONFIG = config + + if config.Login != "" && config.AddressFrom != "" && config.Password != "" && config.SmtpAddress != "" { + MAIL_CONFIG.IsEnabled = true + ConfigureMailTemplateEngine() + } +} + type Error struct { Code ErrorCode UserMessage string @@ -29,7 +64,22 @@ func (e Error) Error() string { return "" } +func ErrorIs(err error, code ErrorCode) bool { + if err == nil { + return false + } + + if apiError, ok := err.(*Error); !ok { + return false + } else { + return apiError.Code == code + } +} + func NewInternalError(err error) *Error { + if apiError, ok := err.(*Error); ok { + return apiError + } return &Error{InternalError, "internal error", err} }