From f14b0097dda3d51e4bae9823b2df2635f804fe2b Mon Sep 17 00:00:00 2001 From: Ben Schumacher Date: Wed, 21 Apr 2021 20:35:57 +0200 Subject: [PATCH] [MM-34798] Respect MM_SERVER_PATH when looking for templates (#17410) --- app/server.go | 3 +-- shared/templates/templates.go | 11 +++++++++++ web/static.go | 3 ++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/app/server.go b/app/server.go index 583cc08708..1c279ec92b 100644 --- a/app/server.go +++ b/app/server.go @@ -67,7 +67,6 @@ import ( "github.com/mattermost/mattermost-server/v5/store/sqlstore" "github.com/mattermost/mattermost-server/v5/store/timerlayer" "github.com/mattermost/mattermost-server/v5/utils" - "github.com/mattermost/mattermost-server/v5/utils/fileutils" ) var MaxNotificationsPerChannelDefault int64 = 1000000 @@ -384,7 +383,7 @@ func NewServer(options ...Option) (*Server, error) { } } - templatesDir, ok := fileutils.FindDir("templates") + templatesDir, ok := templates.GetTemplateDirectory() if !ok { mlog.Error("Failed find server templates", mlog.String("directory", "templates")) } else { diff --git a/shared/templates/templates.go b/shared/templates/templates.go index 12ec85b766..9d1581af7b 100644 --- a/shared/templates/templates.go +++ b/shared/templates/templates.go @@ -7,10 +7,12 @@ import ( "bytes" "html/template" "io" + "os" "path/filepath" "sync" "github.com/fsnotify/fsnotify" + "github.com/mattermost/mattermost-server/v5/utils/fileutils" ) // Container represents a set of templates that can be render @@ -29,6 +31,15 @@ type Data struct { HTML map[string]template.HTML } +func GetTemplateDirectory() (string, bool) { + templatesDir := "templates" + if mattermostPath := os.Getenv("MM_SERVER_PATH"); mattermostPath != "" { + templatesDir = filepath.Join(mattermostPath, templatesDir) + } + + return fileutils.FindDir(templatesDir) +} + // NewFromTemplates creates a new templates container using a // `template.Template` object func NewFromTemplate(templates *template.Template) *Container { diff --git a/web/static.go b/web/static.go index 7cf725c3c6..d2bfd6eb09 100644 --- a/web/static.go +++ b/web/static.go @@ -13,6 +13,7 @@ import ( "github.com/mattermost/mattermost-server/v5/model" "github.com/mattermost/mattermost-server/v5/shared/mlog" + "github.com/mattermost/mattermost-server/v5/shared/templates" "github.com/mattermost/mattermost-server/v5/utils" "github.com/mattermost/mattermost-server/v5/utils/fileutils" ) @@ -114,6 +115,6 @@ func unsupportedBrowserScriptHandler(w http.ResponseWriter, r *http.Request) { return } - templatesDir, _ := fileutils.FindDir("templates") + templatesDir, _ := templates.GetTemplateDirectory() http.ServeFile(w, r, filepath.Join(templatesDir, "unsupported_browser.js")) }