github.com/ashishbhate/mattermost-server@v5.11.1+incompatible/app/app.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"fmt"
     8  	"html/template"
     9  	"net/http"
    10  	"strconv"
    11  
    12  	"github.com/mattermost/mattermost-server/einterfaces"
    13  	"github.com/mattermost/mattermost-server/jobs"
    14  	"github.com/mattermost/mattermost-server/mlog"
    15  	"github.com/mattermost/mattermost-server/model"
    16  	"github.com/mattermost/mattermost-server/services/httpservice"
    17  	"github.com/mattermost/mattermost-server/services/imageproxy"
    18  	"github.com/mattermost/mattermost-server/services/timezones"
    19  	"github.com/mattermost/mattermost-server/utils"
    20  	goi18n "github.com/nicksnyder/go-i18n/i18n"
    21  )
    22  
    23  type App struct {
    24  	Srv *Server
    25  
    26  	Log *mlog.Logger
    27  
    28  	T              goi18n.TranslateFunc
    29  	Session        model.Session
    30  	RequestId      string
    31  	IpAddress      string
    32  	Path           string
    33  	UserAgent      string
    34  	AcceptLanguage string
    35  
    36  	AccountMigration einterfaces.AccountMigrationInterface
    37  	Cluster          einterfaces.ClusterInterface
    38  	Compliance       einterfaces.ComplianceInterface
    39  	DataRetention    einterfaces.DataRetentionInterface
    40  	Elasticsearch    einterfaces.ElasticsearchInterface
    41  	Ldap             einterfaces.LdapInterface
    42  	MessageExport    einterfaces.MessageExportInterface
    43  	Metrics          einterfaces.MetricsInterface
    44  	Saml             einterfaces.SamlInterface
    45  
    46  	HTTPService httpservice.HTTPService
    47  	ImageProxy  *imageproxy.ImageProxy
    48  	Timezones   *timezones.Timezones
    49  }
    50  
    51  func New(options ...AppOption) *App {
    52  	app := &App{}
    53  
    54  	for _, option := range options {
    55  		option(app)
    56  	}
    57  
    58  	return app
    59  }
    60  
    61  // DO NOT CALL THIS.
    62  // This is to avoid having to change all the code in cmd/mattermost/commands/* for now
    63  // shutdown should be called directly on the server
    64  func (a *App) Shutdown() {
    65  	a.Srv.Shutdown()
    66  	a.Srv = nil
    67  }
    68  
    69  func (a *App) configOrLicenseListener() {
    70  	a.regenerateClientConfig()
    71  }
    72  
    73  func (s *Server) initJobs() {
    74  	s.Jobs = jobs.NewJobServer(s, s.Store)
    75  	if jobsDataRetentionJobInterface != nil {
    76  		s.Jobs.DataRetentionJob = jobsDataRetentionJobInterface(s.FakeApp())
    77  	}
    78  	if jobsMessageExportJobInterface != nil {
    79  		s.Jobs.MessageExportJob = jobsMessageExportJobInterface(s.FakeApp())
    80  	}
    81  	if jobsElasticsearchAggregatorInterface != nil {
    82  		s.Jobs.ElasticsearchAggregator = jobsElasticsearchAggregatorInterface(s.FakeApp())
    83  	}
    84  	if jobsElasticsearchIndexerInterface != nil {
    85  		s.Jobs.ElasticsearchIndexer = jobsElasticsearchIndexerInterface(s.FakeApp())
    86  	}
    87  	if jobsLdapSyncInterface != nil {
    88  		s.Jobs.LdapSync = jobsLdapSyncInterface(s.FakeApp())
    89  	}
    90  	if jobsMigrationsInterface != nil {
    91  		s.Jobs.Migrations = jobsMigrationsInterface(s.FakeApp())
    92  	}
    93  	if jobsPluginsInterface != nil {
    94  		s.Jobs.Plugins = jobsPluginsInterface(s.FakeApp())
    95  	}
    96  	s.Jobs.Workers = s.Jobs.InitWorkers()
    97  	s.Jobs.Schedulers = s.Jobs.InitSchedulers()
    98  }
    99  
   100  func (a *App) DiagnosticId() string {
   101  	return a.Srv.diagnosticId
   102  }
   103  
   104  func (a *App) SetDiagnosticId(id string) {
   105  	a.Srv.diagnosticId = id
   106  }
   107  
   108  func (a *App) EnsureDiagnosticId() {
   109  	if a.Srv.diagnosticId != "" {
   110  		return
   111  	}
   112  	if result := <-a.Srv.Store.System().Get(); result.Err == nil {
   113  		props := result.Data.(model.StringMap)
   114  
   115  		id := props[model.SYSTEM_DIAGNOSTIC_ID]
   116  		if len(id) == 0 {
   117  			id = model.NewId()
   118  			systemId := &model.System{Name: model.SYSTEM_DIAGNOSTIC_ID, Value: id}
   119  			<-a.Srv.Store.System().Save(systemId)
   120  		}
   121  
   122  		a.Srv.diagnosticId = id
   123  	}
   124  }
   125  
   126  func (a *App) HTMLTemplates() *template.Template {
   127  	if a.Srv.htmlTemplateWatcher != nil {
   128  		return a.Srv.htmlTemplateWatcher.Templates()
   129  	}
   130  
   131  	return nil
   132  }
   133  
   134  func (a *App) Handle404(w http.ResponseWriter, r *http.Request) {
   135  	err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound)
   136  
   137  	mlog.Debug(fmt.Sprintf("%v: code=404 ip=%v", r.URL.Path, utils.GetIpAddress(r)))
   138  
   139  	utils.RenderWebAppError(a.Config(), w, r, err, a.AsymmetricSigningKey())
   140  }
   141  
   142  func (a *App) getSystemInstallDate() (int64, *model.AppError) {
   143  	result := <-a.Srv.Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY)
   144  	if result.Err != nil {
   145  		return 0, result.Err
   146  	}
   147  	systemData := result.Data.(*model.System)
   148  	value, err := strconv.ParseInt(systemData.Value, 10, 64)
   149  	if err != nil {
   150  		return 0, model.NewAppError("getSystemInstallDate", "app.system_install_date.parse_int.app_error", nil, err.Error(), http.StatusInternalServerError)
   151  	}
   152  	return value, nil
   153  }