2019-09-04 15:22:27 +03:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2020-11-12 17:24:40 +03:00
|
|
|
"crypto/x509"
|
2019-09-12 11:49:41 +03:00
|
|
|
"html/template"
|
2019-10-18 13:18:36 +03:00
|
|
|
"net/http"
|
2019-12-26 16:26:38 +03:00
|
|
|
"sync"
|
2019-09-12 11:49:41 +03:00
|
|
|
|
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"
|
2020-05-18 13:37:24 +03:00
|
|
|
"golang.org/x/text/language"
|
2019-09-04 15:22:27 +03:00
|
|
|
)
|
|
|
|
|
2020-11-13 12:04:32 +03:00
|
|
|
var boolTrue = true
|
|
|
|
|
|
|
|
// DefaultHTTPClientConfig is a default config for HTTP client. It will be used by Engine for building HTTP client
|
|
|
|
// if HTTP client config is not present in the configuration.
|
|
|
|
var DefaultHTTPClientConfig = &HTTPClientConfig{
|
|
|
|
Timeout: 30,
|
|
|
|
SSLVerification: &boolTrue,
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Engine struct.
|
2019-09-04 15:22:27 +03:00
|
|
|
type Engine struct {
|
|
|
|
Localizer
|
|
|
|
ORM
|
|
|
|
Sentry
|
|
|
|
Utils
|
2020-05-18 13:37:24 +03:00
|
|
|
ginEngine *gin.Engine
|
|
|
|
httpClient *http.Client
|
|
|
|
logger LoggerInterface
|
|
|
|
mutex sync.RWMutex
|
|
|
|
csrf *CSRF
|
|
|
|
jobManager *JobManager
|
|
|
|
PreloadLanguages []language.Tag
|
|
|
|
Sessions sessions.Store
|
|
|
|
Config ConfigInterface
|
|
|
|
LogFormatter logging.Formatter
|
|
|
|
prepared bool
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// New Engine instance (must be configured manually, gin can be accessed via engine.Router() directly or
|
|
|
|
// engine.ConfigureRouter(...) with callback).
|
2019-09-04 15:22:27 +03:00
|
|
|
func New() *Engine {
|
|
|
|
return &Engine{
|
2020-05-18 13:37:24 +03:00
|
|
|
Config: nil,
|
|
|
|
Localizer: Localizer{
|
|
|
|
i18nStorage: &sync.Map{},
|
|
|
|
loadMutex: &sync.RWMutex{},
|
|
|
|
},
|
|
|
|
PreloadLanguages: []language.Tag{},
|
|
|
|
ORM: ORM{},
|
|
|
|
Sentry: Sentry{},
|
|
|
|
Utils: Utils{},
|
|
|
|
ginEngine: nil,
|
|
|
|
logger: nil,
|
|
|
|
mutex: sync.RWMutex{},
|
|
|
|
prepared: false,
|
2019-09-04 15:22:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Prepare engine for start.
|
2019-09-04 15:22:27 +03:00
|
|
|
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.LocaleMatcher == nil {
|
|
|
|
e.LocaleMatcher = DefaultLocalizerMatcher()
|
|
|
|
}
|
|
|
|
|
2020-05-18 13:37:24 +03:00
|
|
|
if e.isUnd(e.Localizer.LanguageTag) {
|
|
|
|
e.Localizer.LanguageTag = DefaultLanguage
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
e.LoadTranslations()
|
2020-05-18 13:37:24 +03:00
|
|
|
|
|
|
|
if len(e.PreloadLanguages) > 0 {
|
|
|
|
e.Localizer.Preload(e.PreloadLanguages)
|
|
|
|
}
|
|
|
|
|
2019-09-04 15:22:27 +03:00
|
|
|
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
|
2020-10-30 13:05:42 +03:00
|
|
|
e.Sentry.Stacktrace = true
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// TemplateFuncMap combines func map for templates.
|
2019-09-12 11:49:41 +03:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-09-12 11:49:41 +03:00
|
|
|
return funcMap
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +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
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// CreateRendererFS with translation function and packr box with templates data.
|
2019-09-18 10:28:55 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Router will return current gin.Engine or panic if it's not present.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (e *Engine) Router() *gin.Engine {
|
|
|
|
if !e.prepared {
|
|
|
|
panic("prepare engine first")
|
|
|
|
}
|
|
|
|
if e.ginEngine == nil {
|
|
|
|
e.initGin()
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.ginEngine
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// JobManager will return singleton JobManager from Engine.
|
2019-12-17 10:39:48 +03:00
|
|
|
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())
|
2019-12-17 10:39:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return e.jobManager
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Logger returns current logger.
|
2019-12-26 16:26:38 +03:00
|
|
|
func (e *Engine) Logger() LoggerInterface {
|
|
|
|
return e.logger
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +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
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// BuildHTTPClient builds HTTP client with provided configuration.
|
2020-11-12 17:24:40 +03:00
|
|
|
func (e *Engine) BuildHTTPClient(certs *x509.CertPool, replaceDefault ...bool) *Engine {
|
2020-11-13 12:04:32 +03:00
|
|
|
client, err := NewHTTPClientBuilder().
|
|
|
|
WithLogger(e.Logger()).
|
|
|
|
SetLogging(e.Config.IsDebug()).
|
|
|
|
SetCertPool(certs).
|
|
|
|
FromEngine(e).
|
|
|
|
Build(replaceDefault...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
} else {
|
|
|
|
e.httpClient = client
|
2019-10-18 13:18:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetHTTPClientConfig returns configuration for HTTP client.
|
2020-11-13 12:04:32 +03:00
|
|
|
func (e *Engine) GetHTTPClientConfig() *HTTPClientConfig {
|
|
|
|
if e.Config.GetHTTPClientConfig() != nil {
|
|
|
|
return e.Config.GetHTTPClientConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
return DefaultHTTPClientConfig
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// SetHTTPClient sets HTTP client to engine.
|
2019-10-24 16:44:28 +03:00
|
|
|
func (e *Engine) SetHTTPClient(client *http.Client) *Engine {
|
|
|
|
if client != nil {
|
|
|
|
e.httpClient = client
|
|
|
|
}
|
|
|
|
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// HTTPClient returns inner http client or default http client.
|
2019-10-18 13:18:36 +03:00
|
|
|
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.
|
2019-12-12 12:05:48 +03:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// GetCSRFToken returns CSRF token from provided context.
|
2019-10-31 14:21:39 +03:00
|
|
|
func (e *Engine) GetCSRFToken(c *gin.Context) string {
|
|
|
|
if e.csrf == nil {
|
|
|
|
panic("csrf is not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e.csrf.CSRFFromContext(c)
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// ConfigureRouter will call provided callback with current gin.Engine, or panic if engine is not present.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (e *Engine) ConfigureRouter(callback func(*gin.Engine)) *Engine {
|
|
|
|
callback(e.Router())
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-02-09 14:57:14 +03:00
|
|
|
// Run gin.Engine loop, or panic if engine is not present.
|
2019-09-04 15:22:27 +03:00
|
|
|
func (e *Engine) Run() error {
|
|
|
|
return e.Router().Run(e.Config.GetHTTPConfig().Listen)
|
|
|
|
}
|