mg-transport-core/core/engine.go

282 lines
6.8 KiB
Go
Raw Normal View History

2019-09-04 15:22:27 +03:00
package core
import (
"html/template"
2019-10-18 13:18:36 +03:00
"net/http"
2019-12-26 16:26:38 +03:00
"sync"
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-10-31 14:21:39 +03:00
"github.com/gorilla/securecookie"
"github.com/gorilla/sessions"
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
2019-10-18 13:18:36 +03:00
httpClient *http.Client
2019-12-26 16:26:38 +03:00
logger LoggerInterface
mutex sync.RWMutex
2019-10-31 14:21:39 +03:00
csrf *CSRF
jobManager *JobManager
2019-10-31 14:21:39 +03:00
Sessions sessions.Store
2019-09-04 15:22:27 +03:00
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,
2019-12-26 16:26:38 +03:00
logger: nil,
mutex: sync.RWMutex{},
2019-09-04 15:22:27 +03:00
prepared: false,
}
}
func (e *Engine) initGin() {
2019-09-19 14:16:52 +03:00
if !e.Config.IsDebug() {
2019-09-04 15:22:27 +03:00
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(gin.Recovery())
2019-09-19 14:16:52 +03:00
if e.Config.IsDebug() {
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-19 14:16:52 +03:00
e.resetUtils(e.Config.GetAWSConfig(), e.Config.IsDebug(), 0)
2019-12-26 16:26:38 +03:00
e.SetLogger(NewLogger(e.Config.GetTransportInfo().GetCode(), e.Config.GetLogLevel(), e.LogFormatter))
2019-09-04 15:22:27 +03:00
e.Sentry.Localizer = &e.Localizer
2019-12-26 16:26:38 +03:00
e.Utils.Logger = e.Logger()
e.Sentry.Logger = e.Logger()
2019-09-04 15:22:27 +03:00
e.prepared = true
return e
}
2019-12-12 09:35:05 +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
}
2019-12-12 09:35:05 +03:00
funcMap["version"] = func() string {
return e.Config.GetVersion()
}
return funcMap
}
2019-09-04 15:22:27 +03:00
// CreateRenderer with translation function
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
}
// JobManager will return singleton JobManager from Engine
func (e *Engine) JobManager() *JobManager {
if e.jobManager == nil {
2019-12-26 16:26:38 +03:00
e.jobManager = NewJobManager().SetLogger(e.Logger()).SetLogging(e.Config.IsDebug())
}
return e.jobManager
}
// Logger returns current logger
2019-12-26 16:26:38 +03:00
func (e *Engine) Logger() LoggerInterface {
return e.logger
}
2019-12-26 18:00:55 +03:00
// SetLogger sets provided logger instance to engine
2019-12-26 16:26:38 +03:00
func (e *Engine) SetLogger(l LoggerInterface) *Engine {
if l == nil {
return e
}
e.mutex.Lock()
defer e.mutex.Unlock()
e.logger = l
return e
}
2019-10-18 13:18:36 +03:00
// BuildHTTPClient builds HTTP client with provided configuration
func (e *Engine) BuildHTTPClient(replaceDefault ...bool) *Engine {
if e.Config.GetHTTPClientConfig() != nil {
2019-12-13 09:30:16 +03:00
client, err := NewHTTPClientBuilder().
2019-12-26 16:26:38 +03:00
WithLogger(e.Logger()).
2019-12-13 09:30:16 +03:00
SetLogging(e.Config.IsDebug()).
FromEngine(e).Build(replaceDefault...)
if err != nil {
2019-10-18 13:18:36 +03:00
panic(err)
} else {
e.httpClient = client
}
}
return e
}
// SetHTTPClient sets HTTP client to engine
func (e *Engine) SetHTTPClient(client *http.Client) *Engine {
if client != nil {
e.httpClient = client
}
return e
}
2019-10-18 13:18:36 +03:00
// HTTPClient returns inner http client or default http client
func (e *Engine) HTTPClient() *http.Client {
if e.httpClient == nil {
return http.DefaultClient
}
2019-12-12 09:35:05 +03:00
return e.httpClient
2019-10-18 13:18:36 +03:00
}
2019-10-31 14:21:39 +03:00
// WithCookieSessions generates new CookieStore with optional key length.
// Default key length is 32 bytes.
func (e *Engine) WithCookieSessions(keyLength ...int) *Engine {
length := 32
if len(keyLength) > 0 && keyLength[0] > 0 {
length = keyLength[0]
}
e.Sessions = sessions.NewCookieStore(securecookie.GenerateRandomKey(length))
return e
}
2019-12-12 09:35:05 +03:00
// WithFilesystemSessions generates new FilesystemStore with optional key length.
2019-10-31 14:21:39 +03:00
// Default key length is 32 bytes.
func (e *Engine) WithFilesystemSessions(path string, keyLength ...int) *Engine {
length := 32
if len(keyLength) > 0 && keyLength[0] > 0 {
length = keyLength[0]
}
e.Sessions = sessions.NewFilesystemStore(path, securecookie.GenerateRandomKey(length))
return e
}
// InitCSRF initializes CSRF middleware. engine.Sessions must be already initialized,
// use engine.WithCookieStore or engine.WithFilesystemStore for that.
// Syntax is similar to core.NewCSRF, but you shouldn't pass sessionName, store and salt.
func (e *Engine) InitCSRF(secret string, abortFunc CSRFAbortFunc, getter CSRFTokenGetter) *Engine {
2019-10-31 14:21:39 +03:00
if e.Sessions == nil {
panic("engine.Sessions must be initialized first")
}
e.csrf = NewCSRF("", secret, "", e.Sessions, abortFunc, getter)
return e
}
// VerifyCSRFMiddleware returns CSRF verifier middleware
// Usage:
// engine.Router().Use(engine.VerifyCSRFMiddleware(core.DefaultIgnoredMethods))
func (e *Engine) VerifyCSRFMiddleware(ignoredMethods []string) gin.HandlerFunc {
if e.csrf == nil {
panic("csrf is not initialized")
}
return e.csrf.VerifyCSRFMiddleware(ignoredMethods)
}
// GenerateCSRFMiddleware returns CSRF generator middleware
// Usage:
// engine.Router().Use(engine.GenerateCSRFMiddleware())
func (e *Engine) GenerateCSRFMiddleware() gin.HandlerFunc {
if e.csrf == nil {
panic("csrf is not initialized")
}
return e.csrf.GenerateCSRFMiddleware()
}
// GetCSRFToken returns CSRF token from provided context
func (e *Engine) GetCSRFToken(c *gin.Context) string {
if e.csrf == nil {
panic("csrf is not initialized")
}
return e.csrf.CSRFFromContext(c)
}
2019-09-04 15:22:27 +03:00
// 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)
}