From e0f3e6d6e045f6ea2ac5b71954561312aee892a4 Mon Sep 17 00:00:00 2001 From: Pavel Date: Thu, 12 Sep 2019 11:49:41 +0300 Subject: [PATCH] improved error helpers, fix for invalid column definitions in models --- core/config.go | 20 +++++++++++++++----- core/engine.go | 17 +++++++++++++++-- core/error.go | 20 +++++++++++++++++--- core/models.go | 24 ++++++++++++------------ 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/core/config.go b/core/config.go index bb0abe9..85f9b72 100644 --- a/core/config.go +++ b/core/config.go @@ -96,6 +96,20 @@ func NewConfig(path string) *Config { // LoadConfig read & load configuration file 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 path, err = filepath.Abs(path) @@ -108,11 +122,7 @@ func (c *Config) LoadConfig(path string) *Config { panic(err) } - if err = yaml.Unmarshal(source, c); err != nil { - panic(err) - } - - return c + return source } // GetSentryDSN sentry connection dsn diff --git a/core/engine.go b/core/engine.go index 0eb3a08..25f8b75 100644 --- a/core/engine.go +++ b/core/engine.go @@ -1,6 +1,8 @@ package core import ( + "html/template" + "github.com/gin-gonic/gin" "github.com/op/go-logging" ) @@ -84,9 +86,20 @@ func (e *Engine) Prepare() *Engine { 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 -func (e *Engine) CreateRenderer(callback func(*Renderer)) Renderer { - renderer := NewRenderer(e.LocalizationFuncMap()) +func (e *Engine) CreateRenderer(callback func(*Renderer), funcs template.FuncMap) Renderer { + renderer := NewRenderer(e.TemplateFuncMap(funcs)) callback(&renderer) return renderer } diff --git a/core/error.go b/core/error.go index 0abf347..011f5d6 100644 --- a/core/error.go +++ b/core/error.go @@ -7,11 +7,25 @@ type ErrorResponse struct { 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 // Usage (with gin): // context.JSON(BadRequest("invalid data")) func BadRequest(error string) (int, interface{}) { - return http.StatusBadRequest, ErrorResponse{ - Error: error, - } + return GetErrorResponse(http.StatusBadRequest, 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) +} \ No newline at end of file diff --git a/core/models.go b/core/models.go index 122612b..641309c 100644 --- a/core/models.go +++ b/core/models.go @@ -5,11 +5,11 @@ import "time" // Connection model type Connection struct { ID int `gorm:"primary_key"` - 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"` - 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"` - GateToken string `gorm:"column:mg_token;type:varchar(100);not null;unique" json:"mg_token,omitempty" binding:"max=100"` + 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"` + 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"` + GateToken string `gorm:"column:mg_token; type:varchar(100); not null; unique" json:"mg_token,omitempty" binding:"max=100"` CreatedAt time.Time UpdatedAt time.Time Active bool `json:"active,omitempty"` @@ -20,10 +20,10 @@ type Connection struct { type Account struct { ID int `gorm:"primary_key"` ConnectionID int `gorm:"column:connection_id" json:"connectionId,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"` - 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"` + Channel uint64 `gorm:"column:channel; not null; unique" json:"channel,omitempty"` + 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"` + Lang string `gorm:"column:lang; type:varchar(2)" json:"lang,omitempty" binding:"max=2"` CreatedAt time.Time UpdatedAt time.Time } @@ -31,9 +31,9 @@ type Account struct { // User model type User struct { ID int `gorm:"primary_key"` - ExternalID int `gorm:"column:external_id;not null;unique"` - 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"` + ExternalID int `gorm:"column:external_id; not null; unique"` + 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"` CreatedAt time.Time UpdatedAt time.Time }