dendrite/src/github.com/matrix-org/dendrite/clientapi/routing/routing.go

53 lines
1.8 KiB
Go
Raw Normal View History

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/util"
"github.com/prometheus/client_golang/prometheus"
)
const pathPrefixR0 = "/_matrix/client/r0"
// 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(servMux *http.ServeMux, httpClient *http.Client) {
apiMux := mux.NewRouter()
r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
r0mux.Handle("/createRoom", make("createRoom", wrap(func(req *http.Request) util.JSONResponse {
return writers.CreateRoom(req)
})))
2017-02-24 12:32:27 +00:00
r0mux.Handle("/sync", make("sync", wrap(func(req *http.Request) util.JSONResponse {
return readers.Sync(req)
})))
r0mux.Handle("/rooms/{roomID}/send/{eventType}",
2017-02-24 12:32:27 +00:00
make("send_message", wrap(func(req *http.Request) util.JSONResponse {
vars := mux.Vars(req)
return writers.SendMessage(req, vars["roomID"], vars["eventType"])
})),
)
servMux.Handle("/metrics", prometheus.Handler())
servMux.Handle("/api/", http.StripPrefix("/api", apiMux))
}
// make a util.JSONRequestHandler into an http.Handler
func make(metricsName string, h util.JSONRequestHandler) http.Handler {
return prometheus.InstrumentHandler(metricsName, util.MakeJSONAPI(h))
}
// jsonRequestHandlerWrapper is a wrapper to allow in-line functions to conform to util.JSONRequestHandler
type jsonRequestHandlerWrapper struct {
2017-02-24 12:32:27 +00:00
function func(req *http.Request) util.JSONResponse
}
2017-02-24 12:32:27 +00:00
func (r *jsonRequestHandlerWrapper) OnIncomingRequest(req *http.Request) util.JSONResponse {
return r.function(req)
}
2017-02-24 12:32:27 +00:00
func wrap(f func(req *http.Request) util.JSONResponse) *jsonRequestHandlerWrapper {
return &jsonRequestHandlerWrapper{f}
}