mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-08-02 22:22:46 +00:00
Use gorilla/mux to route HTTP requests (#11)
* Add basic routing based on matched paths * Make /sync and /send use the right API paths
This commit is contained in:
parent
154110b7c2
commit
f1bb59d24a
7 changed files with 131 additions and 43 deletions
|
@ -1,31 +1,19 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/matrix-org/dendrite/clientapi/routing"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 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) {
|
||||
mux.Handle("/metrics", prometheus.Handler())
|
||||
mux.Handle("/api/send", prometheus.InstrumentHandler("send_message", util.MakeJSONAPI(&writers.SendMessage{})))
|
||||
mux.Handle("/api/sync", prometheus.InstrumentHandler("sync", util.MakeJSONAPI(&readers.Sync{})))
|
||||
}
|
||||
|
||||
func main() {
|
||||
bindAddr := os.Getenv("BIND_ADDRESS")
|
||||
if bindAddr == "" {
|
||||
log.Panic("No BIND_ADDRESS environment variable found.")
|
||||
}
|
||||
log.Info("Starting clientapi")
|
||||
setup(http.DefaultServeMux, http.DefaultClient)
|
||||
routing.Setup(http.DefaultServeMux, http.DefaultClient)
|
||||
log.Fatal(http.ListenAndServe(bindAddr, nil))
|
||||
}
|
||||
|
|
|
@ -3,16 +3,12 @@ package readers
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
// Sync handles HTTP requests to /sync
|
||||
type Sync struct{}
|
||||
|
||||
// OnIncomingRequest implements util.JSONRequestHandler
|
||||
func (s *Sync) OnIncomingRequest(req *http.Request) (interface{}, *util.HTTPError) {
|
||||
logger := req.Context().Value(util.CtxValueLogger).(*log.Entry)
|
||||
// Sync implements /sync
|
||||
func Sync(req *http.Request) (interface{}, *util.HTTPError) {
|
||||
logger := util.GetLogger(req.Context())
|
||||
logger.Info("Doing stuff...")
|
||||
return nil, &util.HTTPError{
|
||||
Code: 404,
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
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("/sync", make("sync", wrap(func(req *http.Request) (interface{}, *util.HTTPError) {
|
||||
return readers.Sync(req)
|
||||
})))
|
||||
r0mux.Handle("/rooms/{roomID}/send/{eventType}",
|
||||
make("send_message", wrap(func(req *http.Request) (interface{}, *util.HTTPError) {
|
||||
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 {
|
||||
function func(req *http.Request) (interface{}, *util.HTTPError)
|
||||
}
|
||||
|
||||
func (r *jsonRequestHandlerWrapper) OnIncomingRequest(req *http.Request) (interface{}, *util.HTTPError) {
|
||||
return r.function(req)
|
||||
}
|
||||
func wrap(f func(req *http.Request) (interface{}, *util.HTTPError)) *jsonRequestHandlerWrapper {
|
||||
return &jsonRequestHandlerWrapper{f}
|
||||
}
|
|
@ -3,18 +3,13 @@ package writers
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
// SendMessage handles HTTP requests to /rooms/$room_id/send/$event_type
|
||||
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...")
|
||||
// SendMessage implements /rooms/{roomID}/send/{eventType}
|
||||
func SendMessage(req *http.Request, roomID, eventType string) (interface{}, *util.HTTPError) {
|
||||
logger := util.GetLogger(req.Context())
|
||||
logger.WithField("roomID", roomID).WithField("eventType", eventType).Info("Doing stuff...")
|
||||
return nil, &util.HTTPError{
|
||||
Code: 404,
|
||||
Message: "Not implemented yet",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue