routing.go 1.5 KB
Newer Older
1 2 3 4 5 6
package routing

import (
	"net/http"

	"github.com/gorilla/mux"
7
	"github.com/matrix-org/dendrite/clientapi/config"
8 9 10 11 12 13 14 15 16 17
	"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.
18
func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI) {
19 20
	apiMux := mux.NewRouter()
	r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
21 22
	r0mux.Handle("/createRoom", make("createRoom", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
		return writers.CreateRoom(req, cfg)
23
	})))
24
	r0mux.Handle("/sync", make("sync", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
25 26 27
		return readers.Sync(req)
	})))
	r0mux.Handle("/rooms/{roomID}/send/{eventType}",
28
		make("send_message", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
29 30 31 32 33 34 35 36 37 38 39 40 41
			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))
}