0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/middleware/git/webhook/webhook.go
xenolf b4780a41d3 Added webhook functionality to the git middleware.
The webhook providers reside behind a small interface which determines if
a provider should run. If a provider should run it delegates
responsibility of the request to the provider.
ghdeploy initial commit

Added webhook functionality to the git middleware.
The webhook providers reside behind a small interface which determines if a provider should run. If a provider should run it delegates responsibility of the request to the provider.

Add tests

Remove old implementation

Fix inconsistency with git interval pulling.

Remove '\n' from logging statements and put the initial pull into a startup function
2015-06-04 03:24:16 +02:00

43 lines
1 KiB
Go

package webhook
import (
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/git"
"net/http"
)
// Middleware for handling web hooks of git providers
type WebHook struct {
Repo *git.Repo
Next middleware.Handler
}
// Interface for specific providers to implement.
type hookHandler interface {
DoesHandle(http.Header) bool
Handle(w http.ResponseWriter, r *http.Request, repo *git.Repo) (int, error)
}
// Slice of all registered hookHandlers.
// Register new hook handlers here!
var handlers = []hookHandler{
GithubHook{},
}
// ServeHTTP implements the middlware.Handler interface.
func (h WebHook) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
if r.URL.Path == h.Repo.HookUrl {
for _, handler := range handlers {
// if a handler indicates it does handle the request,
// we do not try other handlers. Only one handler ever
// handles a specific request.
if handler.DoesHandle(r.Header) {
return handler.Handle(w, r, h.Repo)
}
}
}
return h.Next.ServeHTTP(w, r)
}