7 "github.com/gin-gonic/gin"
8 "github.com/jloup/utils"
12 var MAIL_CONFIG MailConfig
14 type MailConfig struct {
16 SmtpAddress string `toml:"smtp_address"`
17 AddressFrom string `toml:"address_from"`
18 Login string `toml:"login"`
19 Password string `toml:"password"`
23 Domain string `toml:"domain"`
24 JwtSecret string `toml:"jwt_secret"`
25 PasswordResetSecret string `toml:"password_reset_secret"`
26 FreeSMSUser string `toml:"free_sms_user"`
27 FreeSMSPass string `toml:"free_sms_pass"`
30 func SetConfig(config Config) {
33 JWT_SECRET = []byte(config.JwtSecret)
34 PASSWORD_RESET_SECRET = []byte(config.PasswordResetSecret)
37 func SetMailConfig(config MailConfig) {
40 if config.Login != "" && config.AddressFrom != "" && config.Password != "" && config.SmtpAddress != "" {
41 MAIL_CONFIG.IsEnabled = true
42 ConfigureMailTemplateEngine()
52 func (e Error) FlagString() string {
53 return e.Code.String()
56 func (e Error) Flag() utils.Flag {
60 func (e Error) Msg() string {
64 func (e Error) ErrorWithCode(f utils.Flag) utils.ErrorFlagged {
65 if utils.Intersect(e.Code, f) {
71 func (e Error) Err() error {
79 func (e Error) Error() string {
87 func ErrorIs(err error, code utils.Flag) bool {
92 if apiError, ok := err.(*Error); !ok {
95 return utils.Intersect(apiError.Code, code) && apiError.Code.String() == code.String()
99 func NewInternalError(err error) *Error {
100 if apiError, ok := err.(*Error); ok {
103 return &Error{InternalError, "internal error", err}
106 func ToSnake(in string) string {
111 for i := 0; i < length; i++ {
112 if i > 0 && unicode.IsUpper(runes[i]) && ((i+1 < length && unicode.IsLower(runes[i+1])) || unicode.IsLower(runes[i-1])) {
113 out = append(out, '_')
115 out = append(out, unicode.ToLower(runes[i]))
121 type Response struct {
122 StatusCode Status `json:"-"`
123 ErrorCode utils.Flag `json:"-"`
125 Status string `json:"status"`
126 Code string `json:"code,omitempty"`
127 Response interface{} `json:"response,omitempty"`
128 Message string `json:"message,omitempty"`
131 func (r Response) populateStatus() Response {
132 r.Status = ToSnake(r.StatusCode.String())
133 r.Code = ToSnake(r.ErrorCode.String())
138 func ErrorResponse(code utils.Flag, message string) Response {
146 func SuccessResponse(data interface{}) Response {
153 func WriteJsonResponse(response Response, c *gin.Context) {
154 response = response.populateStatus()
156 c.JSON(StatusToHttpCode(response.StatusCode, response.ErrorCode), response)
159 func WriteBinary(contentType string, b []byte, c *gin.Context) {
160 c.Data(http.StatusOK, contentType, b)
163 type Middleware func(*gin.Context) *Error
165 func M(handler Middleware) gin.HandlerFunc {
166 return func(c *gin.Context) {
170 WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
179 type Query interface {
180 ValidateParams() *Error
181 Run() (interface{}, *Error)
184 func RunQuery(query Query, c *gin.Context) {
185 if err := query.ValidateParams(); err != nil {
186 WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
191 if out, err := query.Run(); err != nil {
192 WriteJsonResponse(ErrorResponse(err.Flag(), err.UserMessage), c)
195 WriteJsonResponse(SuccessResponse(out), c)