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 // 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

@ -21,9 +21,9 @@ 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
} }
@ -32,8 +32,8 @@ type Account struct {
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
} }