summaryrefslogtreecommitdiffstats
path: root/web/web.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2018-05-14 10:24:58 -0400
committerGitHub <noreply@github.com>2018-05-14 10:24:58 -0400
commit47250c6629416b628a19e5571ac89f7b4646418c (patch)
tree0ccfd50e06af7293e0f9e27c2d1c1200efa78a6a /web/web.go
parent7e7c55198719337e7cb39b07c0d5a48c0a6908de (diff)
downloadchat-47250c6629416b628a19e5571ac89f7b4646418c.tar.gz
chat-47250c6629416b628a19e5571ac89f7b4646418c.tar.bz2
chat-47250c6629416b628a19e5571ac89f7b4646418c.zip
Refactor context out of API packages (#8755)
* Refactor context out of API packages * Update function names per feedback * Move webhook handlers to web and fix web tests * Move more webhook tests out of api package * Fix static handler
Diffstat (limited to 'web/web.go')
-rw-r--r--web/web.go91
1 files changed, 28 insertions, 63 deletions
diff --git a/web/web.go b/web/web.go
index 56a5ab6ac..94363cfde 100644
--- a/web/web.go
+++ b/web/web.go
@@ -6,69 +6,38 @@ 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()
-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 +54,20 @@ 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")
-
- staticDir, _ := utils.FindDir(model.CLIENT_DIR)
- http.ServeFile(w, r, filepath.Join(staticDir, "root.html"))
+func IsApiCall(r *http.Request) bool {
+ return strings.Index(r.URL.Path, "/api/") == 0
}