improved error helpers, fix for invalid column definitions in models

This commit is contained in:
Pavel 2019-09-12 11:49:41 +03:00
parent 868a927690
commit e0f3e6d6e0
4 changed files with 59 additions and 22 deletions

View File

@ -96,6 +96,20 @@ func NewConfig(path string) *Config {
// LoadConfig read & load configuration file // LoadConfig read & load configuration file
func (c *Config) LoadConfig(path string) *Config { func (c *Config) LoadConfig(path string) *Config {
return c.LoadConfigFromData(c.GetConfigData(path))
}
// LoadConfigFromData loads config from byte sequence
func (c *Config) LoadConfigFromData(data []byte) *Config {
if err := yaml.Unmarshal(data, c); err != nil {
panic(err)
}
return c
}
// GetConfigData returns config file data in form of byte sequence
func (c *Config) GetConfigData(path string) []byte {
var err error var err error
path, err = filepath.Abs(path) path, err = filepath.Abs(path)
@ -108,11 +122,7 @@ func (c *Config) LoadConfig(path string) *Config {
panic(err) panic(err)
} }
if err = yaml.Unmarshal(source, c); err != nil { return source
panic(err)
}
return c
} }
// GetSentryDSN sentry connection dsn // GetSentryDSN sentry connection dsn

View File

@ -1,6 +1,8 @@
package core package core
import ( import (
"html/template"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/op/go-logging" "github.com/op/go-logging"
) )
@ -84,9 +86,20 @@ func (e *Engine) Prepare() *Engine {
return e return e
} }
// templateFuncMap combines func map for templates
func (e *Engine) TemplateFuncMap(functions template.FuncMap) template.FuncMap {
funcMap := e.LocalizationFuncMap()
for name, fn := range functions {
funcMap[name] = fn
}
return funcMap
}
// CreateRenderer with translation function // CreateRenderer with translation function
func (e *Engine) CreateRenderer(callback func(*Renderer)) Renderer { func (e *Engine) CreateRenderer(callback func(*Renderer), funcs template.FuncMap) Renderer {
renderer := NewRenderer(e.LocalizationFuncMap()) renderer := NewRenderer(e.TemplateFuncMap(funcs))
callback(&renderer) callback(&renderer)
return renderer return renderer
} }

View File

@ -7,11 +7,25 @@ type ErrorResponse struct {
Error string `json:"error"` Error string `json:"error"`
} }
// GetErrorResponse returns ErrorResponse with specified status code
// Usage (with gin):
// context.JSON(GetErrorResponse(http.StatusPaymentRequired, "Not enough money"))
func GetErrorResponse(statusCode int, error string) (int, interface{}) {
return statusCode, ErrorResponse{
Error: error,
}
}
// BadRequest returns ErrorResponse with code 400 // BadRequest returns ErrorResponse with code 400
// Usage (with gin): // Usage (with gin):
// context.JSON(BadRequest("invalid data")) // context.JSON(BadRequest("invalid data"))
func BadRequest(error string) (int, interface{}) { func BadRequest(error string) (int, interface{}) {
return http.StatusBadRequest, ErrorResponse{ return GetErrorResponse(http.StatusBadRequest, error)
Error: error,
}
} }
// InternalServerError returns ErrorResponse with code 500
// Usage (with gin):
// context.JSON(BadRequest("invalid data"))
func InternalServerError(error string) (int, interface{}) {
return GetErrorResponse(http.StatusInternalServerError, error)
}

View File

@ -5,11 +5,11 @@ import "time"
// Connection model // Connection model
type Connection struct { type Connection struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
ClientID string `gorm:"column:client_id;type:varchar(70);not null;unique" json:"clientId,omitempty"` ClientID string `gorm:"column:client_id; type:varchar(70); not null; unique" json:"clientId,omitempty"`
Key string `gorm:"column:api_key;type:varchar(100);not null" json:"api_key,omitempty" binding:"required,max=100"` Key string `gorm:"column:api_key; type:varchar(100); not null" json:"api_key,omitempty" binding:"required,max=100"`
URL string `gorm:"column:api_url;type:varchar(255);not null" json:"api_url,omitempty" binding:"required,validatecrmurl,max=255"` URL string `gorm:"column:api_url; type:varchar(255); not null" json:"api_url,omitempty" binding:"required,validatecrmurl,max=255"`
GateURL string `gorm:"column:mg_url;type:varchar(255);not null;" json:"mg_url,omitempty" binding:"max=255"` GateURL string `gorm:"column:mg_url; type:varchar(255); not null;" json:"mg_url,omitempty" binding:"max=255"`
GateToken string `gorm:"column:mg_token;type:varchar(100);not null;unique" json:"mg_token,omitempty" binding:"max=100"` GateToken string `gorm:"column:mg_token; type:varchar(100); not null; unique" json:"mg_token,omitempty" binding:"max=100"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
Active bool `json:"active,omitempty"` Active bool `json:"active,omitempty"`
@ -20,10 +20,10 @@ type Connection struct {
type Account struct { type Account struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
ConnectionID int `gorm:"column:connection_id" json:"connectionId,omitempty"` ConnectionID int `gorm:"column:connection_id" json:"connectionId,omitempty"`
Channel uint64 `gorm:"column:channel;not null;unique" json:"channel,omitempty"` Channel uint64 `gorm:"column:channel; not null; unique" json:"channel,omitempty"`
ChannelSettingsHash string `gorm:"column:channel_settings_hash type:varchar(70)" binding:"max=70"` ChannelSettingsHash string `gorm:"column:channel_settings_hash; type:varchar(70)" binding:"max=70"`
Name string `gorm:"column:name type:varchar(40)" json:"name,omitempty" binding:"max=40"` Name string `gorm:"column:name; type:varchar(40)" json:"name,omitempty" binding:"max=40"`
Lang string `gorm:"column:lang type:varchar(2)" json:"lang,omitempty" binding:"max=2"` Lang string `gorm:"column:lang; type:varchar(2)" json:"lang,omitempty" binding:"max=2"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }
@ -31,9 +31,9 @@ type Account struct {
// User model // User model
type User struct { type User struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
ExternalID int `gorm:"column:external_id;not null;unique"` ExternalID int `gorm:"column:external_id; not null; unique"`
UserPhotoURL string `gorm:"column:user_photo_url type:varchar(255)" binding:"max=255"` UserPhotoURL string `gorm:"column:user_photo_url; type:varchar(255)" binding:"max=255"`
UserPhotoID string `gorm:"column:user_photo_id type:varchar(100)" binding:"max=100"` UserPhotoID string `gorm:"column:user_photo_id; type:varchar(100)" binding:"max=100"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
} }