-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
server_app_adapters.go
202 lines (164 loc) · 6.36 KB
/
server_app_adapters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package app
import (
"net/http"
"net/url"
"path"
"github.com/mattermost/mattermost-server/v5/mlog"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/services/mailservice"
"github.com/mattermost/mattermost-server/v5/store"
"github.com/mattermost/mattermost-server/v5/store/localcachelayer"
"github.com/mattermost/mattermost-server/v5/store/searchlayer"
"github.com/mattermost/mattermost-server/v5/store/sqlstore"
"github.com/mattermost/mattermost-server/v5/utils"
"github.com/pkg/errors"
)
// This is a bridge between the old and new initialization for the context refactor.
// It calls app layer initialization code that then turns around and acts on the server.
// Don't add anything new here, new initialization should be done in the server and
// performed in the NewServer function.
func (s *Server) RunOldAppInitialization() error {
s.FakeApp().createPushNotificationsHub()
if err := utils.InitTranslations(s.Config().LocalizationSettings); err != nil {
return errors.Wrapf(err, "unable to load Mattermost translation files")
}
s.configListenerId = s.AddConfigListener(func(_, _ *model.Config) {
s.FakeApp().configOrLicenseListener()
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CONFIG_CHANGED, "", "", "", nil)
message.Add("config", s.FakeApp().ClientConfigWithComputed())
s.Go(func() {
s.FakeApp().Publish(message)
})
})
s.licenseListenerId = s.AddLicenseListener(func(oldLicense, newLicense *model.License) {
s.FakeApp().configOrLicenseListener()
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_LICENSE_CHANGED, "", "", "", nil)
message.Add("license", s.FakeApp().GetSanitizedClientLicense())
s.Go(func() {
s.FakeApp().Publish(message)
})
})
if err := s.FakeApp().SetupInviteEmailRateLimiting(); err != nil {
return err
}
mlog.Info("Server is initializing...")
s.initEnterprise()
if s.newStore == nil {
s.newStore = func() store.Store {
s.sqlStore = sqlstore.NewSqlSupplier(s.Config().SqlSettings, s.Metrics)
searchStore := searchlayer.NewSearchLayer(
localcachelayer.NewLocalCacheLayer(
s.sqlStore,
s.Metrics,
s.Cluster,
s.CacheProvider,
),
s.SearchEngine,
s.Config(),
)
s.AddConfigListener(func(prevCfg, cfg *model.Config) {
searchStore.UpdateConfig(cfg)
})
s.sqlStore.UpdateLicense(s.License())
s.AddLicenseListener(func(oldLicense, newLicense *model.License) {
s.sqlStore.UpdateLicense(newLicense)
})
return store.NewTimerLayer(
searchStore,
s.Metrics,
)
}
}
if htmlTemplateWatcher, err := utils.NewHTMLTemplateWatcher("templates"); err != nil {
mlog.Error("Failed to parse server templates", mlog.Err(err))
} else {
s.htmlTemplateWatcher = htmlTemplateWatcher
}
s.Store = s.newStore()
s.FakeApp().StartPushNotificationsHubWorkers()
if err := s.FakeApp().ensureAsymmetricSigningKey(); err != nil {
return errors.Wrapf(err, "unable to ensure asymmetric signing key")
}
if err := s.FakeApp().ensurePostActionCookieSecret(); err != nil {
return errors.Wrapf(err, "unable to ensure PostAction cookie secret")
}
if err := s.FakeApp().ensureInstallationDate(); err != nil {
return errors.Wrapf(err, "unable to ensure installation date")
}
if err := s.FakeApp().ensureFirstServerRunTimestamp(); err != nil {
return errors.Wrapf(err, "unable to ensure first run timestamp")
}
s.ensureDiagnosticId()
s.FakeApp().regenerateClientConfig()
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.Jobs != nil {
s.Jobs.Schedulers.HandleClusterLeaderChange(s.FakeApp().IsLeader())
}
})
subpath, err := utils.GetSubpathFromConfig(s.Config())
if err != nil {
return errors.Wrap(err, "failed to parse SiteURL subpath")
}
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.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.Router.NotFoundHandler = http.HandlerFunc(s.FakeApp().Handle404)
s.WebSocketRouter = &WebSocketRouter{
app: s.FakeApp(),
handlers: make(map[string]webSocketHandler),
}
if err := mailservice.TestConnection(s.Config()); err != nil {
mlog.Error("Mail server connection test is failed: " + err.Message)
}
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.FileBackend()
if appErr == nil {
appErr = backend.TestConnection()
}
if appErr != nil {
mlog.Error("Problem with file storage settings", mlog.Err(appErr))
}
if model.BuildEnterpriseReady == "true" {
s.FakeApp().LoadLicense()
}
s.FakeApp().DoAppMigrations()
s.FakeApp().InitPostMetadata()
s.FakeApp().InitPlugins(*s.Config().PluginSettings.Directory, *s.Config().PluginSettings.ClientDirectory)
s.FakeApp().AddConfigListener(func(prevCfg, cfg *model.Config) {
if *cfg.PluginSettings.Enable {
s.FakeApp().InitPlugins(*cfg.PluginSettings.Directory, *s.Config().PluginSettings.ClientDirectory)
} else {
s.FakeApp().ShutDownPlugins()
}
})
return nil
}
func (s *Server) RunOldAppShutdown() {
s.FakeApp().HubStop()
s.FakeApp().StopPushNotificationsHubWorkers()
s.FakeApp().ShutDownPlugins()
s.FakeApp().RemoveLicenseListener(s.licenseListenerId)
s.RemoveClusterLeaderChangedListener(s.clusterLeaderListenerId)
}
// A temporary bridge to deal with cases where the code is so tighly coupled that
// this is easier as a temporary solution
func (s *Server) FakeApp() *App {
a := New(
ServerConnector(s),
)
return a
}