diff --git a/app/app.go b/app/app.go index 78cda50f28..c3e66c9923 100644 --- a/app/app.go +++ b/app/app.go @@ -78,16 +78,16 @@ func (a *App) configOrLicenseListener() { func (s *Server) initJobs() { s.Jobs = jobs.NewJobServer(s, s.Store) if jobsDataRetentionJobInterface != nil { - s.Jobs.DataRetentionJob = jobsDataRetentionJobInterface(s.FakeApp()) + s.Jobs.DataRetentionJob = jobsDataRetentionJobInterface(s) } if jobsMessageExportJobInterface != nil { - s.Jobs.MessageExportJob = jobsMessageExportJobInterface(s.FakeApp()) + s.Jobs.MessageExportJob = jobsMessageExportJobInterface(s) } if jobsElasticsearchAggregatorInterface != nil { - s.Jobs.ElasticsearchAggregator = jobsElasticsearchAggregatorInterface(s.FakeApp()) + s.Jobs.ElasticsearchAggregator = jobsElasticsearchAggregatorInterface(s) } if jobsElasticsearchIndexerInterface != nil { - s.Jobs.ElasticsearchIndexer = jobsElasticsearchIndexerInterface(s.FakeApp()) + s.Jobs.ElasticsearchIndexer = jobsElasticsearchIndexerInterface(s) } if jobsLdapSyncInterface != nil { s.Jobs.LdapSync = jobsLdapSyncInterface(s.FakeApp()) @@ -110,25 +110,6 @@ func (a *App) SetDiagnosticId(id string) { a.Srv().diagnosticId = id } -func (a *App) EnsureDiagnosticId() { - if a.Srv().diagnosticId != "" { - return - } - props, err := a.Srv().Store.System().Get() - if err != nil { - return - } - - id := props[model.SYSTEM_DIAGNOSTIC_ID] - if len(id) == 0 { - id = model.NewId() - systemId := &model.System{Name: model.SYSTEM_DIAGNOSTIC_ID, Value: id} - a.Srv().Store.System().Save(systemId) - } - - a.Srv().diagnosticId = id -} - func (a *App) HTMLTemplates() *template.Template { if a.Srv().htmlTemplateWatcher != nil { return a.Srv().htmlTemplateWatcher.Templates() diff --git a/app/app_iface.go b/app/app_iface.go index bd24703381..d7418e746c 100644 --- a/app/app_iface.go +++ b/app/app_iface.go @@ -411,7 +411,6 @@ type AppIface interface { DoUploadFileExpectModification(now time.Time, rawTeamId string, rawChannelId string, rawUserId string, rawFilename string, data []byte) (*model.FileInfo, []byte, *model.AppError) DownloadFromURL(downloadURL string) ([]byte, error) EnableUserAccessToken(token *model.UserAccessToken) *model.AppError - EnsureDiagnosticId() EnvironmentConfig() map[string]interface{} // @openTracingParams args ExecuteCommand(args *model.CommandArgs) (*model.CommandResponse, *model.AppError) diff --git a/app/enterprise.go b/app/enterprise.go index 8a6cf3a588..dcd98526f8 100644 --- a/app/enterprise.go +++ b/app/enterprise.go @@ -24,45 +24,45 @@ func RegisterClusterInterface(f func(*Server) einterfaces.ClusterInterface) { clusterInterface = f } -var complianceInterface func(*App) einterfaces.ComplianceInterface +var complianceInterface func(*Server) einterfaces.ComplianceInterface -func RegisterComplianceInterface(f func(*App) einterfaces.ComplianceInterface) { +func RegisterComplianceInterface(f func(*Server) einterfaces.ComplianceInterface) { complianceInterface = f } -var dataRetentionInterface func(*App) einterfaces.DataRetentionInterface +var dataRetentionInterface func(*Server) einterfaces.DataRetentionInterface -func RegisterDataRetentionInterface(f func(*App) einterfaces.DataRetentionInterface) { +func RegisterDataRetentionInterface(f func(*Server) einterfaces.DataRetentionInterface) { dataRetentionInterface = f } -var elasticsearchInterface func(*App) searchengine.SearchEngineInterface +var elasticsearchInterface func(*Server) searchengine.SearchEngineInterface -func RegisterElasticsearchInterface(f func(*App) searchengine.SearchEngineInterface) { +func RegisterElasticsearchInterface(f func(*Server) searchengine.SearchEngineInterface) { elasticsearchInterface = f } -var jobsDataRetentionJobInterface func(*App) ejobs.DataRetentionJobInterface +var jobsDataRetentionJobInterface func(*Server) ejobs.DataRetentionJobInterface -func RegisterJobsDataRetentionJobInterface(f func(*App) ejobs.DataRetentionJobInterface) { +func RegisterJobsDataRetentionJobInterface(f func(*Server) ejobs.DataRetentionJobInterface) { jobsDataRetentionJobInterface = f } -var jobsMessageExportJobInterface func(*App) ejobs.MessageExportJobInterface +var jobsMessageExportJobInterface func(*Server) ejobs.MessageExportJobInterface -func RegisterJobsMessageExportJobInterface(f func(*App) ejobs.MessageExportJobInterface) { +func RegisterJobsMessageExportJobInterface(f func(*Server) ejobs.MessageExportJobInterface) { jobsMessageExportJobInterface = f } -var jobsElasticsearchAggregatorInterface func(*App) ejobs.ElasticsearchAggregatorInterface +var jobsElasticsearchAggregatorInterface func(*Server) ejobs.ElasticsearchAggregatorInterface -func RegisterJobsElasticsearchAggregatorInterface(f func(*App) ejobs.ElasticsearchAggregatorInterface) { +func RegisterJobsElasticsearchAggregatorInterface(f func(*Server) ejobs.ElasticsearchAggregatorInterface) { jobsElasticsearchAggregatorInterface = f } -var jobsElasticsearchIndexerInterface func(*App) ejobs.ElasticsearchIndexerInterface +var jobsElasticsearchIndexerInterface func(*Server) ejobs.ElasticsearchIndexerInterface -func RegisterJobsElasticsearchIndexerInterface(f func(*App) ejobs.ElasticsearchIndexerInterface) { +func RegisterJobsElasticsearchIndexerInterface(f func(*Server) ejobs.ElasticsearchIndexerInterface) { jobsElasticsearchIndexerInterface = f } @@ -90,15 +90,15 @@ func RegisterLdapInterface(f func(*App) einterfaces.LdapInterface) { ldapInterface = f } -var messageExportInterface func(*App) einterfaces.MessageExportInterface +var messageExportInterface func(*Server) einterfaces.MessageExportInterface -func RegisterMessageExportInterface(f func(*App) einterfaces.MessageExportInterface) { +func RegisterMessageExportInterface(f func(*Server) einterfaces.MessageExportInterface) { messageExportInterface = f } -var metricsInterface func(*App) einterfaces.MetricsInterface +var metricsInterface func(*Server) einterfaces.MetricsInterface -func RegisterMetricsInterface(f func(*App) einterfaces.MetricsInterface) { +func RegisterMetricsInterface(f func(*Server) einterfaces.MetricsInterface) { metricsInterface = f } @@ -122,19 +122,19 @@ func RegisterNotificationInterface(f func(*App) einterfaces.NotificationInterfac func (s *Server) initEnterprise() { if metricsInterface != nil { - s.Metrics = metricsInterface(s.FakeApp()) + s.Metrics = metricsInterface(s) } if accountMigrationInterface != nil { s.AccountMigration = accountMigrationInterface(s) } if complianceInterface != nil { - s.Compliance = complianceInterface(s.FakeApp()) + s.Compliance = complianceInterface(s) } if ldapInterface != nil { s.Ldap = ldapInterface(s.FakeApp()) } if messageExportInterface != nil { - s.MessageExport = messageExportInterface(s.FakeApp()) + s.MessageExport = messageExportInterface(s) } if notificationInterface != nil { s.Notification = notificationInterface(s.FakeApp()) @@ -154,13 +154,13 @@ func (s *Server) initEnterprise() { }) } if dataRetentionInterface != nil { - s.DataRetention = dataRetentionInterface(s.FakeApp()) + s.DataRetention = dataRetentionInterface(s) } if clusterInterface != nil { s.Cluster = clusterInterface(s) } if elasticsearchInterface != nil { - s.SearchEngine.RegisterElasticsearchEngine(elasticsearchInterface(s.FakeApp())) + s.SearchEngine.RegisterElasticsearchEngine(elasticsearchInterface(s)) } } diff --git a/app/file.go b/app/file.go index 335b84bbe7..cb9392b210 100644 --- a/app/file.go +++ b/app/file.go @@ -69,8 +69,7 @@ const ( ) func (a *App) FileBackend() (filesstore.FileBackend, *model.AppError) { - license := a.License() - return filesstore.NewFileBackend(&a.Config().FileSettings, license != nil && *license.Features.Compliance) + return a.Srv().FileBackend() } func (a *App) ReadFile(path string) ([]byte, *model.AppError) { diff --git a/app/opentracing_layer.go b/app/opentracing_layer.go index dbe33e8ac1..d324ab2de8 100644 --- a/app/opentracing_layer.go +++ b/app/opentracing_layer.go @@ -3234,21 +3234,6 @@ func (a *OpenTracingAppLayer) EnableUserAccessToken(token *model.UserAccessToken return resultVar0 } -func (a *OpenTracingAppLayer) EnsureDiagnosticId() { - origCtx := a.ctx - span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.EnsureDiagnosticId") - - a.ctx = newCtx - a.app.Srv().Store.SetContext(newCtx) - defer func() { - a.app.Srv().Store.SetContext(origCtx) - a.ctx = origCtx - }() - - defer span.Finish() - a.app.EnsureDiagnosticId() -} - func (a *OpenTracingAppLayer) EnvironmentConfig() map[string]interface{} { origCtx := a.ctx span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.EnvironmentConfig") diff --git a/app/server.go b/app/server.go index 34448acc11..743e088d4f 100644 --- a/app/server.go +++ b/app/server.go @@ -33,6 +33,7 @@ import ( "github.com/mattermost/mattermost-server/v5/plugin" "github.com/mattermost/mattermost-server/v5/services/cache" "github.com/mattermost/mattermost-server/v5/services/cache/lru" + "github.com/mattermost/mattermost-server/v5/services/filesstore" "github.com/mattermost/mattermost-server/v5/services/httpservice" "github.com/mattermost/mattermost-server/v5/services/imageproxy" "github.com/mattermost/mattermost-server/v5/services/searchengine" @@ -202,7 +203,7 @@ func NewServer(options ...Option) (*Server, error) { s.NotificationsLog.ChangeLevels(utils.MloggerConfigFromLoggerConfig(notificationLogSettings, utils.GetNotificationsLogFileLocation)) }) - s.HTTPService = httpservice.MakeHTTPService(s.FakeApp()) + s.HTTPService = httpservice.MakeHTTPService(s) s.ImageProxy = imageproxy.MakeImageProxy(s, s.HTTPService, s.Log) @@ -918,3 +919,36 @@ func (s *Server) SetHub(index int, hub *Hub) error { s.hubs[index] = hub return nil } + +func (s *Server) FileBackend() (filesstore.FileBackend, *model.AppError) { + license := s.License() + return filesstore.NewFileBackend(&s.Config().FileSettings, license != nil && *license.Features.Compliance) +} + +func (s *Server) TotalWebsocketConnections() int { + count := int64(0) + for _, hub := range s.GetHubs() { + count = count + atomic.LoadInt64(&hub.connectionCount) + } + + return int(count) +} + +func (s *Server) ensureDiagnosticId() { + if s.diagnosticId != "" { + return + } + props, err := s.Store.System().Get() + if err != nil { + return + } + + id := props[model.SYSTEM_DIAGNOSTIC_ID] + if len(id) == 0 { + id = model.NewId() + systemID := &model.System{Name: model.SYSTEM_DIAGNOSTIC_ID, Value: id} + s.Store.System().Save(systemID) + } + + s.diagnosticId = id +} diff --git a/app/server_app_adapters.go b/app/server_app_adapters.go index 04a689913c..555ec2b584 100644 --- a/app/server_app_adapters.go +++ b/app/server_app_adapters.go @@ -26,11 +26,11 @@ import ( func (s *Server) RunOldAppInitialization() error { s.FakeApp().createPushNotificationsHub() - if err := utils.InitTranslations(s.FakeApp().Config().LocalizationSettings); err != nil { + if err := utils.InitTranslations(s.Config().LocalizationSettings); err != nil { return errors.Wrapf(err, "unable to load Mattermost translation files") } - s.FakeApp().Srv().configListenerId = s.FakeApp().AddConfigListener(func(_, _ *model.Config) { + s.configListenerId = s.AddConfigListener(func(_, _ *model.Config) { s.FakeApp().configOrLicenseListener() message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CONFIG_CHANGED, "", "", "", nil) @@ -40,7 +40,7 @@ func (s *Server) RunOldAppInitialization() error { s.FakeApp().Publish(message) }) }) - s.FakeApp().Srv().licenseListenerId = s.FakeApp().AddLicenseListener(func(oldLicense, newLicense *model.License) { + s.licenseListenerId = s.AddLicenseListener(func(oldLicense, newLicense *model.License) { s.FakeApp().configOrLicenseListener() message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_LICENSE_CHANGED, "", "", "", nil) @@ -59,12 +59,12 @@ func (s *Server) RunOldAppInitialization() error { s.initEnterprise() - if s.FakeApp().Srv().newStore == nil { - s.FakeApp().Srv().newStore = func() store.Store { + if s.newStore == nil { + s.newStore = func() store.Store { return store.NewTimerLayer( searchlayer.NewSearchLayer( localcachelayer.NewLocalCacheLayer( - sqlstore.NewSqlSupplier(s.FakeApp().Config().SqlSettings, s.Metrics), + sqlstore.NewSqlSupplier(s.Config().SqlSettings, s.Metrics), s.Metrics, s.Cluster, s.CacheProvider, @@ -79,10 +79,10 @@ func (s *Server) RunOldAppInitialization() error { if htmlTemplateWatcher, err := utils.NewHTMLTemplateWatcher("templates"); err != nil { mlog.Error("Failed to parse server templates", mlog.Err(err)) } else { - s.FakeApp().Srv().htmlTemplateWatcher = htmlTemplateWatcher + s.htmlTemplateWatcher = htmlTemplateWatcher } - s.FakeApp().Srv().Store = s.FakeApp().Srv().newStore() + s.Store = s.newStore() s.FakeApp().StartPushNotificationsHubWorkers() if err := s.FakeApp().ensureAsymmetricSigningKey(); err != nil { @@ -97,49 +97,49 @@ func (s *Server) RunOldAppInitialization() error { return errors.Wrapf(err, "unable to ensure installation date") } - s.FakeApp().EnsureDiagnosticId() + s.ensureDiagnosticId() s.FakeApp().regenerateClientConfig() - s.FakeApp().Srv().clusterLeaderListenerId = s.FakeApp().Srv().AddClusterLeaderChangedListener(func() { + s.clusterLeaderListenerId = s.AddClusterLeaderChangedListener(func() { mlog.Info("Cluster leader changed. Determining if job schedulers should be running:", mlog.Bool("isLeader", s.FakeApp().IsLeader())) - if s.FakeApp().Srv().Jobs != nil { - s.FakeApp().Srv().Jobs.Schedulers.HandleClusterLeaderChange(s.FakeApp().IsLeader()) + if s.Jobs != nil { + s.Jobs.Schedulers.HandleClusterLeaderChange(s.FakeApp().IsLeader()) } }) - subpath, err := utils.GetSubpathFromConfig(s.FakeApp().Config()) + subpath, err := utils.GetSubpathFromConfig(s.Config()) if err != nil { return errors.Wrap(err, "failed to parse SiteURL subpath") } - s.FakeApp().Srv().Router = s.FakeApp().Srv().RootRouter.PathPrefix(subpath).Subrouter() - pluginsRoute := s.FakeApp().Srv().Router.PathPrefix("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter() + s.Router = s.RootRouter.PathPrefix(subpath).Subrouter() + pluginsRoute := s.Router.PathPrefix("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}").Subrouter() pluginsRoute.HandleFunc("", s.FakeApp().ServePluginRequest) pluginsRoute.HandleFunc("/public/{public_file:.*}", s.FakeApp().ServePluginPublicRequest) pluginsRoute.HandleFunc("/{anything:.*}", s.FakeApp().ServePluginRequest) // If configured with a subpath, redirect 404s at the root back into the subpath. if subpath != "/" { - s.FakeApp().Srv().RootRouter.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + s.RootRouter.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.URL.Path = path.Join(subpath, r.URL.Path) http.Redirect(w, r, r.URL.String(), http.StatusFound) }) } - s.FakeApp().Srv().Router.NotFoundHandler = http.HandlerFunc(s.FakeApp().Handle404) + s.Router.NotFoundHandler = http.HandlerFunc(s.FakeApp().Handle404) - s.FakeApp().Srv().WebSocketRouter = &WebSocketRouter{ + s.WebSocketRouter = &WebSocketRouter{ app: s.FakeApp(), handlers: make(map[string]webSocketHandler), } - if err := mailservice.TestConnection(s.FakeApp().Config()); err != nil { + if err := mailservice.TestConnection(s.Config()); err != nil { mlog.Error("Mail server connection test is failed: " + err.Message) } - if _, err := url.ParseRequestURI(*s.FakeApp().Config().ServiceSettings.SiteURL); err != nil { + if _, err := url.ParseRequestURI(*s.Config().ServiceSettings.SiteURL); err != nil { mlog.Error("SiteURL must be set. Some features will operate incorrectly if the SiteURL is not set. See documentation for details: http://about.mattermost.com/default-site-url") } - backend, appErr := s.FakeApp().FileBackend() + backend, appErr := s.FileBackend() if appErr == nil { appErr = backend.TestConnection() } diff --git a/app/web_hub.go b/app/web_hub.go index 00ee3e0e75..53ecc64965 100644 --- a/app/web_hub.go +++ b/app/web_hub.go @@ -61,12 +61,7 @@ func (a *App) NewWebHub() *Hub { } func (a *App) TotalWebsocketConnections() int { - count := int64(0) - for _, hub := range a.Srv().GetHubs() { - count = count + atomic.LoadInt64(&hub.connectionCount) - } - - return int(count) + return a.Srv().TotalWebsocketConnections() } func (a *App) HubStart() {