Browse Source

Add handler for /services/hooks/{serviceType}

Defers through to the given service's OnReceiveWebhook method.
kegan/webhooks
Kegan Dougal 8 years ago
parent
commit
41ba67235a
  1. 13
      src/github.com/matrix-org/go-neb/api.go
  2. 1
      src/github.com/matrix-org/go-neb/goneb.go
  3. 2
      src/github.com/matrix-org/go-neb/services/echo/echo.go
  4. 2
      src/github.com/matrix-org/go-neb/services/github/github.go
  5. 2
      src/github.com/matrix-org/go-neb/services/github/webhook/webhook.go
  6. 2
      src/github.com/matrix-org/go-neb/types/types.go

13
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

1
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)
}

2
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
}

2
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, "")
}

2
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")

2
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{}

Loading…
Cancel
Save