Files
mattermost/app/app.go

211 lines
5.3 KiB
Go
Raw Normal View History

// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"io/ioutil"
"net/http"
2017-09-19 18:31:35 -05:00
"sync"
"time"
2017-09-19 18:31:35 -05:00
"github.com/mattermost/mattermost-server/einterfaces"
ejobs "github.com/mattermost/mattermost-server/einterfaces/jobs"
"github.com/mattermost/mattermost-server/jobs"
2017-09-21 04:13:34 -05:00
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin/pluginenv"
2017-09-21 04:13:34 -05:00
"github.com/mattermost/mattermost-server/utils"
)
2017-09-06 17:12:54 -05:00
type App struct {
2017-09-19 18:31:35 -05:00
Srv *Server
PluginEnv *pluginenv.Environment
PluginConfigListenerId string
2017-09-19 18:31:35 -05:00
EmailBatching *EmailBatchingJob
Hubs []*Hub
HubsStopCheckingForDeadlock chan bool
Jobs *jobs.JobServer
2017-09-19 18:31:35 -05:00
AccountMigration einterfaces.AccountMigrationInterface
Brand einterfaces.BrandInterface
Cluster einterfaces.ClusterInterface
Compliance einterfaces.ComplianceInterface
Elasticsearch einterfaces.ElasticsearchInterface
Ldap einterfaces.LdapInterface
Metrics einterfaces.MetricsInterface
Mfa einterfaces.MfaInterface
Saml einterfaces.SamlInterface
2017-09-06 17:12:54 -05:00
}
var globalApp App = App{
Jobs: &jobs.JobServer{},
}
2017-09-06 17:12:54 -05:00
var appCount = 0
2017-09-19 18:31:35 -05:00
var initEnterprise sync.Once
var UseGlobalApp = true
// New creates a new App. You must call Shutdown when you're done with it.
// XXX: Doesn't necessarily create a new App yet.
func New() *App {
appCount++
if !UseGlobalApp {
if appCount > 1 {
panic("Only one App should exist at a time. Did you forget to call Shutdown()?")
}
app := &App{
Jobs: &jobs.JobServer{},
}
app.initEnterprise()
return app
}
2017-09-19 18:31:35 -05:00
initEnterprise.Do(func() {
2017-09-21 04:13:34 -05:00
globalApp.initEnterprise()
2017-09-19 18:31:35 -05:00
})
2017-09-06 17:12:54 -05:00
return &globalApp
}
func (a *App) Shutdown() {
appCount--
if appCount == 0 {
// XXX: This is to give all of our runaway goroutines time to complete.
// We should wrangle them up and remove this.
time.Sleep(time.Second)
if a.Srv != nil {
a.StopServer()
}
}
}
2017-09-21 04:13:34 -05:00
var accountMigrationInterface func(*App) einterfaces.AccountMigrationInterface
func RegisterAccountMigrationInterface(f func(*App) einterfaces.AccountMigrationInterface) {
accountMigrationInterface = f
}
var clusterInterface func(*App) einterfaces.ClusterInterface
func RegisterClusterInterface(f func(*App) einterfaces.ClusterInterface) {
clusterInterface = f
}
var complianceInterface func(*App) einterfaces.ComplianceInterface
func RegisterComplianceInterface(f func(*App) einterfaces.ComplianceInterface) {
complianceInterface = f
}
var jobsDataRetentionInterface func(*App) ejobs.DataRetentionInterface
func RegisterJobsDataRetentionInterface(f func(*App) ejobs.DataRetentionInterface) {
jobsDataRetentionInterface = f
}
var jobsElasticsearchAggregatorInterface func(*App) ejobs.ElasticsearchAggregatorInterface
func RegisterJobsElasticsearchAggregatorInterface(f func(*App) ejobs.ElasticsearchAggregatorInterface) {
jobsElasticsearchAggregatorInterface = f
}
var jobsElasticsearchIndexerInterface func(*App) ejobs.ElasticsearchIndexerInterface
func RegisterJobsElasticsearchIndexerInterface(f func(*App) ejobs.ElasticsearchIndexerInterface) {
jobsElasticsearchIndexerInterface = f
}
var jobsLdapSyncInterface func(*App) ejobs.LdapSyncInterface
func RegisterJobsLdapSyncInterface(f func(*App) ejobs.LdapSyncInterface) {
jobsLdapSyncInterface = f
}
2017-09-21 04:13:34 -05:00
var ldapInterface func(*App) einterfaces.LdapInterface
func RegisterLdapInterface(f func(*App) einterfaces.LdapInterface) {
ldapInterface = f
}
var metricsInterface func(*App) einterfaces.MetricsInterface
func RegisterMetricsInterface(f func(*App) einterfaces.MetricsInterface) {
metricsInterface = f
}
var mfaInterface func(*App) einterfaces.MfaInterface
func RegisterMfaInterface(f func(*App) einterfaces.MfaInterface) {
mfaInterface = f
}
var samlInterface func(*App) einterfaces.SamlInterface
func RegisterSamlInterface(f func(*App) einterfaces.SamlInterface) {
samlInterface = f
}
func (a *App) initEnterprise() {
if accountMigrationInterface != nil {
a.AccountMigration = accountMigrationInterface(a)
}
a.Brand = einterfaces.GetBrandInterface()
if clusterInterface != nil {
a.Cluster = clusterInterface(a)
}
if complianceInterface != nil {
a.Compliance = complianceInterface(a)
}
a.Elasticsearch = einterfaces.GetElasticsearchInterface()
if ldapInterface != nil {
a.Ldap = ldapInterface(a)
utils.AddConfigListener(func(_, cfg *model.Config) {
if err := utils.ValidateLdapFilter(cfg, a.Ldap); err != nil {
panic(utils.T(err.Id))
}
})
}
if metricsInterface != nil {
a.Metrics = metricsInterface(a)
}
if mfaInterface != nil {
a.Mfa = mfaInterface(a)
}
if samlInterface != nil {
a.Saml = samlInterface(a)
utils.AddConfigListener(func(_, cfg *model.Config) {
a.Saml.ConfigureSP()
})
}
if jobsDataRetentionInterface != nil {
a.Jobs.DataRetention = jobsDataRetentionInterface(a)
}
if jobsElasticsearchAggregatorInterface != nil {
a.Jobs.ElasticsearchAggregator = jobsElasticsearchAggregatorInterface(a)
}
if jobsElasticsearchIndexerInterface != nil {
a.Jobs.ElasticsearchIndexer = jobsElasticsearchIndexerInterface(a)
}
if jobsLdapSyncInterface != nil {
a.Jobs.LdapSync = jobsLdapSyncInterface(a)
}
2017-09-21 04:13:34 -05:00
}
func (a *App) Config() *model.Config {
return utils.Cfg
}
func CloseBody(r *http.Response) {
if r.Body != nil {
ioutil.ReadAll(r.Body)
r.Body.Close()
}
}