From aafaf6ede6d563ecc505afe9a948eb214497a613 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 3 Feb 2017 16:05:46 +0000 Subject: [PATCH 1/3] Add stub clientapi webserver with readers/writers packages As per RL discussion with Mjark --- .../dendrite/clientapi/clientapi.go | 31 +++++++++++++++++++ .../dendrite/clientapi/readers/sync.go | 19 ++++++++++++ .../dendrite/clientapi/writers/sendmessage.go | 20 ++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/github.com/matrix-org/dendrite/clientapi/clientapi.go create mode 100644 src/github.com/matrix-org/dendrite/clientapi/readers/sync.go create mode 100644 src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go diff --git a/src/github.com/matrix-org/dendrite/clientapi/clientapi.go b/src/github.com/matrix-org/dendrite/clientapi/clientapi.go new file mode 100644 index 000000000..1e83a43ac --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/clientapi.go @@ -0,0 +1,31 @@ +package main + +import ( + "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("/send", prometheus.InstrumentHandler("send_message", util.MakeJSONAPI(&writers.SendMessage{}))) + mux.Handle("/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) + log.Fatal(http.ListenAndServe(bindAddr, nil)) +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go b/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go new file mode 100644 index 000000000..fcec593a8 --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go @@ -0,0 +1,19 @@ +package readers + +import ( + "net/http" + + log "github.com/Sirupsen/logrus" + "github.com/matrix-org/util" +) + +type Sync struct{} + +func (s *Sync) OnIncomingRequest(req *http.Request) (interface{}, *util.HTTPError) { + logger := req.Context().Value(util.CtxValueLogger).(*log.Entry) + logger.Info("Doing stuff...") + return nil, &util.HTTPError{ + Code: 404, + Message: "Not implemented yet", + } +} diff --git a/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go b/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go new file mode 100644 index 000000000..55cab225a --- /dev/null +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go @@ -0,0 +1,20 @@ +package writers + +import ( + "net/http" + + log "github.com/Sirupsen/logrus" + "github.com/matrix-org/util" +) + +type SendMessage struct { +} + +func (s *SendMessage) OnIncomingRequest(req *http.Request) (interface{}, *util.HTTPError) { + logger := req.Context().Value(util.CtxValueLogger).(*log.Entry) + logger.Info("Doing stuff...") + return nil, &util.HTTPError{ + Code: 404, + Message: "Not implemented yet", + } +} From 225cce9c773a1ee5122fe8ff402adc624cef7b74 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 3 Feb 2017 16:28:11 +0000 Subject: [PATCH 2/3] Prefix /api as per standards --- src/github.com/matrix-org/dendrite/clientapi/clientapi.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/github.com/matrix-org/dendrite/clientapi/clientapi.go b/src/github.com/matrix-org/dendrite/clientapi/clientapi.go index 1e83a43ac..989dbf3b2 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/clientapi.go +++ b/src/github.com/matrix-org/dendrite/clientapi/clientapi.go @@ -16,8 +16,8 @@ import ( // to clients which need to make outbound HTTP requests. func setup(mux *http.ServeMux, httpClient *http.Client) { mux.Handle("/metrics", prometheus.Handler()) - mux.Handle("/send", prometheus.InstrumentHandler("send_message", util.MakeJSONAPI(&writers.SendMessage{}))) - mux.Handle("/sync", prometheus.InstrumentHandler("sync", util.MakeJSONAPI(&readers.Sync{}))) + 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() { From b04dfae9c5f6b103ae6bf18f23c7bd2695ffddb7 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 3 Feb 2017 16:32:55 +0000 Subject: [PATCH 3/3] Add comments --- src/github.com/matrix-org/dendrite/clientapi/readers/sync.go | 2 ++ .../matrix-org/dendrite/clientapi/writers/sendmessage.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go b/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go index fcec593a8..c5de15b06 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go +++ b/src/github.com/matrix-org/dendrite/clientapi/readers/sync.go @@ -7,8 +7,10 @@ import ( "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) logger.Info("Doing stuff...") 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 55cab225a..dc4df2f5d 100644 --- a/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go +++ b/src/github.com/matrix-org/dendrite/clientapi/writers/sendmessage.go @@ -7,9 +7,11 @@ import ( "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...")