mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-01 21:24:23 +08:00
b4780a41d3
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
44 lines
1.0 KiB
Go
44 lines
1.0 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)
|
|
}
|