Cleanup related to context refactor (#9988)

Этот коммит содержится в:
Christopher Speller
2018-12-17 08:51:46 -08:00
коммит произвёл GitHub
родитель 829f183bb0
Коммит 8429add371
55 изменённых файлов: 1027 добавлений и 949 удалений

Просмотреть файл

@@ -14,6 +14,7 @@ import (
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/services/httpservice"
"github.com/mattermost/mattermost-server/services/timezones"
"github.com/mattermost/mattermost-server/utils"
goi18n "github.com/nicksnyder/go-i18n/i18n"
)
@@ -42,6 +43,7 @@ type App struct {
Saml einterfaces.SamlInterface
HTTPService httpservice.HTTPService
Timezones *timezones.Timezones
}
func New(options ...AppOption) *App {
@@ -132,57 +134,6 @@ func (a *App) Handle404(w http.ResponseWriter, r *http.Request) {
utils.RenderWebAppError(a.Config(), w, r, err, a.AsymmetricSigningKey())
}
func (a *App) StartElasticsearch() {
a.Srv.Go(func() {
if err := a.Elasticsearch.Start(); err != nil {
mlog.Error(err.Error())
}
})
a.AddConfigListener(func(oldConfig *model.Config, newConfig *model.Config) {
if !*oldConfig.ElasticsearchSettings.EnableIndexing && *newConfig.ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() {
if err := a.Elasticsearch.Start(); err != nil {
mlog.Error(err.Error())
}
})
} else if *oldConfig.ElasticsearchSettings.EnableIndexing && !*newConfig.ElasticsearchSettings.EnableIndexing {
a.Srv.Go(func() {
if err := a.Elasticsearch.Stop(); err != nil {
mlog.Error(err.Error())
}
})
} else if *oldConfig.ElasticsearchSettings.Password != *newConfig.ElasticsearchSettings.Password || *oldConfig.ElasticsearchSettings.Username != *newConfig.ElasticsearchSettings.Username || *oldConfig.ElasticsearchSettings.ConnectionUrl != *newConfig.ElasticsearchSettings.ConnectionUrl || *oldConfig.ElasticsearchSettings.Sniff != *newConfig.ElasticsearchSettings.Sniff {
a.Srv.Go(func() {
if *oldConfig.ElasticsearchSettings.EnableIndexing {
if err := a.Elasticsearch.Stop(); err != nil {
mlog.Error(err.Error())
}
if err := a.Elasticsearch.Start(); err != nil {
mlog.Error(err.Error())
}
}
})
}
})
a.AddLicenseListener(func() {
if a.License() != nil {
a.Srv.Go(func() {
if err := a.Elasticsearch.Start(); err != nil {
mlog.Error(err.Error())
}
})
} else {
a.Srv.Go(func() {
if err := a.Elasticsearch.Stop(); err != nil {
mlog.Error(err.Error())
}
})
}
})
}
func (a *App) getSystemInstallDate() (int64, *model.AppError) {
result := <-a.Srv.Store.System().GetByName(model.SYSTEM_INSTALLATION_DATE_KEY)
if result.Err != nil {

Просмотреть файл

@@ -11,6 +11,7 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
type AutoPostCreator struct {
@@ -43,7 +44,7 @@ func NewAutoPostCreator(client *model.Client4, channelid string) *AutoPostCreato
func (cfg *AutoPostCreator) UploadTestFile() ([]string, bool) {
filename := cfg.ImageFilenames[utils.RandIntFromRange(utils.Range{Begin: 0, End: len(cfg.ImageFilenames) - 1})]
path, _ := utils.FindDir("web/static/images")
path, _ := fileutils.FindDir("web/static/images")
file, err := os.Open(filepath.Join(path, filename))
if err != nil {
return nil, false

Просмотреть файл

@@ -16,13 +16,14 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store/sqlstore"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
func TestLoadConfig(t *testing.T) {
tempConfig, err := ioutil.TempFile("", "")
require.Nil(t, err)
input, err := ioutil.ReadFile(utils.FindConfigFile("config.json"))
input, err := ioutil.ReadFile(fileutils.FindConfigFile("config.json"))
require.Nil(t, err)
lines := strings.Split(string(input), "\n")
for i, line := range lines {

Просмотреть файл

@@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
func TestGeneratePublicLinkHash(t *testing.T) {
@@ -171,7 +171,7 @@ func TestMigrateFilenamesToFileInfos(t *testing.T) {
infos = th.App.MigrateFilenamesToFileInfos(post)
assert.Equal(t, 0, len(infos))
path, _ := utils.FindDir("tests")
path, _ := fileutils.FindDir("tests")
file, fileErr := os.Open(filepath.Join(path, "test.png"))
require.Nil(t, fileErr)
defer file.Close()

Просмотреть файл

@@ -15,6 +15,7 @@ import (
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
type TestHelper struct {
@@ -35,7 +36,7 @@ type TestHelper struct {
func setupTestHelper(enterprise bool) *TestHelper {
mainHelper.Store.DropAllTables()
permConfig, err := os.Open(utils.FindConfigFile("config.json"))
permConfig, err := os.Open(fileutils.FindConfigFile("config.json"))
if err != nil {
panic(err)
}
@@ -68,16 +69,13 @@ func setupTestHelper(enterprise bool) *TestHelper {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.RateLimitSettings.Enable = false })
prevListenAddress := *th.App.Config().ServiceSettings.ListenAddress
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
serverErr := th.App.StartServer()
serverErr := th.Server.Start()
if serverErr != nil {
panic(serverErr)
}
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = prevListenAddress })
th.App.DoAdvancedPermissionsMigration()
th.App.DoEmojisPermissionsMigration()
th.App.Srv.Store.MarkSystemRanUnitTests()
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.TeamSettings.EnableOpenServer = true })

Просмотреть файл

@@ -13,7 +13,7 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
func TestImportImportScheme(t *testing.T) {
@@ -613,7 +613,7 @@ func TestImportImportUser(t *testing.T) {
// Do a valid user in apply mode.
username := model.NewId()
testsDir, _ := utils.FindDir("tests")
testsDir, _ := fileutils.FindDir("tests")
data = UserImportData{
ProfileImage: ptrStr(filepath.Join(testsDir, "test.png")),
Username: &username,
@@ -2345,7 +2345,7 @@ func TestImportImportEmoji(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
testsDir, _ := utils.FindDir("tests")
testsDir, _ := fileutils.FindDir("tests")
testImage := filepath.Join(testsDir, "test.png")
data := EmojiImportData{Name: ptrStr(model.NewId())}
@@ -2382,7 +2382,7 @@ func TestImportAttachment(t *testing.T) {
th := Setup()
defer th.TearDown()
testsDir, _ := utils.FindDir("tests")
testsDir, _ := fileutils.FindDir("tests")
testImage := filepath.Join(testsDir, "test.png")
invalidPath := "some-invalid-path"
@@ -2455,7 +2455,7 @@ func TestImportPostAndRepliesWithAttachments(t *testing.T) {
time := model.GetMillis()
attachmentsPostTime := time
attachmentsReplyTime := time + 1
testsDir, _ := utils.FindDir("tests")
testsDir, _ := fileutils.FindDir("tests")
testImage := filepath.Join(testsDir, "test.png")
testMarkDown := filepath.Join(testsDir, "test-attachments.md")
data := &PostImportData{
@@ -2545,7 +2545,7 @@ func TestImportDirectPostWithAttachments(t *testing.T) {
th := Setup()
defer th.TearDown()
testsDir, _ := utils.FindDir("tests")
testsDir, _ := fileutils.FindDir("tests")
testImage := filepath.Join(testsDir, "test.png")
// Create a user.

Просмотреть файл

@@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
func ptrStr(s string) *string {
@@ -171,7 +171,7 @@ func TestImportBulkImport(t *testing.T) {
username2 := model.NewId()
username3 := model.NewId()
emojiName := model.NewId()
testsDir, _ := utils.FindDir("tests")
testsDir, _ := fileutils.FindDir("tests")
testImage := filepath.Join(testsDir, "test.png")
teamTheme1 := `{\"awayIndicator\":\"#DBBD4E\",\"buttonBg\":\"#23A1FF\",\"buttonColor\":\"#FFFFFF\",\"centerChannelBg\":\"#ffffff\",\"centerChannelColor\":\"#333333\",\"codeTheme\":\"github\",\"image\":\"/static/files/a4a388b38b32678e83823ef1b3e17766.png\",\"linkColor\":\"#2389d7\",\"mentionBg\":\"#2389d7\",\"mentionColor\":\"#ffffff\",\"mentionHighlightBg\":\"#fff2bb\",\"mentionHighlightLink\":\"#2f81b7\",\"newMessageSeparator\":\"#FF8800\",\"onlineIndicator\":\"#7DBE00\",\"sidebarBg\":\"#fafafa\",\"sidebarHeaderBg\":\"#3481B9\",\"sidebarHeaderTextColor\":\"#ffffff\",\"sidebarText\":\"#333333\",\"sidebarTextActiveBorder\":\"#378FD2\",\"sidebarTextActiveColor\":\"#111111\",\"sidebarTextHoverBg\":\"#e6f2fa\",\"sidebarUnreadText\":\"#333333\",\"type\":\"Mattermost\"}`
teamTheme2 := `{\"awayIndicator\":\"#DBBD4E\",\"buttonBg\":\"#23A100\",\"buttonColor\":\"#EEEEEE\",\"centerChannelBg\":\"#ffffff\",\"centerChannelColor\":\"#333333\",\"codeTheme\":\"github\",\"image\":\"/static/files/a4a388b38b32678e83823ef1b3e17766.png\",\"linkColor\":\"#2389d7\",\"mentionBg\":\"#2389d7\",\"mentionColor\":\"#ffffff\",\"mentionHighlightBg\":\"#fff2bb\",\"mentionHighlightLink\":\"#2f81b7\",\"newMessageSeparator\":\"#FF8800\",\"onlineIndicator\":\"#7DBE00\",\"sidebarBg\":\"#fafafa\",\"sidebarHeaderBg\":\"#3481B9\",\"sidebarHeaderTextColor\":\"#ffffff\",\"sidebarText\":\"#333333\",\"sidebarTextActiveBorder\":\"#378FD2\",\"sidebarTextActiveColor\":\"#222222\",\"sidebarTextHoverBg\":\"#e6f2fa\",\"sidebarUnreadText\":\"#444444\",\"type\":\"Mattermost\"}`

Просмотреть файл

@@ -9,7 +9,7 @@ import (
"testing"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
"github.com/stretchr/testify/assert"
)
@@ -572,7 +572,7 @@ func TestImportValidateUserImportData(t *testing.T) {
}
// Test a valid User with all fields populated.
testsDir, _ := utils.FindDir("tests")
testsDir, _ := fileutils.FindDir("tests")
data = UserImportData{
ProfileImage: ptrStr(filepath.Join(testsDir, "test.png")),
Username: ptrStr("bob"),

Просмотреть файл

@@ -171,12 +171,22 @@ func (a *App) RemoveLicense() *model.AppError {
return nil
}
func (s *Server) AddLicenseListener(listener func()) string {
id := model.NewId()
s.licenseListeners[id] = listener
return id
}
func (a *App) AddLicenseListener(listener func()) string {
id := model.NewId()
a.Srv.licenseListeners[id] = listener
return id
}
func (s *Server) RemoveLicenseListener(id string) {
delete(s.licenseListeners, id)
}
func (a *App) RemoveLicenseListener(id string) {
delete(a.Srv.licenseListeners, id)
}

Просмотреть файл

@@ -11,6 +11,7 @@ import (
"time"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/services/timezones"
"github.com/mattermost/mattermost-server/utils"
)
@@ -230,7 +231,7 @@ func TestGetNotificationEmailBodyFullNotificationLocaleTimeWithTimezone(t *testi
defer th.TearDown()
recipient := &model.User{
Timezone: model.DefaultUserTimezone(),
Timezone: timezones.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
@@ -261,7 +262,7 @@ func TestGetNotificationEmailBodyFullNotificationLocaleTimeNoTimezone(t *testing
defer th.TearDown()
recipient := &model.User{
Timezone: model.DefaultUserTimezone(),
Timezone: timezones.DefaultUserTimezone(),
}
post := &model.Post{
CreateAt: 1524681000000,
@@ -303,7 +304,7 @@ func TestGetNotificationEmailBodyFullNotificationLocaleTime12Hour(t *testing.T)
defer th.TearDown()
recipient := &model.User{
Timezone: model.DefaultUserTimezone(),
Timezone: timezones.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{
@@ -335,7 +336,7 @@ func TestGetNotificationEmailBodyFullNotificationLocaleTime24Hour(t *testing.T)
defer th.TearDown()
recipient := &model.User{
Timezone: model.DefaultUserTimezone(),
Timezone: timezones.DefaultUserTimezone(),
}
recipient.Timezone["automaticTimezone"] = "America/New_York"
post := &model.Post{

Просмотреть файл

@@ -36,6 +36,10 @@ func ConfigFile(file string) Option {
}
}
func RunJobs(s *Server) {
s.runjobs = true
}
func DisableConfigWatch(s *Server) {
s.disableConfigWatch = true
}
@@ -49,7 +53,6 @@ func ServerConnector(s *Server) AppOption {
a.Log = s.Log
a.HTTPService = s.HTTPService
a.AccountMigration = s.AccountMigration
a.Cluster = s.Cluster
a.Compliance = s.Compliance
@@ -59,5 +62,8 @@ func ServerConnector(s *Server) AppOption {
a.MessageExport = s.MessageExport
a.Metrics = s.Metrics
a.Saml = s.Saml
a.HTTPService = s.HTTPService
a.Timezones = s.timezones
}
}

Просмотреть файл

@@ -12,7 +12,7 @@ import (
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
// GetPluginsEnvironment returns the plugin environment for use if plugins are enabled and
@@ -146,7 +146,7 @@ func (a *App) InitPlugins(pluginDir, webappPluginDir string) {
}
a.SetPluginsEnvironment(env)
prepackagedPluginsDir, found := utils.FindDir("prepackaged_plugins")
prepackagedPluginsDir, found := fileutils.FindDir("prepackaged_plugins")
if found {
if err := filepath.Walk(prepackagedPluginsDir, func(walkPath string, info os.FileInfo, err error) error {
if !strings.HasSuffix(walkPath, ".tar.gz") {

Просмотреть файл

@@ -12,6 +12,7 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
func (a *App) GetSamlMetadata() (string, *model.AppError) {
@@ -40,7 +41,7 @@ func WriteSamlFile(fileData *multipart.FileHeader) *model.AppError {
}
defer file.Close()
configDir, _ := utils.FindDir("config")
configDir, _ := fileutils.FindDir("config")
out, err := os.Create(filepath.Join(configDir, filename))
if err != nil {
return model.NewAppError("AddSamlCertificate", "api.admin.add_certificate.saving.app_error", nil, err.Error(), http.StatusInternalServerError)
@@ -112,7 +113,7 @@ func RemoveSamlFile(filename string) *model.AppError {
return model.NewAppError("AddSamlCertificate", "api.admin.remove_certificate.delete.app_error", nil, "", http.StatusBadRequest)
}
if err := os.Remove(utils.FindConfigFile(filename)); err != nil {
if err := os.Remove(fileutils.FindConfigFile(filename)); err != nil {
return model.NewAppError("removeCertificate", "api.admin.remove_certificate.delete.app_error", map[string]interface{}{"Filename": filename}, err.Error(), http.StatusInternalServerError)
}

Просмотреть файл

@@ -13,6 +13,7 @@ import (
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/services/mailservice"
"github.com/mattermost/mattermost-server/utils"
)
@@ -31,9 +32,9 @@ const (
PROP_SECURITY_UNIT_TESTS = "ut"
)
func (a *App) DoSecurityUpdateCheck() {
if *a.Config().ServiceSettings.EnableSecurityFixAlert {
if result := <-a.Srv.Store.System().Get(); result.Err == nil {
func (s *Server) DoSecurityUpdateCheck() {
if *s.Config().ServiceSettings.EnableSecurityFixAlert {
if result := <-s.Store.System().Get(); result.Err == nil {
props := result.Data.(model.StringMap)
lastSecurityTime, _ := strconv.ParseInt(props[model.SYSTEM_LAST_SECURITY_TIME], 10, 0)
currentTime := model.GetMillis()
@@ -43,10 +44,10 @@ func (a *App) DoSecurityUpdateCheck() {
v := url.Values{}
v.Set(PROP_SECURITY_ID, a.DiagnosticId())
v.Set(PROP_SECURITY_ID, s.diagnosticId)
v.Set(PROP_SECURITY_BUILD, model.CurrentVersion+"."+model.BuildNumber)
v.Set(PROP_SECURITY_ENTERPRISE_READY, model.BuildEnterpriseReady)
v.Set(PROP_SECURITY_DATABASE, *a.Config().SqlSettings.DriverName)
v.Set(PROP_SECURITY_DATABASE, *s.Config().SqlSettings.DriverName)
v.Set(PROP_SECURITY_OS, runtime.GOOS)
if len(props[model.SYSTEM_RAN_UNIT_TESTS]) > 0 {
@@ -57,20 +58,20 @@ func (a *App) DoSecurityUpdateCheck() {
systemSecurityLastTime := &model.System{Name: model.SYSTEM_LAST_SECURITY_TIME, Value: strconv.FormatInt(currentTime, 10)}
if lastSecurityTime == 0 {
<-a.Srv.Store.System().Save(systemSecurityLastTime)
<-s.Store.System().Save(systemSecurityLastTime)
} else {
<-a.Srv.Store.System().Update(systemSecurityLastTime)
<-s.Store.System().Update(systemSecurityLastTime)
}
if ucr := <-a.Srv.Store.User().GetTotalUsersCount(); ucr.Err == nil {
if ucr := <-s.Store.User().GetTotalUsersCount(); ucr.Err == nil {
v.Set(PROP_SECURITY_USER_COUNT, strconv.FormatInt(ucr.Data.(int64), 10))
}
if ucr := <-a.Srv.Store.Status().GetTotalActiveUsersCount(); ucr.Err == nil {
if ucr := <-s.Store.Status().GetTotalActiveUsersCount(); ucr.Err == nil {
v.Set(PROP_SECURITY_ACTIVE_USER_COUNT, strconv.FormatInt(ucr.Data.(int64), 10))
}
if tcr := <-a.Srv.Store.Team().AnalyticsTeamCount(); tcr.Err == nil {
if tcr := <-s.Store.Team().AnalyticsTeamCount(); tcr.Err == nil {
v.Set(PROP_SECURITY_TEAM_COUNT, strconv.FormatInt(tcr.Data.(int64), 10))
}
@@ -86,7 +87,7 @@ func (a *App) DoSecurityUpdateCheck() {
for _, bulletin := range bulletins {
if bulletin.AppliesToVersion == model.CurrentVersion {
if props["SecurityBulletin_"+bulletin.Id] == "" {
results := <-a.Srv.Store.User().GetSystemAdminProfiles()
results := <-s.Store.User().GetSystemAdminProfiles()
if results.Err != nil {
mlog.Error("Failed to get system admins for security update information from Mattermost.")
return
@@ -108,11 +109,12 @@ func (a *App) DoSecurityUpdateCheck() {
for _, user := range users {
mlog.Info(fmt.Sprintf("Sending security bulletin for %v to %v", bulletin.Id, user.Email))
a.SendMail(user.Email, utils.T("mattermost.bulletin.subject"), string(body))
license := s.License()
mailservice.SendMailUsingConfig(user.Email, utils.T("mattermost.bulletin.subject"), string(body), s.Config(), license != nil && *license.Features.Compliance)
}
bulletinSeen := &model.System{Name: "SecurityBulletin_" + bulletin.Id, Value: bulletin.Id}
<-a.Srv.Store.System().Save(bulletinSeen)
<-s.Store.System().Save(bulletinSeen)
}
}
}

Просмотреть файл

@@ -14,7 +14,6 @@ import (
"net/http"
"net/url"
"os"
"path"
"strings"
"sync"
"sync/atomic"
@@ -33,10 +32,10 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/plugin"
"github.com/mattermost/mattermost-server/services/httpservice"
"github.com/mattermost/mattermost-server/services/mailservice"
"github.com/mattermost/mattermost-server/services/timezones"
"github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/store/sqlstore"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
var MaxNotificationsPerChannelDefault int64 = 1000000
@@ -73,7 +72,8 @@ type Server struct {
PushNotificationsHub PushNotificationsHub
Jobs *jobs.JobServer
runjobs bool
Jobs *jobs.JobServer
config atomic.Value
envConfig map[string]interface{}
@@ -85,7 +85,7 @@ type Server struct {
clientLicenseValue atomic.Value
licenseListeners map[string]func()
timezones atomic.Value
timezones *timezones.Timezones
newStore func() store.Store
@@ -124,150 +124,6 @@ type Server struct {
Saml einterfaces.SamlInterface
}
// This is a bridge between the old and new initalization for the context refactor.
// It calls app layer initalization code that then turns around and acts on the server.
// Don't add anything new here, new initilization should be done in the server and
// performed in the NewServer function.
func (s *Server) RunOldAppInitalization() error {
a := s.FakeApp()
a.CreatePushNotificationsHub()
a.StartPushNotificationsHubWorkers()
if utils.T == nil {
if err := utils.TranslationsPreInit(); err != nil {
return errors.Wrapf(err, "unable to load Mattermost translation files")
}
}
model.AppErrorInit(utils.T)
a.LoadTimezones()
if err := utils.InitTranslations(a.Config().LocalizationSettings); err != nil {
return errors.Wrapf(err, "unable to load Mattermost translation files")
}
a.Srv.configListenerId = a.AddConfigListener(func(_, _ *model.Config) {
a.configOrLicenseListener()
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CONFIG_CHANGED, "", "", "", nil)
message.Add("config", a.ClientConfigWithComputed())
a.Srv.Go(func() {
a.Publish(message)
})
})
a.Srv.licenseListenerId = a.AddLicenseListener(func() {
a.configOrLicenseListener()
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_LICENSE_CHANGED, "", "", "", nil)
message.Add("license", a.GetSanitizedClientLicense())
a.Srv.Go(func() {
a.Publish(message)
})
})
if err := a.SetupInviteEmailRateLimiting(); err != nil {
return err
}
mlog.Info("Server is initializing...")
s.initEnterprise()
if a.Srv.newStore == nil {
a.Srv.newStore = func() store.Store {
return store.NewLayeredStore(sqlstore.NewSqlSupplier(a.Config().SqlSettings, a.Metrics), a.Metrics, a.Cluster)
}
}
if htmlTemplateWatcher, err := utils.NewHTMLTemplateWatcher("templates"); err != nil {
mlog.Error(fmt.Sprintf("Failed to parse server templates %v", err))
} else {
a.Srv.htmlTemplateWatcher = htmlTemplateWatcher
}
a.Srv.Store = a.Srv.newStore()
if err := a.ensureAsymmetricSigningKey(); err != nil {
return errors.Wrapf(err, "unable to ensure asymmetric signing key")
}
if err := a.ensureInstallationDate(); err != nil {
return errors.Wrapf(err, "unable to ensure installation date")
}
a.EnsureDiagnosticId()
a.regenerateClientConfig()
s.initJobs()
a.AddLicenseListener(func() {
s.initJobs()
})
a.Srv.clusterLeaderListenerId = a.AddClusterLeaderChangedListener(func() {
mlog.Info("Cluster leader changed. Determining if job schedulers should be running:", mlog.Bool("isLeader", a.IsLeader()))
a.Srv.Jobs.Schedulers.HandleClusterLeaderChange(a.IsLeader())
})
subpath, err := utils.GetSubpathFromConfig(a.Config())
if err != nil {
return errors.Wrap(err, "failed to parse SiteURL subpath")
}
a.Srv.Router = a.Srv.RootRouter.PathPrefix(subpath).Subrouter()
a.Srv.Router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}", a.ServePluginRequest)
a.Srv.Router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}/{anything:.*}", a.ServePluginRequest)
// If configured with a subpath, redirect 404s at the root back into the subpath.
if subpath != "/" {
a.Srv.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)
})
}
a.Srv.Router.NotFoundHandler = http.HandlerFunc(a.Handle404)
a.Srv.WebSocketRouter = &WebSocketRouter{
app: a,
handlers: make(map[string]webSocketHandler),
}
mailservice.TestConnection(a.Config())
if _, err := url.ParseRequestURI(*a.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 := a.FileBackend()
if appErr == nil {
appErr = backend.TestConnection()
}
if appErr != nil {
mlog.Error("Problem with file storage settings: " + appErr.Error())
}
if model.BuildEnterpriseReady == "true" {
a.LoadLicense()
}
a.DoAdvancedPermissionsMigration()
a.DoEmojisPermissionsMigration()
a.InitPostMetadata()
a.InitPlugins(*a.Config().PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory)
a.AddConfigListener(func(prevCfg, cfg *model.Config) {
if *cfg.PluginSettings.Enable {
a.InitPlugins(*cfg.PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory)
} else {
a.ShutDownPlugins()
}
})
return nil
}
func NewServer(options ...Option) (*Server, error) {
rootRouter := mux.NewRouter()
@@ -305,11 +161,21 @@ func NewServer(options ...Option) (*Server, error) {
s.HTTPService = httpservice.MakeHTTPService(s.FakeApp())
if utils.T == nil {
if err := utils.TranslationsPreInit(); err != nil {
return nil, errors.Wrapf(err, "unable to load Mattermost translation files")
}
}
err := s.RunOldAppInitalization()
if err != nil {
return nil, err
}
model.AppErrorInit(utils.T)
s.timezones = timezones.New("")
// Start email batching because it's not like the other jobs
s.InitEmailBatching()
s.AddConfigListener(func(_, _ *model.Config) {
@@ -320,7 +186,7 @@ func NewServer(options ...Option) (*Server, error) {
mlog.Info(fmt.Sprintf("Enterprise Enabled: %v", model.BuildEnterpriseReady))
pwd, _ := os.Getwd()
mlog.Info(fmt.Sprintf("Current working directory is %v", pwd))
mlog.Info(fmt.Sprintf("Loaded config file from %v", utils.FindConfigFile(s.configFile)))
mlog.Info(fmt.Sprintf("Loaded config file from %v", fileutils.FindConfigFile(s.configFile)))
license := s.License()
@@ -348,6 +214,50 @@ func NewServer(options ...Option) (*Server, error) {
mlog.Error(fmt.Sprint("Error to reset the server status.", result.Err.Error()))
}
if s.Cluster != nil {
s.FakeApp().RegisterAllClusterMessageHandlers()
s.Cluster.StartInterNodeCommunication()
}
if s.Metrics != nil {
s.Metrics.StartServer()
}
if s.Elasticsearch != nil {
s.StartElasticsearch()
}
s.initJobs()
if s.runjobs {
s.Go(func() {
runSecurityJob(s)
})
s.Go(func() {
runDiagnosticsJob(s)
})
s.Go(func() {
runSessionCleanupJob(s)
})
s.Go(func() {
runTokenCleanupJob(s)
})
s.Go(func() {
runCommandWebhookCleanupJob(s)
})
if complianceI := s.Compliance; complianceI != nil {
complianceI.StartComplianceDailyJob()
}
if *s.Config().JobSettings.RunJobs && s.Jobs != nil {
s.Jobs.StartWorkers()
}
if *s.Config().JobSettings.RunScheduler && s.Jobs != nil {
s.Jobs.StartSchedulers()
}
}
return s, nil
}
@@ -358,19 +268,6 @@ func (s *Server) AppOptions() []AppOption {
}
}
// 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
}
func (s *Server) StartServer() error {
return s.FakeApp().StartServer()
}
const TIME_TO_WAIT_FOR_CONNECTIONS_TO_CLOSE_ON_SERVER_SHUTDOWN = time.Second
func (s *Server) StopHTTPServer() {
@@ -395,15 +292,6 @@ func (s *Server) StopHTTPServer() {
}
}
func (s *Server) RunOldAppShutdown() {
a := s.FakeApp()
a.HubStop()
a.StopPushNotificationsHubWorkers()
a.ShutDownPlugins()
a.RemoveLicenseListener(s.licenseListenerId)
a.RemoveClusterLeaderChangedListener(s.clusterLeaderListenerId)
}
func (s *Server) Shutdown() error {
mlog.Info("Stopping Server...")
@@ -425,15 +313,23 @@ func (s *Server) Shutdown() error {
s.DisableConfigWatch()
if s.Cluster != nil {
s.Cluster.StopInterNodeCommunication()
}
if s.Metrics != nil {
s.Metrics.StopServer()
}
if s.Jobs != nil && s.runjobs {
s.Jobs.StopWorkers()
s.Jobs.StopSchedulers()
}
mlog.Info("Server stopped")
return nil
}
func (s *Server) License() *model.License {
license, _ := s.licenseValue.Load().(*model.License)
return license
}
// Go creates a goroutine, but maintains a record of it to ensure that execution completes before
// the server is shutdown.
func (s *Server) Go(f func()) {
@@ -493,14 +389,14 @@ func stripPort(hostport string) string {
return net.JoinHostPort(host, "443")
}
func (a *App) StartServer() error {
func (s *Server) Start() error {
mlog.Info("Starting Server...")
var handler http.Handler = a.Srv.RootRouter
if allowedOrigins := *a.Config().ServiceSettings.AllowCorsFrom; allowedOrigins != "" {
exposedCorsHeaders := *a.Config().ServiceSettings.CorsExposedHeaders
allowCredentials := *a.Config().ServiceSettings.CorsAllowCredentials
debug := *a.Config().ServiceSettings.CorsDebug
var handler http.Handler = s.RootRouter
if allowedOrigins := *s.Config().ServiceSettings.AllowCorsFrom; allowedOrigins != "" {
exposedCorsHeaders := *s.Config().ServiceSettings.CorsExposedHeaders
allowCredentials := *s.Config().ServiceSettings.CorsAllowCredentials
debug := *s.Config().ServiceSettings.CorsDebug
corsWrapper := cors.New(cors.Options{
AllowedOrigins: strings.Fields(allowedOrigins),
AllowedMethods: corsAllowedMethods,
@@ -513,34 +409,34 @@ func (a *App) StartServer() error {
// If we have debugging of CORS turned on then forward messages to logs
if debug {
corsWrapper.Log = a.Log.StdLog(mlog.String("source", "cors"))
corsWrapper.Log = s.Log.StdLog(mlog.String("source", "cors"))
}
handler = corsWrapper.Handler(handler)
}
if *a.Config().RateLimitSettings.Enable {
if *s.Config().RateLimitSettings.Enable {
mlog.Info("RateLimiter is enabled")
rateLimiter, err := NewRateLimiter(&a.Config().RateLimitSettings)
rateLimiter, err := NewRateLimiter(&s.Config().RateLimitSettings)
if err != nil {
return err
}
a.Srv.RateLimiter = rateLimiter
s.RateLimiter = rateLimiter
handler = rateLimiter.RateLimitHandler(handler)
}
a.Srv.Server = &http.Server{
s.Server = &http.Server{
Handler: handlers.RecoveryHandler(handlers.RecoveryLogger(&RecoveryLogger{}), handlers.PrintRecoveryStack(true))(handler),
ReadTimeout: time.Duration(*a.Config().ServiceSettings.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(*a.Config().ServiceSettings.WriteTimeout) * time.Second,
ErrorLog: a.Log.StdLog(mlog.String("source", "httpserver")),
ReadTimeout: time.Duration(*s.Config().ServiceSettings.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(*s.Config().ServiceSettings.WriteTimeout) * time.Second,
ErrorLog: s.Log.StdLog(mlog.String("source", "httpserver")),
}
addr := *a.Config().ServiceSettings.ListenAddress
addr := *s.Config().ServiceSettings.ListenAddress
if addr == "" {
if *a.Config().ServiceSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
if *s.Config().ServiceSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
addr = ":https"
} else {
addr = ":http"
@@ -552,23 +448,23 @@ func (a *App) StartServer() error {
errors.Wrapf(err, utils.T("api.server.start_server.starting.critical"), err)
return err
}
a.Srv.ListenAddr = listener.Addr().(*net.TCPAddr)
s.ListenAddr = listener.Addr().(*net.TCPAddr)
mlog.Info(fmt.Sprintf("Server is listening on %v", listener.Addr().String()))
// Migration from old let's encrypt library
if *a.Config().ServiceSettings.UseLetsEncrypt {
if stat, err := os.Stat(*a.Config().ServiceSettings.LetsEncryptCertificateCacheFile); err == nil && !stat.IsDir() {
os.Remove(*a.Config().ServiceSettings.LetsEncryptCertificateCacheFile)
if *s.Config().ServiceSettings.UseLetsEncrypt {
if stat, err := os.Stat(*s.Config().ServiceSettings.LetsEncryptCertificateCacheFile); err == nil && !stat.IsDir() {
os.Remove(*s.Config().ServiceSettings.LetsEncryptCertificateCacheFile)
}
}
m := &autocert.Manager{
Cache: autocert.DirCache(*a.Config().ServiceSettings.LetsEncryptCertificateCacheFile),
Cache: autocert.DirCache(*s.Config().ServiceSettings.LetsEncryptCertificateCacheFile),
Prompt: autocert.AcceptTOS,
}
if *a.Config().ServiceSettings.Forward80To443 {
if *s.Config().ServiceSettings.Forward80To443 {
if host, port, err := net.SplitHostPort(addr); err != nil {
mlog.Error("Unable to setup forwarding: " + err.Error())
} else if port != "443" {
@@ -576,11 +472,11 @@ func (a *App) StartServer() error {
} else {
httpListenAddress := net.JoinHostPort(host, "http")
if *a.Config().ServiceSettings.UseLetsEncrypt {
if *s.Config().ServiceSettings.UseLetsEncrypt {
server := &http.Server{
Addr: httpListenAddress,
Handler: m.HTTPHandler(nil),
ErrorLog: a.Log.StdLog(mlog.String("source", "le_forwarder_server")),
ErrorLog: s.Log.StdLog(mlog.String("source", "le_forwarder_server")),
}
go server.ListenAndServe()
} else {
@@ -594,27 +490,27 @@ func (a *App) StartServer() error {
server := &http.Server{
Handler: http.HandlerFunc(handleHTTPRedirect),
ErrorLog: a.Log.StdLog(mlog.String("source", "forwarder_server")),
ErrorLog: s.Log.StdLog(mlog.String("source", "forwarder_server")),
}
server.Serve(redirectListener)
}()
}
}
} else if *a.Config().ServiceSettings.UseLetsEncrypt {
} else if *s.Config().ServiceSettings.UseLetsEncrypt {
return errors.New(utils.T("api.server.start_server.forward80to443.disabled_while_using_lets_encrypt"))
}
a.Srv.didFinishListen = make(chan struct{})
s.didFinishListen = make(chan struct{})
go func() {
var err error
if *a.Config().ServiceSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
if *s.Config().ServiceSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
tlsConfig := &tls.Config{
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
}
switch *a.Config().ServiceSettings.TLSMinVer {
switch *s.Config().ServiceSettings.TLSMinVer {
case "1.0":
tlsConfig.MinVersion = tls.VersionTLS10
case "1.1":
@@ -632,11 +528,11 @@ func (a *App) StartServer() error {
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
}
if len(a.Config().ServiceSettings.TLSOverwriteCiphers) == 0 {
if len(s.Config().ServiceSettings.TLSOverwriteCiphers) == 0 {
tlsConfig.CipherSuites = defaultCiphers
} else {
var cipherSuites []uint16
for _, cipher := range a.Config().ServiceSettings.TLSOverwriteCiphers {
for _, cipher := range s.Config().ServiceSettings.TLSOverwriteCiphers {
value, ok := model.ServerTLSSupportedCiphers[cipher]
if !ok {
@@ -658,18 +554,18 @@ func (a *App) StartServer() error {
certFile := ""
keyFile := ""
if *a.Config().ServiceSettings.UseLetsEncrypt {
if *s.Config().ServiceSettings.UseLetsEncrypt {
tlsConfig.GetCertificate = m.GetCertificate
tlsConfig.NextProtos = append(tlsConfig.NextProtos, "h2")
} else {
certFile = *a.Config().ServiceSettings.TLSCertFile
keyFile = *a.Config().ServiceSettings.TLSKeyFile
certFile = *s.Config().ServiceSettings.TLSCertFile
keyFile = *s.Config().ServiceSettings.TLSKeyFile
}
a.Srv.Server.TLSConfig = tlsConfig
err = a.Srv.Server.ServeTLS(listener, certFile, keyFile)
s.Server.TLSConfig = tlsConfig
err = s.Server.ServeTLS(listener, certFile, keyFile)
} else {
err = a.Srv.Server.Serve(listener)
err = s.Server.Serve(listener)
}
if err != nil && err != http.ErrServerClosed {
@@ -677,7 +573,7 @@ func (a *App) StartServer() error {
time.Sleep(time.Second)
}
close(a.Srv.didFinishListen)
close(s.didFinishListen)
}()
return nil
@@ -705,3 +601,115 @@ func consumeAndClose(r *http.Response) {
r.Body.Close()
}
}
func runSecurityJob(s *Server) {
doSecurity(s)
model.CreateRecurringTask("Security", func() {
doSecurity(s)
}, time.Hour*4)
}
func runDiagnosticsJob(s *Server) {
doDiagnostics(s)
model.CreateRecurringTask("Diagnostics", func() {
doDiagnostics(s)
}, time.Hour*24)
}
func runTokenCleanupJob(s *Server) {
doTokenCleanup(s)
model.CreateRecurringTask("Token Cleanup", func() {
doTokenCleanup(s)
}, time.Hour*1)
}
func runCommandWebhookCleanupJob(s *Server) {
doCommandWebhookCleanup(s)
model.CreateRecurringTask("Command Hook Cleanup", func() {
doCommandWebhookCleanup(s)
}, time.Hour*1)
}
func runSessionCleanupJob(s *Server) {
doSessionCleanup(s)
model.CreateRecurringTask("Session Cleanup", func() {
doSessionCleanup(s)
}, time.Hour*24)
}
func doSecurity(s *Server) {
s.DoSecurityUpdateCheck()
}
func doDiagnostics(s *Server) {
if *s.Config().LogSettings.EnableDiagnostics {
s.FakeApp().SendDailyDiagnostics()
}
}
func doTokenCleanup(s *Server) {
s.Store.Token().Cleanup()
}
func doCommandWebhookCleanup(s *Server) {
s.Store.CommandWebhook().Cleanup()
}
const (
SESSIONS_CLEANUP_BATCH_SIZE = 1000
)
func doSessionCleanup(s *Server) {
s.Store.Session().Cleanup(model.GetMillis(), SESSIONS_CLEANUP_BATCH_SIZE)
}
func (s *Server) StartElasticsearch() {
s.Go(func() {
if err := s.Elasticsearch.Start(); err != nil {
s.Log.Error(err.Error())
}
})
s.AddConfigListener(func(oldConfig *model.Config, newConfig *model.Config) {
if !*oldConfig.ElasticsearchSettings.EnableIndexing && *newConfig.ElasticsearchSettings.EnableIndexing {
s.Go(func() {
if err := s.Elasticsearch.Start(); err != nil {
mlog.Error(err.Error())
}
})
} else if *oldConfig.ElasticsearchSettings.EnableIndexing && !*newConfig.ElasticsearchSettings.EnableIndexing {
s.Go(func() {
if err := s.Elasticsearch.Stop(); err != nil {
mlog.Error(err.Error())
}
})
} else if *oldConfig.ElasticsearchSettings.Password != *newConfig.ElasticsearchSettings.Password || *oldConfig.ElasticsearchSettings.Username != *newConfig.ElasticsearchSettings.Username || *oldConfig.ElasticsearchSettings.ConnectionUrl != *newConfig.ElasticsearchSettings.ConnectionUrl || *oldConfig.ElasticsearchSettings.Sniff != *newConfig.ElasticsearchSettings.Sniff {
s.Go(func() {
if *oldConfig.ElasticsearchSettings.EnableIndexing {
if err := s.Elasticsearch.Stop(); err != nil {
mlog.Error(err.Error())
}
if err := s.Elasticsearch.Start(); err != nil {
mlog.Error(err.Error())
}
}
})
}
})
s.AddLicenseListener(func() {
if s.License() != nil {
s.Go(func() {
if err := s.Elasticsearch.Start(); err != nil {
mlog.Error(err.Error())
}
})
} else {
s.Go(func() {
if err := s.Elasticsearch.Stop(); err != nil {
mlog.Error(err.Error())
}
})
}
})
}

169
app/server_app_adapters.go Обычный файл
Просмотреть файл

@@ -0,0 +1,169 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"fmt"
"net/http"
"net/url"
"path"
"github.com/mattermost/mattermost-server/mlog"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/services/mailservice"
"github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/store/sqlstore"
"github.com/mattermost/mattermost-server/utils"
"github.com/pkg/errors"
)
// This is a bridge between the old and new initalization for the context refactor.
// It calls app layer initalization code that then turns around and acts on the server.
// Don't add anything new here, new initilization should be done in the server and
// performed in the NewServer function.
func (s *Server) RunOldAppInitalization() error {
a := s.FakeApp()
a.CreatePushNotificationsHub()
a.StartPushNotificationsHubWorkers()
if err := utils.InitTranslations(a.Config().LocalizationSettings); err != nil {
return errors.Wrapf(err, "unable to load Mattermost translation files")
}
a.Srv.configListenerId = a.AddConfigListener(func(_, _ *model.Config) {
a.configOrLicenseListener()
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_CONFIG_CHANGED, "", "", "", nil)
message.Add("config", a.ClientConfigWithComputed())
a.Srv.Go(func() {
a.Publish(message)
})
})
a.Srv.licenseListenerId = a.AddLicenseListener(func() {
a.configOrLicenseListener()
message := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_LICENSE_CHANGED, "", "", "", nil)
message.Add("license", a.GetSanitizedClientLicense())
a.Srv.Go(func() {
a.Publish(message)
})
})
if err := a.SetupInviteEmailRateLimiting(); err != nil {
return err
}
mlog.Info("Server is initializing...")
s.initEnterprise()
if a.Srv.newStore == nil {
a.Srv.newStore = func() store.Store {
return store.NewLayeredStore(sqlstore.NewSqlSupplier(a.Config().SqlSettings, a.Metrics), a.Metrics, a.Cluster)
}
}
if htmlTemplateWatcher, err := utils.NewHTMLTemplateWatcher("templates"); err != nil {
mlog.Error(fmt.Sprintf("Failed to parse server templates %v", err))
} else {
a.Srv.htmlTemplateWatcher = htmlTemplateWatcher
}
a.Srv.Store = a.Srv.newStore()
if err := a.ensureAsymmetricSigningKey(); err != nil {
return errors.Wrapf(err, "unable to ensure asymmetric signing key")
}
if err := a.ensureInstallationDate(); err != nil {
return errors.Wrapf(err, "unable to ensure installation date")
}
a.EnsureDiagnosticId()
a.regenerateClientConfig()
a.Srv.clusterLeaderListenerId = a.AddClusterLeaderChangedListener(func() {
mlog.Info("Cluster leader changed. Determining if job schedulers should be running:", mlog.Bool("isLeader", a.IsLeader()))
if a.Srv.Jobs != nil {
a.Srv.Jobs.Schedulers.HandleClusterLeaderChange(a.IsLeader())
}
})
subpath, err := utils.GetSubpathFromConfig(a.Config())
if err != nil {
return errors.Wrap(err, "failed to parse SiteURL subpath")
}
a.Srv.Router = a.Srv.RootRouter.PathPrefix(subpath).Subrouter()
a.Srv.Router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}", a.ServePluginRequest)
a.Srv.Router.HandleFunc("/plugins/{plugin_id:[A-Za-z0-9\\_\\-\\.]+}/{anything:.*}", a.ServePluginRequest)
// If configured with a subpath, redirect 404s at the root back into the subpath.
if subpath != "/" {
a.Srv.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)
})
}
a.Srv.Router.NotFoundHandler = http.HandlerFunc(a.Handle404)
a.Srv.WebSocketRouter = &WebSocketRouter{
app: a,
handlers: make(map[string]webSocketHandler),
}
mailservice.TestConnection(a.Config())
if _, err := url.ParseRequestURI(*a.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 := a.FileBackend()
if appErr == nil {
appErr = backend.TestConnection()
}
if appErr != nil {
mlog.Error("Problem with file storage settings: " + appErr.Error())
}
if model.BuildEnterpriseReady == "true" {
a.LoadLicense()
}
a.DoAdvancedPermissionsMigration()
a.DoEmojisPermissionsMigration()
a.InitPostMetadata()
a.InitPlugins(*a.Config().PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory)
a.AddConfigListener(func(prevCfg, cfg *model.Config) {
if *cfg.PluginSettings.Enable {
a.InitPlugins(*cfg.PluginSettings.Directory, *a.Config().PluginSettings.ClientDirectory)
} else {
a.ShutDownPlugins()
}
})
return nil
}
func (s *Server) RunOldAppShutdown() {
a := s.FakeApp()
a.HubStop()
a.StopPushNotificationsHubWorkers()
a.ShutDownPlugins()
a.RemoveLicenseListener(s.licenseListenerId)
a.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
}

11
app/server_license.go Обычный файл
Просмотреть файл

@@ -0,0 +1,11 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import "github.com/mattermost/mattermost-server/model"
func (s *Server) License() *model.License {
license, _ := s.licenseValue.Load().(*model.License)
return license
}

Просмотреть файл

@@ -12,9 +12,8 @@ import (
"strings"
"testing"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils/fileutils"
"github.com/stretchr/testify/require"
)
@@ -23,7 +22,7 @@ func TestStartServerSuccess(t *testing.T) {
require.NoError(t, err)
s.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.ListenAddress = ":0" })
serverErr := s.StartServer()
serverErr := s.Start()
client := &http.Client{}
checkEndpoint(t, client, "http://localhost:"+strconv.Itoa(s.ListenAddr.Port)+"/", http.StatusNotFound)
@@ -42,7 +41,7 @@ func TestStartServerRateLimiterCriticalError(t *testing.T) {
*cfg.RateLimitSettings.MaxBurst = -100
})
serverErr := s.StartServer()
serverErr := s.Start()
s.Shutdown()
require.Error(t, serverErr)
}
@@ -60,7 +59,7 @@ func TestStartServerPortUnavailable(t *testing.T) {
*cfg.ServiceSettings.ListenAddress = listener.Addr().String()
})
serverErr := s.StartServer()
serverErr := s.Start()
s.Shutdown()
require.Error(t, serverErr)
}
@@ -69,14 +68,14 @@ func TestStartServerTLSSuccess(t *testing.T) {
s, err := NewServer()
require.NoError(t, err)
testDir, _ := utils.FindDir("tests")
testDir, _ := fileutils.FindDir("tests")
s.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.ListenAddress = ":0"
*cfg.ServiceSettings.ConnectionSecurity = "TLS"
*cfg.ServiceSettings.TLSKeyFile = path.Join(testDir, "tls_test_key.pem")
*cfg.ServiceSettings.TLSCertFile = path.Join(testDir, "tls_test_cert.pem")
})
serverErr := s.StartServer()
serverErr := s.Start()
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@@ -93,7 +92,7 @@ func TestStartServerTLSVersion(t *testing.T) {
s, err := NewServer()
require.NoError(t, err)
testDir, _ := utils.FindDir("tests")
testDir, _ := fileutils.FindDir("tests")
s.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.ListenAddress = ":0"
*cfg.ServiceSettings.ConnectionSecurity = "TLS"
@@ -101,7 +100,7 @@ func TestStartServerTLSVersion(t *testing.T) {
*cfg.ServiceSettings.TLSKeyFile = path.Join(testDir, "tls_test_key.pem")
*cfg.ServiceSettings.TLSCertFile = path.Join(testDir, "tls_test_cert.pem")
})
serverErr := s.StartServer()
serverErr := s.Start()
tr := &http.Transport{
TLSClientConfig: &tls.Config{
@@ -137,7 +136,7 @@ func TestStartServerTLSOverwriteCipher(t *testing.T) {
s, err := NewServer()
require.NoError(t, err)
testDir, _ := utils.FindDir("tests")
testDir, _ := fileutils.FindDir("tests")
s.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.ListenAddress = ":0"
*cfg.ServiceSettings.ConnectionSecurity = "TLS"
@@ -148,7 +147,7 @@ func TestStartServerTLSOverwriteCipher(t *testing.T) {
*cfg.ServiceSettings.TLSKeyFile = path.Join(testDir, "tls_test_key.pem")
*cfg.ServiceSettings.TLSCertFile = path.Join(testDir, "tls_test_cert.pem")
})
serverErr := s.StartServer()
serverErr := s.Start()
tr := &http.Transport{
TLSClientConfig: &tls.Config{

Просмотреть файл

@@ -1,28 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils"
)
func (a *App) Timezones() model.SupportedTimezones {
if cfg := a.Srv.timezones.Load(); cfg != nil {
return cfg.(model.SupportedTimezones)
}
return model.SupportedTimezones{}
}
func (a *App) LoadTimezones() {
timezonePath := "timezones.json"
if a.Config().TimezoneSettings.SupportedTimezonesPath != nil && len(*a.Config().TimezoneSettings.SupportedTimezonesPath) > 0 {
timezonePath = *a.Config().TimezoneSettings.SupportedTimezonesPath
}
timezoneCfg := utils.LoadTimezones(timezonePath)
a.Srv.timezones.Store(timezoneCfg)
}

Просмотреть файл

@@ -30,6 +30,7 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/services/mfa"
"github.com/mattermost/mattermost-server/utils"
"github.com/mattermost/mattermost-server/utils/fileutils"
)
const (
@@ -707,7 +708,7 @@ func getFont(initialFont string) (*truetype.Font, error) {
initialFont = "nunito-bold.ttf"
}
fontDir, _ := utils.FindDir("fonts")
fontDir, _ := fileutils.FindDir("fonts")
fontBytes, err := ioutil.ReadFile(filepath.Join(fontDir, initialFont))
if err != nil {
return nil, err