Improve API4 initialization (#18680)
* Improve API4 initialization - Refactored openGraphDataCache to be inside app layer. - Moved the cache instance from global variable to be inside server. - Moved out the app instantiation from the global commands package to be instantiated on every call. Only the server instance is passed. - Moved InitLocal to be called from inside Init. ```release-note NONE ``` * Remove commented line ```release-note NONE ```
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
43a99a5783
Коммит
c27814393d
@@ -41,7 +41,7 @@ func GetHandlerName(h func(*Context, http.ResponseWriter, *http.Request)) string
|
||||
|
||||
func (w *Web) NewHandler(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
|
||||
return &Handler{
|
||||
App: w.app,
|
||||
Srv: w.srv,
|
||||
HandleFunc: h,
|
||||
HandlerName: GetHandlerName(h),
|
||||
RequireSession: false,
|
||||
@@ -55,10 +55,10 @@ func (w *Web) NewHandler(h func(*Context, http.ResponseWriter, *http.Request)) h
|
||||
func (w *Web) NewStaticHandler(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
|
||||
// Determine the CSP SHA directive needed for subpath support, if any. This value is fixed
|
||||
// on server start and intentionally requires a restart to take effect.
|
||||
subpath, _ := utils.GetSubpathFromConfig(w.app.Config())
|
||||
subpath, _ := utils.GetSubpathFromConfig(w.srv.Config())
|
||||
|
||||
return &Handler{
|
||||
App: w.app,
|
||||
Srv: w.srv,
|
||||
HandleFunc: h,
|
||||
HandlerName: GetHandlerName(h),
|
||||
RequireSession: false,
|
||||
@@ -71,7 +71,7 @@ func (w *Web) NewStaticHandler(h func(*Context, http.ResponseWriter, *http.Reque
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
App app.AppIface
|
||||
Srv *app.Server
|
||||
HandleFunc func(*Context, http.ResponseWriter, *http.Request)
|
||||
HandlerName string
|
||||
RequireSession bool
|
||||
@@ -90,6 +90,8 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w = newWrappedWriter(w)
|
||||
now := time.Now()
|
||||
|
||||
appInstance := app.New(app.ServerConnector(h.Srv.Channels()))
|
||||
|
||||
requestID := model.NewId()
|
||||
var statusCode string
|
||||
defer func() {
|
||||
@@ -107,7 +109,7 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
c := &Context{
|
||||
AppContext: &request.Context{},
|
||||
App: h.App,
|
||||
App: appInstance,
|
||||
}
|
||||
|
||||
t, _ := i18n.GetTranslationsAndLocaleFromRequest(r)
|
||||
@@ -394,7 +396,7 @@ func (h *Handler) checkCSRFToken(c *Context, r *http.Request, token string, toke
|
||||
// granted.
|
||||
func (w *Web) APIHandler(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
|
||||
handler := &Handler{
|
||||
App: w.app,
|
||||
Srv: w.srv,
|
||||
HandleFunc: h,
|
||||
HandlerName: GetHandlerName(h),
|
||||
RequireSession: false,
|
||||
@@ -403,7 +405,7 @@ func (w *Web) APIHandler(h func(*Context, http.ResponseWriter, *http.Request)) h
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
if *w.app.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
if *w.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gziphandler.GzipHandler(handler)
|
||||
}
|
||||
return handler
|
||||
@@ -414,7 +416,7 @@ func (w *Web) APIHandler(h func(*Context, http.ResponseWriter, *http.Request)) h
|
||||
// websocket.
|
||||
func (w *Web) APIHandlerTrustRequester(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
|
||||
handler := &Handler{
|
||||
App: w.app,
|
||||
Srv: w.srv,
|
||||
HandleFunc: h,
|
||||
HandlerName: GetHandlerName(h),
|
||||
RequireSession: false,
|
||||
@@ -423,7 +425,7 @@ func (w *Web) APIHandlerTrustRequester(h func(*Context, http.ResponseWriter, *ht
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
if *w.app.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
if *w.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gziphandler.GzipHandler(handler)
|
||||
}
|
||||
return handler
|
||||
@@ -433,7 +435,7 @@ func (w *Web) APIHandlerTrustRequester(h func(*Context, http.ResponseWriter, *ht
|
||||
// be granted.
|
||||
func (w *Web) APISessionRequired(h func(*Context, http.ResponseWriter, *http.Request)) http.Handler {
|
||||
handler := &Handler{
|
||||
App: w.app,
|
||||
Srv: w.srv,
|
||||
HandleFunc: h,
|
||||
HandlerName: GetHandlerName(h),
|
||||
RequireSession: true,
|
||||
@@ -442,7 +444,7 @@ func (w *Web) APISessionRequired(h func(*Context, http.ResponseWriter, *http.Req
|
||||
IsStatic: false,
|
||||
IsLocal: false,
|
||||
}
|
||||
if *w.app.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
if *w.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
return gziphandler.GzipHandler(handler)
|
||||
}
|
||||
return handler
|
||||
|
||||
@@ -25,7 +25,7 @@ func TestHandlerServeHTTPErrors(t *testing.T) {
|
||||
th := SetupWithStoreMock(t)
|
||||
defer th.TearDown()
|
||||
|
||||
web := New(th.App, th.Server.Router)
|
||||
web := New(th.Server)
|
||||
handler := web.NewHandler(handlerForHTTPErrors)
|
||||
|
||||
var flagtests = []struct {
|
||||
@@ -84,7 +84,7 @@ func TestHandlerServeHTTPSecureTransport(t *testing.T) {
|
||||
*config.ServiceSettings.TLSStrictTransportMaxAge = 6000
|
||||
})
|
||||
|
||||
web := New(th.App, th.Server.Router)
|
||||
web := New(th.Server)
|
||||
handler := web.NewHandler(handlerForHTTPSecureTransport)
|
||||
|
||||
request := httptest.NewRequest("GET", "/api/v4/test", nil)
|
||||
@@ -136,10 +136,10 @@ func TestHandlerServeCSRFToken(t *testing.T) {
|
||||
t.Errorf("Expected nil, got %s", err)
|
||||
}
|
||||
|
||||
web := New(th.App, th.Server.Router)
|
||||
web := New(th.Server)
|
||||
|
||||
handler := Handler{
|
||||
App: web.app,
|
||||
Srv: web.srv,
|
||||
HandleFunc: handlerForCSRFToken,
|
||||
RequireSession: true,
|
||||
TrustRequester: false,
|
||||
@@ -219,7 +219,7 @@ func TestHandlerServeCSRFToken(t *testing.T) {
|
||||
// Handler with RequireSession set to false
|
||||
|
||||
handlerNoSession := Handler{
|
||||
App: th.App,
|
||||
Srv: th.Server,
|
||||
HandleFunc: handlerForCSRFToken,
|
||||
RequireSession: false,
|
||||
TrustRequester: false,
|
||||
@@ -263,10 +263,10 @@ func TestHandlerServeCSPHeader(t *testing.T) {
|
||||
th := SetupWithStoreMock(t)
|
||||
defer th.TearDown()
|
||||
|
||||
web := New(th.App, th.Server.Router)
|
||||
web := New(th.Server)
|
||||
|
||||
handler := Handler{
|
||||
App: web.app,
|
||||
Srv: web.srv,
|
||||
HandleFunc: handlerForCSPHeader,
|
||||
RequireSession: false,
|
||||
TrustRequester: false,
|
||||
@@ -285,10 +285,10 @@ func TestHandlerServeCSPHeader(t *testing.T) {
|
||||
th := SetupWithStoreMock(t)
|
||||
defer th.TearDown()
|
||||
|
||||
web := New(th.App, th.Server.Router)
|
||||
web := New(th.Server)
|
||||
|
||||
handler := Handler{
|
||||
App: web.app,
|
||||
Srv: web.srv,
|
||||
HandleFunc: handlerForCSPHeader,
|
||||
RequireSession: false,
|
||||
TrustRequester: false,
|
||||
@@ -325,10 +325,10 @@ func TestHandlerServeCSPHeader(t *testing.T) {
|
||||
*cfg.ServiceSettings.SiteURL = *cfg.ServiceSettings.SiteURL + "/subpath"
|
||||
})
|
||||
|
||||
web := New(th.App, th.Server.Router)
|
||||
web := New(th.Server)
|
||||
|
||||
handler := Handler{
|
||||
App: web.app,
|
||||
Srv: web.srv,
|
||||
HandleFunc: handlerForCSPHeader,
|
||||
RequireSession: false,
|
||||
TrustRequester: false,
|
||||
@@ -380,10 +380,10 @@ func TestHandlerServeInvalidToken(t *testing.T) {
|
||||
*cfg.ServiceSettings.SiteURL = tc.SiteURL
|
||||
})
|
||||
|
||||
web := New(th.App, th.Server.Router)
|
||||
web := New(th.Server)
|
||||
|
||||
handler := Handler{
|
||||
App: web.app,
|
||||
Srv: web.srv,
|
||||
HandleFunc: handlerForCSRFToken,
|
||||
RequireSession: true,
|
||||
TrustRequester: false,
|
||||
|
||||
@@ -21,20 +21,20 @@ import (
|
||||
var robotsTxt = []byte("User-agent: *\nDisallow: /\n")
|
||||
|
||||
func (w *Web) InitStatic() {
|
||||
if *w.app.Config().ServiceSettings.WebserverMode != "disabled" {
|
||||
if err := utils.UpdateAssetsSubpathFromConfig(w.app.Config()); err != nil {
|
||||
if *w.srv.Config().ServiceSettings.WebserverMode != "disabled" {
|
||||
if err := utils.UpdateAssetsSubpathFromConfig(w.srv.Config()); err != nil {
|
||||
mlog.Error("Failed to update assets subpath from config", mlog.Err(err))
|
||||
}
|
||||
|
||||
staticDir, _ := fileutils.FindDir(model.ClientDir)
|
||||
mlog.Debug("Using client directory", mlog.String("clientDir", staticDir))
|
||||
|
||||
subpath, _ := utils.GetSubpathFromConfig(w.app.Config())
|
||||
subpath, _ := utils.GetSubpathFromConfig(w.srv.Config())
|
||||
|
||||
staticHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static"), http.FileServer(http.Dir(staticDir))))
|
||||
pluginHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static", "plugins"), http.FileServer(http.Dir(*w.app.Config().PluginSettings.ClientDirectory))))
|
||||
pluginHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static", "plugins"), http.FileServer(http.Dir(*w.srv.Config().PluginSettings.ClientDirectory))))
|
||||
|
||||
if *w.app.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
if *w.srv.Config().ServiceSettings.WebserverMode == "gzip" {
|
||||
staticHandler = gziphandler.GzipHandler(staticHandler)
|
||||
pluginHandler = gziphandler.GzipHandler(pluginHandler)
|
||||
}
|
||||
|
||||
@@ -18,16 +18,16 @@ import (
|
||||
)
|
||||
|
||||
type Web struct {
|
||||
app app.AppIface
|
||||
srv *app.Server
|
||||
MainRouter *mux.Router
|
||||
}
|
||||
|
||||
func New(a app.AppIface, root *mux.Router) *Web {
|
||||
func New(srv *app.Server) *Web {
|
||||
mlog.Debug("Initializing web routes")
|
||||
|
||||
web := &Web{
|
||||
app: a,
|
||||
MainRouter: root,
|
||||
srv: srv,
|
||||
MainRouter: srv.Router,
|
||||
}
|
||||
|
||||
web.InitOAuth()
|
||||
|
||||
@@ -122,7 +122,7 @@ func setupTestHelper(tb testing.TB, includeCacheLayer bool) *TestHelper {
|
||||
ctx := &request.Context{}
|
||||
a := app.New(app.ServerConnector(s.Channels()))
|
||||
|
||||
web := New(a, s.Router)
|
||||
web := New(s)
|
||||
URL = fmt.Sprintf("http://localhost:%v", s.ListenAddr.Port)
|
||||
apiClient = model.NewAPIv4Client(URL)
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user