diff --git a/src/github.com/matrix-org/go-neb/api.go b/src/github.com/matrix-org/go-neb/api.go index cfec0dc..8a07874 100644 --- a/src/github.com/matrix-org/go-neb/api.go +++ b/src/github.com/matrix-org/go-neb/api.go @@ -7,6 +7,7 @@ import ( "github.com/matrix-org/go-neb/errors" "github.com/matrix-org/go-neb/types" "net/http" + "strings" ) type heartbeatHandler struct{} @@ -15,6 +16,18 @@ func (*heartbeatHandler) OnIncomingRequest(req *http.Request) (interface{}, *err return &struct{}{}, nil } +func handleWebhook(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 { + w.WriteHeader(404) + return + } + service.OnReceiveWebhook(w, req) +} + type configureClientHandler struct { db *database.ServiceDB clients *clients.Clients diff --git a/src/github.com/matrix-org/go-neb/goneb.go b/src/github.com/matrix-org/go-neb/goneb.go index 062970b..7ba75c2 100644 --- a/src/github.com/matrix-org/go-neb/goneb.go +++ b/src/github.com/matrix-org/go-neb/goneb.go @@ -31,6 +31,7 @@ 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) http.ListenAndServe(bindAddress, nil) } diff --git a/src/github.com/matrix-org/go-neb/services/echo/echo.go b/src/github.com/matrix-org/go-neb/services/echo/echo.go index ccdc5e4..11655d8 100644 --- a/src/github.com/matrix-org/go-neb/services/echo/echo.go +++ b/src/github.com/matrix-org/go-neb/services/echo/echo.go @@ -30,7 +30,7 @@ func (e *echoService) Plugin(roomID string) plugin.Plugin { }, } } -func (e *echoService) OnReceiveWebhook(w http.ResponseWriter, req http.Request) { +func (e *echoService) OnReceiveWebhook(w http.ResponseWriter, req *http.Request) { w.WriteHeader(200) // Do nothing } diff --git a/src/github.com/matrix-org/go-neb/services/github/github.go b/src/github.com/matrix-org/go-neb/services/github/github.go index 9d86414..0796a79 100644 --- a/src/github.com/matrix-org/go-neb/services/github/github.go +++ b/src/github.com/matrix-org/go-neb/services/github/github.go @@ -62,7 +62,7 @@ func (s *githubService) Plugin(roomID string) plugin.Plugin { }, } } -func (s *githubService) OnReceiveWebhook(w http.ResponseWriter, req http.Request) { +func (s *githubService) OnReceiveWebhook(w http.ResponseWriter, req *http.Request) { // defer entirely to the webhook package webhook.OnReceiveRequest(w, req, "") } diff --git a/src/github.com/matrix-org/go-neb/services/github/webhook/webhook.go b/src/github.com/matrix-org/go-neb/services/github/webhook/webhook.go index c11cc11..3def2ca 100644 --- a/src/github.com/matrix-org/go-neb/services/github/webhook/webhook.go +++ b/src/github.com/matrix-org/go-neb/services/github/webhook/webhook.go @@ -16,7 +16,7 @@ import ( // OnReceiveRequest processes incoming github webhook requests. The secretToken // parameter is optional. -func OnReceiveRequest(w http.ResponseWriter, r http.Request, secretToken string) { +func OnReceiveRequest(w http.ResponseWriter, r *http.Request, secretToken string) { // Verify the HMAC signature if NEB was configured with a secret token eventType := r.Header.Get("X-GitHub-Event") signatureSHA1 := r.Header.Get("X-Hub-Signature") diff --git a/src/github.com/matrix-org/go-neb/types/types.go b/src/github.com/matrix-org/go-neb/types/types.go index c5dc8ce..dcf42e6 100644 --- a/src/github.com/matrix-org/go-neb/types/types.go +++ b/src/github.com/matrix-org/go-neb/types/types.go @@ -32,7 +32,7 @@ type Service interface { ServiceType() string RoomIDs() []string Plugin(roomID string) plugin.Plugin - OnReceiveWebhook(w http.ResponseWriter, req http.Request) + OnReceiveWebhook(w http.ResponseWriter, req *http.Request) } var servicesByType = map[string]func(string) Service{}