github.com/jfrerich/mattermost-server@v5.8.0-rc2+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  	"github.com/mattermost/mattermost-server/utils/fileutils"
    20  )
    21  
    22  var robotsTxt = []byte("User-agent: *\nDisallow: /\n")
    23  
    24  func (w *Web) InitStatic() {
    25  	if *w.ConfigService.Config().ServiceSettings.WebserverMode != "disabled" {
    26  		utils.UpdateAssetsSubpathFromConfig(w.ConfigService.Config())
    27  
    28  		staticDir, _ := fileutils.FindDir(model.CLIENT_DIR)
    29  		mlog.Debug(fmt.Sprintf("Using client directory at %v", staticDir))
    30  
    31  		subpath, _ := utils.GetSubpathFromConfig(w.ConfigService.Config())
    32  
    33  		mime.AddExtensionType(".wasm", "application/wasm")
    34  
    35  		staticHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static"), http.FileServer(http.Dir(staticDir))))
    36  		pluginHandler := staticFilesHandler(http.StripPrefix(path.Join(subpath, "static", "plugins"), http.FileServer(http.Dir(*w.ConfigService.Config().PluginSettings.ClientDirectory))))
    37  
    38  		if *w.ConfigService.Config().ServiceSettings.WebserverMode == "gzip" {
    39  			staticHandler = gziphandler.GzipHandler(staticHandler)
    40  			pluginHandler = gziphandler.GzipHandler(pluginHandler)
    41  		}
    42  
    43  		w.MainRouter.PathPrefix("/static/plugins/").Handler(pluginHandler)
    44  		w.MainRouter.PathPrefix("/static/").Handler(staticHandler)
    45  		w.MainRouter.Handle("/robots.txt", http.HandlerFunc(robotsHandler))
    46  		w.MainRouter.Handle("/{anything:.*}", w.NewStaticHandler(root)).Methods("GET")
    47  
    48  		// When a subpath is defined, it's necessary to handle redirects without a
    49  		// trailing slash. We don't want to use StrictSlash on the w.MainRouter and affect
    50  		// all routes, just /subpath -> /subpath/.
    51  		w.MainRouter.HandleFunc("", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    52  			r.URL.Path += "/"
    53  			http.Redirect(w, r, r.URL.String(), http.StatusFound)
    54  		}))
    55  	}
    56  }
    57  
    58  func root(c *Context, w http.ResponseWriter, r *http.Request) {
    59  
    60  	if !CheckClientCompatability(r.UserAgent()) {
    61  		w.Header().Set("Cache-Control", "no-store")
    62  		page := utils.NewHTMLTemplate(c.App.HTMLTemplates(), "unsupported_browser")
    63  		page.Props["Title"] = c.App.T("web.error.unsupported_browser.title")
    64  		page.Props["Message"] = c.App.T("web.error.unsupported_browser.message")
    65  		page.RenderToWriter(w)
    66  		return
    67  	}
    68  
    69  	if IsApiCall(c.App, r) {
    70  		Handle404(c.App, w, r)
    71  		return
    72  	}
    73  
    74  	w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public")
    75  
    76  	staticDir, _ := fileutils.FindDir(model.CLIENT_DIR)
    77  	http.ServeFile(w, r, filepath.Join(staticDir, "root.html"))
    78  }
    79  
    80  func staticFilesHandler(handler http.Handler) http.Handler {
    81  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    82  		w.Header().Set("Cache-Control", "max-age=31556926, public")
    83  		if strings.HasSuffix(r.URL.Path, "/") {
    84  			http.NotFound(w, r)
    85  			return
    86  		}
    87  		handler.ServeHTTP(w, r)
    88  	})
    89  }
    90  
    91  func robotsHandler(w http.ResponseWriter, r *http.Request) {
    92  	if strings.HasSuffix(r.URL.Path, "/") {
    93  		http.NotFound(w, r)
    94  		return
    95  	}
    96  	w.Write(robotsTxt)
    97  }