github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/app/app.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package app 5 6 import ( 7 "context" 8 "html/template" 9 "net/http" 10 "strconv" 11 12 goi18n "github.com/mattermost/go-i18n/i18n" 13 "github.com/vnforks/kid/v5/einterfaces" 14 "github.com/vnforks/kid/v5/jobs" 15 "github.com/vnforks/kid/v5/mlog" 16 "github.com/vnforks/kid/v5/model" 17 "github.com/vnforks/kid/v5/services/httpservice" 18 "github.com/vnforks/kid/v5/services/imageproxy" 19 "github.com/vnforks/kid/v5/services/searchengine" 20 "github.com/vnforks/kid/v5/services/timezones" 21 "github.com/vnforks/kid/v5/utils" 22 ) 23 24 type App struct { 25 srv *Server 26 27 log *mlog.Logger 28 notificationsLog *mlog.Logger 29 30 t goi18n.TranslateFunc 31 session model.Session 32 requestId string 33 ipAddress string 34 path string 35 userAgent string 36 acceptLanguage string 37 38 accountMigration einterfaces.AccountMigrationInterface 39 cluster einterfaces.ClusterInterface 40 compliance einterfaces.ComplianceInterface 41 dataRetention einterfaces.DataRetentionInterface 42 searchEngine *searchengine.Broker 43 ldap einterfaces.LdapInterface 44 messageExport einterfaces.MessageExportInterface 45 metrics einterfaces.MetricsInterface 46 notification einterfaces.NotificationInterface 47 saml einterfaces.SamlInterface 48 49 httpService httpservice.HTTPService 50 imageProxy *imageproxy.ImageProxy 51 timezones *timezones.Timezones 52 53 context context.Context 54 } 55 56 func New(options ...AppOption) *App { 57 app := &App{} 58 59 for _, option := range options { 60 option(app) 61 } 62 63 return app 64 } 65 66 // DO NOT CALL THIS. 67 // This is to avoid having to change all the code in cmd/kid/commands/* for now 68 // shutdown should be called directly on the server 69 func (a *App) Shutdown() { 70 a.Srv().Shutdown() 71 a.srv = nil 72 } 73 74 func (a *App) configOrLicenseListener() { 75 a.regenerateClientConfig() 76 } 77 78 func (s *Server) initJobs() { 79 s.Jobs = jobs.NewJobServer(s, s.Store) 80 if jobsDataRetentionJobInterface != nil { 81 s.Jobs.DataRetentionJob = jobsDataRetentionJobInterface(s) 82 } 83 if jobsMessageExportJobInterface != nil { 84 s.Jobs.MessageExportJob = jobsMessageExportJobInterface(s) 85 } 86 if jobsElasticsearchAggregatorInterface != nil { 87 s.Jobs.ElasticsearchAggregator = jobsElasticsearchAggregatorInterface(s) 88 } 89 if jobsElasticsearchIndexerInterface != nil { 90 s.Jobs.ElasticsearchIndexer = jobsElasticsearchIndexerInterface(s) 91 } 92 if jobsLdapSyncInterface != nil { 93 s.Jobs.LdapSync = jobsLdapSyncInterface(s.FakeApp()) 94 } 95 if jobsMigrationsInterface != nil { 96 s.Jobs.Migrations = jobsMigrationsInterface(s.FakeApp()) 97 } 98 if jobsPluginsInterface != nil { 99 s.Jobs.Plugins = jobsPluginsInterface(s.FakeApp()) 100 } 101 s.Jobs.Workers = s.Jobs.InitWorkers() 102 s.Jobs.Schedulers = s.Jobs.InitSchedulers() 103 } 104 105 func (a *App) DiagnosticId() string { 106 return a.Srv().diagnosticId 107 } 108 109 func (a *App) SetDiagnosticId(id string) { 110 a.Srv().diagnosticId = id 111 } 112 113 func (a *App) HTMLTemplates() *template.Template { 114 if a.Srv().htmlTemplateWatcher != nil { 115 return a.Srv().htmlTemplateWatcher.Templates() 116 } 117 118 return nil 119 } 120 121 func (a *App) Handle404(w http.ResponseWriter, r *http.Request) { 122 ipAddress := utils.GetIpAddress(r, a.Config().ServiceSettings.TrustedProxyIPHeader) 123 mlog.Debug("not found handler triggered", mlog.String("path", r.URL.Path), mlog.Int("code", 404), mlog.String("ip", ipAddress)) 124 125 if *a.Config().ServiceSettings.WebserverMode == "disabled" { 126 http.NotFound(w, r) 127 return 128 } 129 130 utils.RenderWebAppError(a.Config(), w, r, model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound), a.AsymmetricSigningKey()) 131 } 132 133 func (a *App) getSystemInstallDate() (int64, *model.AppError) { 134 systemData, appErr := a.Srv().Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY) 135 if appErr != nil { 136 return 0, appErr 137 } 138 value, err := strconv.ParseInt(systemData.Value, 10, 64) 139 if err != nil { 140 return 0, model.NewAppError("getSystemInstallDate", "app.system_install_date.parse_int.app_error", nil, err.Error(), http.StatusInternalServerError) 141 } 142 return value, nil 143 } 144 145 func (a *App) Srv() *Server { 146 return a.srv 147 } 148 func (a *App) Log() *mlog.Logger { 149 return a.log 150 } 151 func (a *App) NotificationsLog() *mlog.Logger { 152 return a.notificationsLog 153 } 154 func (a *App) T(translationID string, args ...interface{}) string { 155 return a.t(translationID, args...) 156 } 157 func (a *App) Session() *model.Session { 158 return &a.session 159 } 160 func (a *App) RequestId() string { 161 return a.requestId 162 } 163 func (a *App) IpAddress() string { 164 return a.ipAddress 165 } 166 func (a *App) Path() string { 167 return a.path 168 } 169 func (a *App) UserAgent() string { 170 return a.userAgent 171 } 172 func (a *App) AcceptLanguage() string { 173 return a.acceptLanguage 174 } 175 func (a *App) AccountMigration() einterfaces.AccountMigrationInterface { 176 return a.accountMigration 177 } 178 func (a *App) Cluster() einterfaces.ClusterInterface { 179 return a.cluster 180 } 181 func (a *App) Compliance() einterfaces.ComplianceInterface { 182 return a.compliance 183 } 184 func (a *App) DataRetention() einterfaces.DataRetentionInterface { 185 return a.dataRetention 186 } 187 func (a *App) SearchEngine() *searchengine.Broker { 188 return a.searchEngine 189 } 190 func (a *App) Ldap() einterfaces.LdapInterface { 191 return a.ldap 192 } 193 func (a *App) MessageExport() einterfaces.MessageExportInterface { 194 return a.messageExport 195 } 196 func (a *App) Metrics() einterfaces.MetricsInterface { 197 return a.metrics 198 } 199 func (a *App) Notification() einterfaces.NotificationInterface { 200 return a.notification 201 } 202 func (a *App) Saml() einterfaces.SamlInterface { 203 return a.saml 204 } 205 func (a *App) HTTPService() httpservice.HTTPService { 206 return a.httpService 207 } 208 func (a *App) ImageProxy() *imageproxy.ImageProxy { 209 return a.imageProxy 210 } 211 func (a *App) Timezones() *timezones.Timezones { 212 return a.timezones 213 } 214 func (a *App) Context() context.Context { 215 return a.context 216 } 217 218 func (a *App) SetSession(s *model.Session) { 219 a.session = *s 220 } 221 222 func (a *App) SetT(t goi18n.TranslateFunc) { 223 a.t = t 224 } 225 func (a *App) SetRequestId(s string) { 226 a.requestId = s 227 } 228 func (a *App) SetIpAddress(s string) { 229 a.ipAddress = s 230 } 231 func (a *App) SetUserAgent(s string) { 232 a.userAgent = s 233 } 234 func (a *App) SetAcceptLanguage(s string) { 235 a.acceptLanguage = s 236 } 237 func (a *App) SetPath(s string) { 238 a.path = s 239 } 240 func (a *App) SetContext(c context.Context) { 241 a.context = c 242 } 243 func (a *App) SetServer(srv *Server) { 244 a.srv = srv 245 } 246 func (a *App) GetT() goi18n.TranslateFunc { 247 return a.t 248 } 249 func (a *App) SetLog(l *mlog.Logger) { 250 a.log = l 251 }