github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+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  	"net/http"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/NYTimes/gziphandler"
    13  
    14  	"github.com/mattermost/mattermost-server/mlog"
    15  	"github.com/mattermost/mattermost-server/model"
    16  	"github.com/mattermost/mattermost-server/utils"
    17  )
    18  
    19  func (w *Web) InitStatic() {
    20  	if *w.App.Config().ServiceSettings.WebserverMode != "disabled" {
    21  		staticDir, _ := utils.FindDir(model.CLIENT_DIR)
    22  		mlog.Debug(fmt.Sprintf("Using client directory at %v", staticDir))
    23  
    24  		staticHandler := staticHandler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir))))
    25  		pluginHandler := pluginHandler(w.App.Config, http.StripPrefix("/static/plugins/", http.FileServer(http.Dir(*w.App.Config().PluginSettings.ClientDirectory))))
    26  
    27  		if *w.App.Config().ServiceSettings.WebserverMode == "gzip" {
    28  			staticHandler = gziphandler.GzipHandler(staticHandler)
    29  			pluginHandler = gziphandler.GzipHandler(pluginHandler)
    30  		}
    31  
    32  		w.MainRouter.PathPrefix("/static/plugins/").Handler(pluginHandler)
    33  		w.MainRouter.PathPrefix("/static/").Handler(staticHandler)
    34  		w.MainRouter.Handle("/{anything:.*}", w.NewStaticHandler(root)).Methods("GET")
    35  	}
    36  }
    37  
    38  func root(c *Context, w http.ResponseWriter, r *http.Request) {
    39  
    40  	if !CheckClientCompatability(r.UserAgent()) {
    41  		w.Header().Set("Cache-Control", "no-store")
    42  		page := utils.NewHTMLTemplate(c.App.HTMLTemplates(), "unsupported_browser")
    43  		page.Props["Title"] = c.T("web.error.unsupported_browser.title")
    44  		page.Props["Message"] = c.T("web.error.unsupported_browser.message")
    45  		page.RenderToWriter(w)
    46  		return
    47  	}
    48  
    49  	if IsApiCall(r) {
    50  		Handle404(c.App, w, r)
    51  		return
    52  	}
    53  
    54  	w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public")
    55  
    56  	staticDir, _ := utils.FindDir(model.CLIENT_DIR)
    57  	http.ServeFile(w, r, filepath.Join(staticDir, "root.html"))
    58  }
    59  
    60  func staticHandler(handler http.Handler) http.Handler {
    61  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    62  		w.Header().Set("Cache-Control", "max-age=31556926, public")
    63  		if strings.HasSuffix(r.URL.Path, "/") {
    64  			http.NotFound(w, r)
    65  			return
    66  		}
    67  		handler.ServeHTTP(w, r)
    68  	})
    69  }
    70  
    71  func pluginHandler(config model.ConfigFunc, handler http.Handler) http.Handler {
    72  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    73  		if *config().ServiceSettings.EnableDeveloper {
    74  			w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
    75  		} else {
    76  			w.Header().Set("Cache-Control", "max-age=31556926, public")
    77  		}
    78  		if strings.HasSuffix(r.URL.Path, "/") {
    79  			http.NotFound(w, r)
    80  			return
    81  		}
    82  		handler.ServeHTTP(w, r)
    83  	})
    84  }