2019-09-04 15:22:27 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2019-09-12 11:49:41 +03:00
|
|
|
"html/template"
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
2019-09-18 10:28:55 +03:00
|
|
|
"github.com/gobuffalo/packr/v2"
|
2019-09-18 13:40:36 +03:00
|
|
|
"github.com/op/go-logging"
|
2019-09-04 15:22:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Engine struct
|
|
|
|
type Engine struct {
|
|
|
|
Localizer
|
|
|
|
ORM
|
|
|
|
Sentry
|
|
|
|
Utils
|
|
|
|
ginEngine *gin.Engine
|
|
|
|
Logger *logging.Logger
|
|
|
|
Config ConfigInterface
|
|
|
|
LogFormatter logging.Formatter
|
|
|
|
prepared bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// New Engine instance (must be configured manually, gin can be accessed via engine.Router() directly or engine.ConfigureRouter(...) with callback)
|
|
|
|
func New() *Engine {
|
|
|
|
return &Engine{
|
|
|
|
Config: nil,
|
|
|
|
Localizer: Localizer{},
|
|
|
|
ORM: ORM{},
|
|
|
|
Sentry: Sentry{},
|
|
|
|
Utils: Utils{},
|
|
|
|
ginEngine: nil,
|
|
|
|
Logger: nil,
|
|
|
|
prepared: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) initGin() {
|
2019-09-18 18:02:40 +03:00
|
|
|
if !e.Config.GetDebug() {
|
2019-09-04 15:22:27 +03:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
r := gin.New()
|
|
|
|
r.Use(gin.Recovery())
|
|
|
|
|
2019-09-18 18:02:40 +03:00
|
|
|
if e.Config.GetDebug() {
|
2019-09-04 15:22:27 +03:00
|
|
|
r.Use(gin.Logger())
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Use(e.LocalizationMiddleware(), e.ErrorMiddleware())
|
|
|
|
e.ginEngine = r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare engine for start
|
|
|
|
func (e *Engine) Prepare() *Engine {
|
|
|
|
if e.prepared {
|
|
|
|
panic("engine already initialized")
|
|
|
|
}
|
|
|
|
if e.Config == nil {
|
|
|
|
panic("engine.Config must be loaded before initializing")
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.DefaultError == "" {
|
|
|
|
e.DefaultError = "error"
|
|
|
|
}
|
|
|
|
if e.LogFormatter == nil {
|
|
|
|
e.LogFormatter = DefaultLogFormatter()
|
|
|
|
}
|
|
|
|
if e.LocaleBundle == nil {
|
|
|
|
e.LocaleBundle = DefaultLocalizerBundle()
|
|
|
|
}
|
|
|
|
if e.LocaleMatcher == nil {
|
|
|
|
e.LocaleMatcher = DefaultLocalizerMatcher()
|
|
|
|
}
|
|
|
|
|
|
|
|
e.LoadTranslations()
|
|
|
|
e.createDB(e.Config.GetDBConfig())
|
|
|
|
e.createRavenClient(e.Config.GetSentryDSN())
|
2019-09-18 18:02:40 +03:00
|
|
|
e.resetUtils(e.Config.GetAWSConfig(), e.Config.GetDebug(), 0)
|
2019-09-04 15:22:27 +03:00
|
|
|
e.Logger = NewLogger(e.Config.GetTransportInfo().GetCode(), e.Config.GetLogLevel(), e.LogFormatter)
|
|
|
|
e.Sentry.Localizer = &e.Localizer
|
|
|
|
e.Utils.Logger = e.Logger
|
|
|
|
e.Sentry.Logger = e.Logger
|
|
|
|
e.prepared = true
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:49:41 +03:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
// CreateRenderer with translation function
|
2019-09-12 11:49:41 +03:00
|
|
|
func (e *Engine) CreateRenderer(callback func(*Renderer), funcs template.FuncMap) Renderer {
|
|
|
|
renderer := NewRenderer(e.TemplateFuncMap(funcs))
|
2019-09-04 15:22:27 +03:00
|
|
|
callback(&renderer)
|
|
|
|
return renderer
|
|
|
|
}
|
|
|
|
|
2019-09-18 10:28:55 +03:00
|
|
|
// CreateRendererFS with translation function and packr box with templates data
|
|
|
|
func (e *Engine) CreateRendererFS(box *packr.Box, callback func(*Renderer), funcs template.FuncMap) Renderer {
|
|
|
|
renderer := NewRenderer(e.TemplateFuncMap(funcs))
|
|
|
|
renderer.TemplatesBox = box
|
|
|
|
callback(&renderer)
|
|
|
|
return renderer
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
// Router will return current gin.Engine or panic if it's not present
|
|
|
|
func (e *Engine) Router() *gin.Engine {
|
|
|
|
if !e.prepared {
|
|
|
|
panic("prepare engine first")
|
|
|
|
}
|
|
|
|
if e.ginEngine == nil {
|
|
|
|
e.initGin()
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.ginEngine
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConfigureRouter will call provided callback with current gin.Engine, or panic if engine is not present
|
|
|
|
func (e *Engine) ConfigureRouter(callback func(*gin.Engine)) *Engine {
|
|
|
|
callback(e.Router())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run gin.Engine loop, or panic if engine is not present
|
|
|
|
func (e *Engine) Run() error {
|
|
|
|
return e.Router().Run(e.Config.GetHTTPConfig().Listen)
|
|
|
|
}
|