routing.go 2.7 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
	"github.com/matrix-org/dendrite/clientapi/producers"
9 10
	"github.com/matrix-org/dendrite/clientapi/readers"
	"github.com/matrix-org/dendrite/clientapi/writers"
11
	"github.com/matrix-org/dendrite/roomserver/api"
12 13 14 15 16 17 18 19
	"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.
20
func Setup(servMux *http.ServeMux, httpClient *http.Client, cfg config.ClientAPI, producer *producers.RoomserverProducer, queryAPI api.RoomserverQueryAPI) {
21 22
	apiMux := mux.NewRouter()
	r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
23
	r0mux.Handle("/createRoom", make("createRoom", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
K
Kegsay 已提交
24
		return writers.CreateRoom(req, cfg, producer)
25
	})))
26
	r0mux.Handle("/rooms/{roomID}/send/{eventType}/{txnID}",
27
		make("send_message", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
28
			vars := mux.Vars(req)
K
Kegsay 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
			return writers.SendEvent(req, vars["roomID"], vars["eventType"], vars["txnID"], nil, cfg, queryAPI, producer)
		})),
	)
	r0mux.Handle("/rooms/{roomID}/state/{eventType}",
		make("send_message", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
			vars := mux.Vars(req)
			emptyString := ""
			return writers.SendEvent(req, vars["roomID"], vars["eventType"], vars["txnID"], &emptyString, cfg, queryAPI, producer)
		})),
	)
	r0mux.Handle("/rooms/{roomID}/state/{eventType}/{stateKey}",
		make("send_message", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
			vars := mux.Vars(req)
			stateKey := vars["stateKey"]
			return writers.SendEvent(req, vars["roomID"], vars["eventType"], vars["txnID"], &stateKey, cfg, queryAPI, producer)
44 45 46 47 48 49 50
		})),
	)

	servMux.Handle("/metrics", prometheus.Handler())
	servMux.Handle("/api/", http.StripPrefix("/api", apiMux))
}

51 52 53 54 55 56 57 58 59 60 61
// SetupSyncServer configures the given mux with sync-server listeners
func SetupSyncServer(servMux *http.ServeMux, httpClient *http.Client, cfg config.Sync) {
	apiMux := mux.NewRouter()
	r0mux := apiMux.PathPrefix(pathPrefixR0).Subrouter()
	r0mux.Handle("/sync", make("sync", util.NewJSONRequestHandler(func(req *http.Request) util.JSONResponse {
		return readers.Sync(req)
	})))
	servMux.Handle("/metrics", prometheus.Handler())
	servMux.Handle("/api/", http.StripPrefix("/api", apiMux))
}

62 63 64 65
// make a util.JSONRequestHandler into an http.Handler
func make(metricsName string, h util.JSONRequestHandler) http.Handler {
	return prometheus.InstrumentHandler(metricsName, util.MakeJSONAPI(h))
}