From 79e5c8a07bb60f715f4e37bd4b2b32c3d4f07cb9 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 17 Feb 2017 17:31:00 +0000 Subject: [PATCH] Use gorilla/mux to route HTTP requests. Make /sync and /send use the right API paths --- .../dendrite/clientapi/routing/routing.go | 53 +++++-------------- .../dendrite/clientapi/writers/sendmessage.go | 5 +- 2 files changed, 17 insertions(+), 41 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go index 177187a2b..817690d8c 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go +++ b/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go @@ -1,56 +1,29 @@ package routing import ( + "net/http" + + "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/readers" - _ "github.com/matrix-org/dendrite/clientapi/writers" + "github.com/matrix-org/dendrite/clientapi/writers" "github.com/matrix-org/util" "github.com/prometheus/client_golang/prometheus" - "net/http" - "strings" ) const pathPrefixR0 = "/_matrix/client/r0" -// Return true if this path should be handled by this handler -type matcher func(path string) bool - -type lookup struct { - Matches matcher - Handler http.Handler -} - -func newLookup(name string, m matcher, h util.JSONRequestHandler) lookup { - return lookup{ - Matches: m, - Handler: prometheus.InstrumentHandler(name, util.MakeJSONAPI(h)), - } -} - -func matchesString(str string) func(path string) bool { - return func(path string) bool { - return path == str - } +func make(metricsName string, h util.JSONRequestHandler) http.Handler { + return prometheus.InstrumentHandler(metricsName, util.MakeJSONAPI(h)) } // Setup registers HTTP handlers with the given ServeMux. It also supplies the given http.Client // to clients which need to make outbound HTTP requests. -func Setup(mux *http.ServeMux, httpClient *http.Client) { - var r0lookups []lookup - r0lookups = append(r0lookups, newLookup("sync", matchesString("/sync"), &readers.Sync{})) +func Setup(servMux *http.ServeMux, httpClient *http.Client) { + apiMux := mux.NewRouter() + r0mux := apiMux.PathPrefix("/_matrix/client/r0").Subrouter() + r0mux.Handle("/sync", make("sync", &readers.Sync{})) + r0mux.Handle("/rooms/{roomID}/send/{eventType}", make("send_message", &writers.SendMessage{})) - mux.Handle("/metrics", prometheus.Handler()) - mux.HandleFunc("/api/", func(w http.ResponseWriter, req *http.Request) { - clientServerPath := strings.TrimPrefix(req.URL.Path, "/api") - if strings.HasPrefix(clientServerPath, pathPrefixR0) { - apiPath := strings.TrimPrefix(clientServerPath, pathPrefixR0) - for _, lookup := range r0lookups { - if lookup.Matches(apiPath) { - lookup.Handler.ServeHTTP(w, req) - return - } - } - } - w.WriteHeader(404) - w.Write([]byte(`{"error":"Not found","errcode":"M_NOT_FOUND"}`)) - }) + servMux.Handle("/metrics", prometheus.Handler()) + servMux.Handle("/api/", http.StripPrefix("/api", apiMux)) } diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go b/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go index dc4df2f5d..f646e91b4 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go @@ -4,6 +4,7 @@ import ( "net/http" log "github.com/Sirupsen/logrus" + "github.com/gorilla/mux" "github.com/matrix-org/util" ) @@ -14,7 +15,9 @@ type SendMessage struct { // OnIncomingRequest implements util.JSONRequestHandler func (s *SendMessage) OnIncomingRequest(req *http.Request) (interface{}, *util.HTTPError) { logger := req.Context().Value(util.CtxValueLogger).(*log.Entry) - logger.Info("Doing stuff...") + vars := mux.Vars(req) + roomID := vars["roomID"] + logger.WithField("roomID", roomID).Info("Doing stuff...") return nil, &util.HTTPError{ Code: 404, Message: "Not implemented yet",