]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blobdiff - api/api.go
Fix missing ticker.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / api.go
index ece2a26aee852a63727a56bac5fd3faac9da4ccf..f9213018a5970052c1885afdf43ac4b4df9623ac 100644 (file)
@@ -5,11 +5,22 @@ import (
        "unicode"
 
        "github.com/gin-gonic/gin"
+       "github.com/jloup/utils"
 )
 
 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"`
@@ -23,12 +34,40 @@ func SetConfig(config Config) {
        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
+       Code        utils.Flag
        UserMessage string
        err         error
 }
 
+func (e Error) FlagString() string {
+       return e.Code.String()
+}
+
+func (e Error) Flag() utils.Flag {
+       return e.Code
+}
+
+func (e Error) Msg() string {
+       return e.UserMessage
+}
+
+func (e Error) ErrorWithCode(f utils.Flag) utils.ErrorFlagged {
+       if utils.Intersect(e.Code, f) {
+               return e
+       }
+
+       return nil
+}
 func (e Error) Err() error {
        if e.err != nil {
                return e
@@ -45,7 +84,7 @@ func (e Error) Error() string {
        return ""
 }
 
-func ErrorIs(err error, code ErrorCode) bool {
+func ErrorIs(err error, code utils.Flag) bool {
        if err == nil {
                return false
        }
@@ -53,11 +92,14 @@ func ErrorIs(err error, code ErrorCode) bool {
        if apiError, ok := err.(*Error); !ok {
                return false
        } else {
-               return apiError.Code == code
+               return utils.Intersect(apiError.Code, code) && apiError.Code.String() == code.String()
        }
 }
 
 func NewInternalError(err error) *Error {
+       if apiError, ok := err.(*Error); ok {
+               return apiError
+       }
        return &Error{InternalError, "internal error", err}
 }
 
@@ -77,8 +119,8 @@ func ToSnake(in string) string {
 }
 
 type Response struct {
-       StatusCode Status    `json:"-"`
-       ErrorCode  ErrorCode `json:"-"`
+       StatusCode Status     `json:"-"`
+       ErrorCode  utils.Flag `json:"-"`
 
        Status   string      `json:"status"`
        Code     string      `json:"code,omitempty"`
@@ -88,15 +130,12 @@ type Response struct {
 
 func (r Response) populateStatus() Response {
        r.Status = ToSnake(r.StatusCode.String())
-
-       if r.ErrorCode != 0 {
-               r.Code = ToSnake(r.ErrorCode.String())
-       }
+       r.Code = ToSnake(r.ErrorCode.String())
 
        return r
 }
 
-func ErrorResponse(code ErrorCode, message string) Response {
+func ErrorResponse(code utils.Flag, message string) Response {
        return Response{
                StatusCode: NOK,
                ErrorCode:  code,
@@ -128,7 +167,7 @@ func M(handler Middleware) gin.HandlerFunc {
                err := handler(c)
 
                if err != nil {
-                       WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c)
+                       WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
                        c.Error(err)
                        c.Abort()
                } else {
@@ -144,13 +183,13 @@ type Query interface {
 
 func RunQuery(query Query, c *gin.Context) {
        if err := query.ValidateParams(); err != nil {
-               WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c)
+               WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
                c.Error(err)
                return
        }
 
        if out, err := query.Run(); err != nil {
-               WriteJsonResponse(ErrorResponse(err.Code, err.UserMessage), c)
+               WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
                c.Error(err)
        } else {
                WriteJsonResponse(SuccessResponse(out), c)