From d3ecdc066284ae0959315e216e99cdaba0b7ffc8 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 15 Aug 2016 10:00:08 +0100 Subject: [PATCH 1/2] Add endpoint to GET a Service config --- src/github.com/matrix-org/go-neb/api.go | 35 +++++++++++++++++++++++ src/github.com/matrix-org/go-neb/goneb.go | 1 + 2 files changed, 36 insertions(+) diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index ff1da59..2b68d58 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -1,6 +1,7 @@ package main import ( + "database/sql" "encoding/json" log "github.com/Sirupsen/logrus" "github.com/matrix-org/go-neb/clients" @@ -228,3 +229,37 @@ func (s *configureServiceHandler) OnIncomingRequest(req *http.Request) (interfac NewConfig types.Service }{body.ID, body.Type, oldService, service}, nil } + +type getServiceHandler struct { + db *database.ServiceDB +} + +func (h *getServiceHandler) OnIncomingRequest(req *http.Request) (interface{}, *errors.HTTPError) { + if req.Method != "GET" { + return nil, &errors.HTTPError{nil, "Unsupported Method", 405} + } + var body struct { + ID string + } + if err := json.NewDecoder(req.Body).Decode(&body); err != nil { + return nil, &errors.HTTPError{err, "Error parsing request JSON", 400} + } + + if body.ID == "" { + return nil, &errors.HTTPError{nil, `Must supply a "ID"`, 400} + } + + srv, err := h.db.LoadService(body.ID) + if err != nil { + if err == sql.ErrNoRows { + return nil, &errors.HTTPError{err, `Service not found`, 404} + } + return nil, &errors.HTTPError{err, `Failed to load service`, 500} + } + + return &struct { + ID string + Type string + Config types.Service + }{srv.ServiceID(), srv.ServiceType(), srv}, nil +} diff --git a/src/github.com/matrix-org/go-neb/goneb.go b/src/github.com/matrix-org/go-neb/goneb.go index 2e22ef9..2e107bd 100644 --- a/src/github.com/matrix-org/go-neb/goneb.go +++ b/src/github.com/matrix-org/go-neb/goneb.go @@ -40,6 +40,7 @@ func main() { } http.Handle("/test", server.MakeJSONAPI(&heartbeatHandler{})) + http.Handle("/admin/getService", server.MakeJSONAPI(&getServiceHandler{db: db})) http.Handle("/admin/configureClient", server.MakeJSONAPI(&configureClientHandler{db: db, clients: clients})) http.Handle("/admin/configureService", server.MakeJSONAPI(&configureServiceHandler{db: db, clients: clients})) http.Handle("/admin/configureAuthRealm", server.MakeJSONAPI(&configureAuthRealmHandler{db: db})) From 4c340dd016b06f310db01fdfd974157da78d1815 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Mon, 15 Aug 2016 10:46:27 +0100 Subject: [PATCH 2/2] s/GET/POST/ --- src/github.com/matrix-org/go-neb/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index 2b68d58..c0f22b4 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -235,7 +235,7 @@ type getServiceHandler struct { } func (h *getServiceHandler) OnIncomingRequest(req *http.Request) (interface{}, *errors.HTTPError) { - if req.Method != "GET" { + if req.Method != "POST" { return nil, &errors.HTTPError{nil, "Unsupported Method", 405} } var body struct {