github.com/levb/mattermost-server@v5.3.1+incompatible/web/static.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package web
     5  
     6  import (
     7  	"fmt"
     8  	"mime"
     9  	"net/http"
    10  	"path"
    11  	"path/filepath"
    12  	"strings"
    13  
    14  	"github.com/NYTimes/gziphandler"
    15  
    16  	"github.com/mattermost/mattermost-server/mlog"
    17  	"github.com/mattermost/mattermost-server/model"
    18  	"github.com/mattermost/mattermost-server/utils"
    19  )
    20  
    21  func (w *Web) InitStatic() {
    22  	if *w.App.Config().ServiceSettings.WebserverMode != "disabled" {
    23  		utils.UpdateAssetsSubpathFromConfig(w.App.Config())
    24  
    25  		staticDir, _ := utils.FindDir(model.CLIENT_DIR)
    26  		mlog.Debug(fmt.Sprintf("Using client directory at %v", staticDir))
    27  
    28  		subpath, _ := utils.GetSubpathFromConfig(w.App.Config())
    29  
    30  		mime.AddExtensionType(".wasm", "application/wasm")
    31  
    32  		staticHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static"), http.FileServer(http.Dir(staticDir))))
    33  		pluginHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static", "plugins"), http.FileServer(http.Dir(*w.App.Config().PluginSettings.ClientDirectory))))
    34  
    35  		if *w.App.Config().ServiceSettings.WebserverMode == "gzip" {
    36  			staticHandler = gziphandler.GzipHandler(staticHandler)
    37  			pluginHandler = gziphandler.GzipHandler(pluginHandler)
    38  		}
    39  
    40  		w.MainRouter.PathPrefix("/static/plugins/").Handler(pluginHandler)
    41  		w.MainRouter.PathPrefix("/static/").Handler(staticHandler)
    42  		w.MainRouter.Handle("/{anything:.*}", w.NewStaticHandler(root)).Methods("GET")
    43  
    44  		// When a subpath is defined, it's necessary to handle redirects without a
    45  		// trailing slash. We don't want to use StrictSlash on the w.MainRouter and affect
    46  		// all routes, just /subpath -> /subpath/.
    47  		w.MainRouter.HandleFunc("", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    48  			http.Redirect(w, r, r.URL.String()+"/", http.StatusFound)
    49  		}))
    50  	}
    51  }
    52  
    53  func root(c *Context, w http.ResponseWriter, r *http.Request) {
    54  
    55  	if !CheckClientCompatability(r.UserAgent()) {
    56  		w.Header().Set("Cache-Control", "no-store")
    57  		page := utils.NewHTMLTemplate(c.App.HTMLTemplates(), "unsupported_browser")
    58  		page.Props["Title"] = c.T("web.error.unsupported_browser.title")
    59  		page.Props["Message"] = c.T("web.error.unsupported_browser.message")
    60  		page.RenderToWriter(w)
    61  		return
    62  	}
    63  
    64  	if IsApiCall(c.App, r) {
    65  		Handle404(c.App, w, r)
    66  		return
    67  	}
    68  
    69  	w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public")
    70  
    71  	staticDir, _ := utils.FindDir(model.CLIENT_DIR)
    72  	http.ServeFile(w, r, filepath.Join(staticDir, "root.html"))
    73  }
    74  
    75  func staticFilesHandler(handler http.Handler) http.Handler {
    76  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    77  		w.Header().Set("Cache-Control", "max-age=31556926, public")
    78  		if strings.HasSuffix(r.URL.Path, "/") {
    79  			http.NotFound(w, r)
    80  			return
    81  		}
    82  		handler.ServeHTTP(w, r)
    83  	})
    84  }