AJ ONeal
4 years ago
6 changed files with 192 additions and 74 deletions
@ -0,0 +1,8 @@ |
|||
// // +build github
|
|||
// TODO omit github unless specified by build tag
|
|||
|
|||
package main |
|||
|
|||
import ( |
|||
_ "git.ryanburnette.com/ryanburnette/git-deploy/internal/webhooks/github" |
|||
) |
@ -0,0 +1,23 @@ |
|||
package options |
|||
|
|||
import ( |
|||
"flag" |
|||
) |
|||
|
|||
var Server *ServerConfig |
|||
|
|||
type ServerConfig struct { |
|||
Addr string |
|||
TrustProxy bool |
|||
Compress bool |
|||
ServePath string |
|||
} |
|||
|
|||
var ServerFlags *flag.FlagSet |
|||
var InitFlags *flag.FlagSet |
|||
var DefaultMaxBodySize int64 = 1024 * 1024 |
|||
|
|||
func init() { |
|||
Server = &ServerConfig{} |
|||
ServerFlags = flag.NewFlagSet("run", flag.ExitOnError) |
|||
} |
@ -0,0 +1,60 @@ |
|||
package webhooks |
|||
|
|||
import ( |
|||
"github.com/go-chi/chi" |
|||
) |
|||
|
|||
// Ref represents typical git webhook info such as:
|
|||
// HTTPSURL ex: https://git@git.example.com/example/example.git
|
|||
// SSHURL ex: ssh://git@git.example.com/example/example.git
|
|||
// Rev ex: 00000000
|
|||
// Ref ex: /refs/heads/master
|
|||
// Branch ex: master
|
|||
// Repo ex: example
|
|||
// Org ex: example
|
|||
type Ref struct { |
|||
HTTPSURL string |
|||
SSHURL string |
|||
Rev string |
|||
Ref string |
|||
Branch string |
|||
Repo string |
|||
Org string |
|||
} |
|||
|
|||
var Providers = make(map[string]func()) |
|||
var Webhooks = make(map[string]func(chi.Router)) |
|||
|
|||
var hooks = make(chan Ref) |
|||
|
|||
func Hook(r Ref) { |
|||
hooks <- r |
|||
} |
|||
|
|||
func Accept() Ref { |
|||
return <-hooks |
|||
} |
|||
|
|||
func AddProvider(name string, initProvider func()) { |
|||
Providers[name] = initProvider |
|||
} |
|||
|
|||
func AddRouteHandler(name string, route func(router chi.Router)) { |
|||
Webhooks[name] = route |
|||
} |
|||
|
|||
func MustRegisterAll() { |
|||
for _, addHandler := range Providers { |
|||
addHandler() |
|||
} |
|||
} |
|||
|
|||
func RouteHandlers(r chi.Router) { |
|||
r.Route("/api/webhooks", func(r chi.Router) { |
|||
for provider, handler := range Webhooks { |
|||
r.Route("/"+provider, func(r chi.Router) { |
|||
handler(r) |
|||
}) |
|||
} |
|||
}) |
|||
} |
Loading…
Reference in new issue