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