github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/app/app.go (about) 1 // Copyright (c) 2016-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package app 5 6 import ( 7 "html/template" 8 "net/http" 9 "strconv" 10 11 goi18n "github.com/xzl8028/go-i18n/i18n" 12 "github.com/xzl8028/xenia-server/einterfaces" 13 "github.com/xzl8028/xenia-server/jobs" 14 "github.com/xzl8028/xenia-server/mlog" 15 "github.com/xzl8028/xenia-server/model" 16 "github.com/xzl8028/xenia-server/services/httpservice" 17 "github.com/xzl8028/xenia-server/services/imageproxy" 18 "github.com/xzl8028/xenia-server/services/timezones" 19 "github.com/xzl8028/xenia-server/utils" 20 ) 21 22 type App struct { 23 Srv *Server 24 25 Log *mlog.Logger 26 NotificationsLog *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/xenia/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 props, err := a.Srv.Store.System().Get() 113 if err != nil { 114 return 115 } 116 117 id := props[model.SYSTEM_DIAGNOSTIC_ID] 118 if len(id) == 0 { 119 id = model.NewId() 120 systemId := &model.System{Name: model.SYSTEM_DIAGNOSTIC_ID, Value: id} 121 a.Srv.Store.System().Save(systemId) 122 } 123 124 a.Srv.diagnosticId = id 125 } 126 127 func (a *App) HTMLTemplates() *template.Template { 128 if a.Srv.htmlTemplateWatcher != nil { 129 return a.Srv.htmlTemplateWatcher.Templates() 130 } 131 132 return nil 133 } 134 135 func (a *App) Handle404(w http.ResponseWriter, r *http.Request) { 136 ipAddress := utils.GetIpAddress(r, a.Config().ServiceSettings.TrustedProxyIPHeader) 137 mlog.Debug("not found handler triggered", mlog.String("path", r.URL.Path), mlog.Int("code", 404), mlog.String("ip", ipAddress)) 138 139 if *a.Config().ServiceSettings.WebserverMode == "disabled" { 140 http.NotFound(w, r) 141 return 142 } 143 144 utils.RenderWebAppError(a.Config(), w, r, model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound), a.AsymmetricSigningKey()) 145 } 146 147 func (a *App) getSystemInstallDate() (int64, *model.AppError) { 148 systemData, appErr := a.Srv.Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY) 149 if appErr != nil { 150 return 0, appErr 151 } 152 value, err := strconv.ParseInt(systemData.Value, 10, 64) 153 if err != nil { 154 return 0, model.NewAppError("getSystemInstallDate", "app.system_install_date.parse_int.app_error", nil, err.Error(), http.StatusInternalServerError) 155 } 156 return value, nil 157 }