diff options
Diffstat (limited to 'web/web.go')
-rw-r--r-- | web/web.go | 96 |
1 files changed, 34 insertions, 62 deletions
diff --git a/web/web.go b/web/web.go index 56a5ab6ac..53276953e 100644 --- a/web/web.go +++ b/web/web.go @@ -6,69 +6,39 @@ package web import ( "fmt" "net/http" - "path/filepath" "strings" - "github.com/NYTimes/gziphandler" "github.com/avct/uasurfer" + "github.com/gorilla/mux" - "github.com/mattermost/mattermost-server/api" + "github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/mlog" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/utils" ) -func Init(api3 *api.API) { - mlog.Debug("Initializing web routes") - - mainrouter := api3.BaseRoutes.Root - - if *api3.App.Config().ServiceSettings.WebserverMode != "disabled" { - staticDir, _ := utils.FindDir(model.CLIENT_DIR) - mlog.Debug(fmt.Sprintf("Using client directory at %v", staticDir)) - - staticHandler := staticHandler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticDir)))) - pluginHandler := pluginHandler(api3.App.Config, http.StripPrefix("/static/plugins/", http.FileServer(http.Dir(*api3.App.Config().PluginSettings.ClientDirectory)))) +type Web struct { + App *app.App + MainRouter *mux.Router +} - if *api3.App.Config().ServiceSettings.WebserverMode == "gzip" { - staticHandler = gziphandler.GzipHandler(staticHandler) - pluginHandler = gziphandler.GzipHandler(pluginHandler) - } +func NewWeb(a *app.App, root *mux.Router) *Web { + mlog.Debug("Initializing web routes") - mainrouter.PathPrefix("/static/plugins/").Handler(pluginHandler) - mainrouter.PathPrefix("/static/").Handler(staticHandler) - mainrouter.Handle("/{anything:.*}", api3.AppHandlerIndependent(root)).Methods("GET") + web := &Web{ + App: a, + MainRouter: root, } -} -func staticHandler(handler http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Cache-Control", "max-age=31556926, public") - if strings.HasSuffix(r.URL.Path, "/") { - http.NotFound(w, r) - return - } - handler.ServeHTTP(w, r) - }) -} + web.InitStatic() + web.InitWebhooks() + web.InitSaml() -func pluginHandler(config model.ConfigFunc, handler http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if *config().ServiceSettings.EnableDeveloper { - w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") - } else { - w.Header().Set("Cache-Control", "max-age=31556926, public") - } - if strings.HasSuffix(r.URL.Path, "/") { - http.NotFound(w, r) - return - } - handler.ServeHTTP(w, r) - }) + return web } -// Due to the complexities of UA detection and the ramifications of a misdetection only older Safari and IE browsers throw incompatibility errors. - +// Due to the complexities of UA detection and the ramifications of a misdetection +// only older Safari and IE browsers throw incompatibility errors. // Map should be of minimum required browser version. var browserMinimumSupported = map[string]int{ "BrowserIE": 11, @@ -85,24 +55,26 @@ func CheckClientCompatability(agentString string) bool { return true } -func root(c *api.Context, w http.ResponseWriter, r *http.Request) { +func Handle404(a *app.App, w http.ResponseWriter, r *http.Request) { + err := model.NewAppError("Handle404", "api.context.404.app_error", nil, "", http.StatusNotFound) - if !CheckClientCompatability(r.UserAgent()) { - w.Header().Set("Cache-Control", "no-store") - page := utils.NewHTMLTemplate(c.App.HTMLTemplates(), "unsupported_browser") - page.Props["Title"] = c.T("web.error.unsupported_browser.title") - page.Props["Message"] = c.T("web.error.unsupported_browser.message") - page.RenderToWriter(w) - return - } + mlog.Debug(fmt.Sprintf("%v: code=404 ip=%v", r.URL.Path, utils.GetIpAddress(r))) - if api.IsApiCall(r) { - api.Handle404(c.App, w, r) - return + if IsApiCall(r) { + w.WriteHeader(err.StatusCode) + err.DetailedError = "There doesn't appear to be an api call for the url='" + r.URL.Path + "'. Typo? are you missing a team_id or user_id as part of the url?" + w.Write([]byte(err.ToJson())) + } else { + utils.RenderWebAppError(w, r, err, a.AsymmetricSigningKey()) } +} - w.Header().Set("Cache-Control", "no-cache, max-age=31556926, public") +func IsApiCall(r *http.Request) bool { + return strings.Index(r.URL.Path, "/api/") == 0 +} - staticDir, _ := utils.FindDir(model.CLIENT_DIR) - http.ServeFile(w, r, filepath.Join(staticDir, "root.html")) +func ReturnStatusOK(w http.ResponseWriter) { + m := make(map[string]string) + m[model.STATUS] = model.STATUS_OK + w.Write([]byte(model.MapToJson(m))) } |