mirror of
https://github.com/adnanh/webhook.git
synced 2025-10-04 21:51:02 +00:00
dir-feat: Make loading from directory recursively possible
The commit adds the following modifications: * Possibility to load hooks recursively from a directory * Extraction of the loadinghook method in webhook.go * Tests done on a fairly complex tree of directory Allowing hooks to be loaded from a directory is a nice feature, as it allows a better organisation/readibility of the hooks that we have. The LoadFromDir method rely on LoadFromFile and is meant to be very permissive, and will return errors only when no hooks have been loaded. Otherwise it will simply return 'nil' and the potential issues/warnings that happen during the loading of hooks (invalid file, invalid path, issue opening, etc).
This commit is contained in:
parent
18b0573bc4
commit
1db3be532b
7 changed files with 345 additions and 18 deletions
54
webhook.go
54
webhook.go
|
@ -31,6 +31,7 @@ var (
|
|||
noPanic = flag.Bool("nopanic", false, "do not panic if hooks cannot be loaded when webhook is not running in verbose mode")
|
||||
hotReload = flag.Bool("hotreload", false, "watch hooks file for changes and reload them automatically")
|
||||
hooksFilePath = flag.String("hooks", "hooks.json", "path to the json file containing defined hooks the webhook should serve")
|
||||
hooksDirPath = flag.String("hooksdir", "", "path to the json directory containing defined hooks the webhook should serve")
|
||||
hooksURLPrefix = flag.String("urlprefix", "hooks", "url prefix to use for served hooks (protocol://yourserver:port/PREFIX/:hook-id)")
|
||||
secure = flag.Bool("secure", false, "use HTTPS instead of HTTP")
|
||||
cert = flag.String("cert", "cert.pem", "path to the HTTPS certificate pem file")
|
||||
|
@ -64,24 +65,7 @@ func main() {
|
|||
setupSignals()
|
||||
|
||||
// load and parse hooks
|
||||
log.Printf("attempting to load hooks from %s\n", *hooksFilePath)
|
||||
|
||||
err := hooks.LoadFromFile(*hooksFilePath)
|
||||
|
||||
if err != nil {
|
||||
if !*verbose && !*noPanic {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.Fatalf("couldn't load any hooks from file! %+v\naborting webhook execution since the -verbose flag is set to false.\nIf, for some reason, you want webhook to start without the hooks, either use -verbose flag, or -nopanic", err)
|
||||
}
|
||||
|
||||
log.Printf("couldn't load hooks from file! %+v\n", err)
|
||||
} else {
|
||||
log.Printf("loaded %d hook(s) from file\n", len(hooks))
|
||||
|
||||
for _, hook := range hooks {
|
||||
log.Printf("\t> %s\n", hook.ID)
|
||||
}
|
||||
}
|
||||
hooks = loadHooks()
|
||||
|
||||
if *hotReload {
|
||||
// set up file watcher
|
||||
|
@ -137,7 +121,41 @@ func main() {
|
|||
log.Printf("starting insecure (http) webhook on %s:%d", *ip, *port)
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *ip, *port), n))
|
||||
}
|
||||
}
|
||||
|
||||
func loadHooks() hook.Hooks {
|
||||
log.Printf("attempting to load hooks from file %s\n", *hooksFilePath)
|
||||
file_hooks := hook.Hooks{}
|
||||
err_loadfile := file_hooks.LoadFromFile(*hooksFilePath)
|
||||
|
||||
log.Printf("attempting to load hooks from dir %s\n", *hooksDirPath)
|
||||
dir_hooks := hook.Hooks{}
|
||||
warnings, err_loaddir := dir_hooks.LoadFromDir(*hooksDirPath)
|
||||
|
||||
if *hooksDirPath != "" && len(warnings) != 0 {
|
||||
log.Printf("faced issues while loading from %s:\n", *hooksDirPath)
|
||||
for _, warning := range warnings {
|
||||
log.Printf("\t> %s\n", warning)
|
||||
}
|
||||
}
|
||||
if err_loadfile != nil && err_loaddir != nil {
|
||||
if !*verbose && !*noPanic {
|
||||
log.SetOutput(os.Stdout)
|
||||
log.Printf("couldn't load any hooks from file and/or dir!\n")
|
||||
log.Printf("if, for some reason, you want webhook to start without the hooks, either use -verbose flag, or -nopanic")
|
||||
log.Fatal("aborting webhook execution since the -verbose flag is set to false.\n")
|
||||
}
|
||||
} else {
|
||||
log.Printf("loaded %d hook(s) from file\n", len(file_hooks))
|
||||
for _, hook := range file_hooks {
|
||||
log.Printf("\t> %s\n", hook.ID)
|
||||
}
|
||||
log.Printf("loaded %d hook(s) from directory\n", len(dir_hooks))
|
||||
for _, hook := range dir_hooks {
|
||||
log.Printf("\t> %s\n", hook.ID)
|
||||
}
|
||||
}
|
||||
return append(dir_hooks, file_hooks...)
|
||||
}
|
||||
|
||||
func hookHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue