From fe1854feaf1842eeda2e919cc75ab310dc703597 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 3 Aug 2016 13:23:41 +0100 Subject: [PATCH] Use the service ID in the webhook path instead of the service type This means we can load the Service from the database then invoke OnIncomingWebhook on it, rather than load up a new Service. --- src/github.com/matrix-org/go-neb/api.go | 16 +++++++++++----- src/github.com/matrix-org/go-neb/goneb.go | 4 +++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index 8a07874..92383a8 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + log "github.com/Sirupsen/logrus" "github.com/matrix-org/go-neb/clients" "github.com/matrix-org/go-neb/database" "github.com/matrix-org/go-neb/errors" @@ -16,12 +17,17 @@ func (*heartbeatHandler) OnIncomingRequest(req *http.Request) (interface{}, *err return &struct{}{}, nil } -func handleWebhook(w http.ResponseWriter, req *http.Request) { +type webhookHandler struct { + db *database.ServiceDB +} + +func (wh *webhookHandler) handle(w http.ResponseWriter, req *http.Request) { segments := strings.Split(req.URL.Path, "/") - // last path segment is the service type which we will pass the incoming request to - srvType := segments[len(segments)-1] - service := types.CreateService("", srvType) - if service == nil { + // last path segment is the service ID which we will pass the incoming request to + srvID := segments[len(segments)-1] + service, err := wh.db.LoadService(srvID) + if err != nil { + log.WithError(err).WithField("service_id", srvID).Print("Failed to load service") w.WriteHeader(404) return } diff --git a/src/github.com/matrix-org/go-neb/goneb.go b/src/github.com/matrix-org/go-neb/goneb.go index 7ba75c2..6515e6d 100644 --- a/src/github.com/matrix-org/go-neb/goneb.go +++ b/src/github.com/matrix-org/go-neb/goneb.go @@ -31,7 +31,9 @@ func main() { http.Handle("/test", server.MakeJSONAPI(&heartbeatHandler{})) http.Handle("/admin/configureClient", server.MakeJSONAPI(&configureClientHandler{db: db, clients: clients})) http.Handle("/admin/configureService", server.MakeJSONAPI(&configureServiceHandler{db: db, clients: clients})) - http.HandleFunc("/services/hooks/", handleWebhook) + + wh := &webhookHandler{db: db} + http.HandleFunc("/services/hooks/", wh.handle) http.ListenAndServe(bindAddress, nil) }