Merge pull request #2 from Neur0toxine/master

[fix & improvement] improved error helpers, fix for invalid column definitions in models
This commit is contained in:
Alex Lushpai 2019-09-12 12:18:25 +03:00 committed by GitHub
commit f70d1db99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
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

View File

@ -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
}

View File

@ -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)
}

View File

@ -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
}