mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-20 16:07:34 +00:00
feat(config): improve configuration flags
This commit is contained in:
parent
5f734a74ec
commit
eb699dd466
11
README.md
11
README.md
|
@ -42,6 +42,8 @@ You can configure the daemon by:
|
||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
|----------|---------|-------------|
|
|----------|---------|-------------|
|
||||||
|
| `APP_LISTEN_ADDR` | `:8080` | HTTP service address |
|
||||||
|
| `APP_NB_WORKERS` | `2` | The number of workers to start |
|
||||||
| `APP_HOOK_TIMEOUT` | `10` | Hook maximum delay before timeout (in second) |
|
| `APP_HOOK_TIMEOUT` | `10` | Hook maximum delay before timeout (in second) |
|
||||||
| `APP_SCRIPTS_DIR` | `./scripts` | Scripts directory |
|
| `APP_SCRIPTS_DIR` | `./scripts` | Scripts directory |
|
||||||
| `APP_SCRIPTS_GIT_URL` | none | GIT repository that contains scripts (Note: this is only used by the Docker image or by using the Docker entrypoint script) |
|
| `APP_SCRIPTS_GIT_URL` | none | GIT repository that contains scripts (Note: this is only used by the Docker image or by using the Docker entrypoint script) |
|
||||||
|
@ -52,14 +54,17 @@ You can configure the daemon by:
|
||||||
| `APP_NOTIFIER_TO` | none | Recipient of the notification |
|
| `APP_NOTIFIER_TO` | none | Recipient of the notification |
|
||||||
| `APP_HTTP_NOTIFIER_URL` | none | URL of the HTTP notifier |
|
| `APP_HTTP_NOTIFIER_URL` | none | URL of the HTTP notifier |
|
||||||
| `APP_SMTP_NOTIFIER_HOST` | none | Hostname of the SMTP relay |
|
| `APP_SMTP_NOTIFIER_HOST` | none | Hostname of the SMTP relay |
|
||||||
|
| `APP_DEBUG` | `false` | Output debug logs |
|
||||||
|
|
||||||
### Using command parameters:
|
### Using command parameters:
|
||||||
|
|
||||||
| Parameter | Default | Description |
|
| Parameter | Default | Description |
|
||||||
|----------|---------|-------------|
|
|----------|---------|-------------|
|
||||||
| `-l <address>` | `:8080` | HTTP service address |
|
| `-l | --listen <address>` | `:8080` | HTTP service address |
|
||||||
| `-n <workers>` | `2` | The number of workers to start |
|
| `-d | --debug` | false | Output debug logs |
|
||||||
| `-d` | false | Output debug logs |
|
| `--nb-workers <workers>` | `2` | The number of workers to start |
|
||||||
|
| `--scripts <dir>` | `./scripts` | Scripts directory |
|
||||||
|
| `--timeout <timeout>` | `10` | Hook maximum delay before timeout (in second) |
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|
52
config.go
Normal file
52
config.go
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Config contain global configuration
|
||||||
|
type Config struct {
|
||||||
|
ListenAddr *string
|
||||||
|
NbWorkers *int
|
||||||
|
Debug *bool
|
||||||
|
Timeout *int
|
||||||
|
ScriptDir *string
|
||||||
|
}
|
||||||
|
|
||||||
|
var config = &Config{
|
||||||
|
ListenAddr: flag.String("listen", getEnv("LISTEN_ADDR", ":8080"), "HTTP service address (e.g.address, ':8080')"),
|
||||||
|
NbWorkers: flag.Int("nb-workers", getIntEnv("NB_WORKERS", 2), "The number of workers to start"),
|
||||||
|
Debug: flag.Bool("debug", getBoolEnv("DEBUG", false), "Output debug logs"),
|
||||||
|
Timeout: flag.Int("timeout", getIntEnv("HOOK_TIMEOUT", 10), "Hook maximum delay before timeout (in second)"),
|
||||||
|
ScriptDir: flag.String("scripts", getEnv("SCRIPTS_DIR", "scripts"), "Scripts directory"),
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(config.ListenAddr, "l", *config.ListenAddr, "HTTP service (e.g address: ':8080')")
|
||||||
|
flag.BoolVar(config.Debug, "d", *config.Debug, "Output debug logs")
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnv(key, fallback string) string {
|
||||||
|
if value, ok := os.LookupEnv("APP_" + key); ok {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
func getIntEnv(key string, fallback int) int {
|
||||||
|
strValue := getEnv(key, strconv.Itoa(fallback))
|
||||||
|
if value, err := strconv.Atoi(strValue); err == nil {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBoolEnv(key string, fallback bool) bool {
|
||||||
|
strValue := getEnv(key, strconv.FormatBool(fallback))
|
||||||
|
if value, err := strconv.ParseBool(strValue); err == nil {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return fallback
|
||||||
|
}
|
20
main.go
20
main.go
|
@ -29,17 +29,11 @@ var (
|
||||||
healthy int32
|
healthy int32
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
listenAddr = flag.String("l", ":8080", "HTTP service address (e.g.address, ':8080')")
|
|
||||||
nbWorkers = flag.Int("n", 2, "The number of workers to start")
|
|
||||||
debug = flag.Bool("d", false, "Output debug logs")
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
level := "info"
|
level := "info"
|
||||||
if *debug {
|
if *config.Debug {
|
||||||
level = "debug"
|
level = "debug"
|
||||||
}
|
}
|
||||||
logger.Init(level)
|
logger.Init(level)
|
||||||
|
@ -47,7 +41,7 @@ func main() {
|
||||||
logger.Debug.Println("Starting webhookd server...")
|
logger.Debug.Println("Starting webhookd server...")
|
||||||
|
|
||||||
router := http.NewServeMux()
|
router := http.NewServeMux()
|
||||||
router.Handle("/", api.Index())
|
router.Handle("/", api.Index(*config.Timeout, *config.ScriptDir))
|
||||||
router.Handle("/healthz", healthz())
|
router.Handle("/healthz", healthz())
|
||||||
|
|
||||||
nextRequestID := func() string {
|
nextRequestID := func() string {
|
||||||
|
@ -55,14 +49,14 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: *listenAddr,
|
Addr: *config.ListenAddr,
|
||||||
Handler: tracing(nextRequestID)(logging(logger.Debug)(router)),
|
Handler: tracing(nextRequestID)(logging(logger.Debug)(router)),
|
||||||
ErrorLog: logger.Error,
|
ErrorLog: logger.Error,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the dispatcher.
|
// Start the dispatcher.
|
||||||
logger.Debug.Printf("Starting the dispatcher (%d workers)...\n", *nbWorkers)
|
logger.Debug.Printf("Starting the dispatcher (%d workers)...\n", *config.NbWorkers)
|
||||||
worker.StartDispatcher(*nbWorkers)
|
worker.StartDispatcher(*config.NbWorkers)
|
||||||
|
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
quit := make(chan os.Signal, 1)
|
quit := make(chan os.Signal, 1)
|
||||||
|
@ -83,10 +77,10 @@ func main() {
|
||||||
close(done)
|
close(done)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
logger.Info.Println("Server is ready to handle requests at", *listenAddr)
|
logger.Info.Println("Server is ready to handle requests at", *config.ListenAddr)
|
||||||
atomic.StoreInt32(&healthy, 1)
|
atomic.StoreInt32(&healthy, 1)
|
||||||
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
||||||
logger.Error.Fatalf("Could not listen on %s: %v\n", *listenAddr, err)
|
logger.Error.Fatalf("Could not listen on %s: %v\n", *config.ListenAddr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
<-done
|
<-done
|
||||||
|
|
|
@ -4,30 +4,30 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/ncarlier/webhookd/pkg/hook"
|
|
||||||
"github.com/ncarlier/webhookd/pkg/logger"
|
"github.com/ncarlier/webhookd/pkg/logger"
|
||||||
"github.com/ncarlier/webhookd/pkg/tools"
|
"github.com/ncarlier/webhookd/pkg/tools"
|
||||||
"github.com/ncarlier/webhookd/pkg/worker"
|
"github.com/ncarlier/webhookd/pkg/worker"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultTimeout = atoiFallback(os.Getenv("APP_HOOK_TIMEOUT"), 10)
|
defaultTimeout int
|
||||||
|
scriptDir string
|
||||||
)
|
)
|
||||||
|
|
||||||
func atoiFallback(str string, fallback int) int {
|
func atoiFallback(str string, fallback int) int {
|
||||||
value, err := strconv.Atoi(str)
|
if value, err := strconv.Atoi(str); err == nil && value > 0 {
|
||||||
if err != nil || value < 0 {
|
return value
|
||||||
return fallback
|
|
||||||
}
|
}
|
||||||
return value
|
return fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
// Index is the main handler of the API.
|
// Index is the main handler of the API.
|
||||||
func Index() http.Handler {
|
func Index(timeout int, scrDir string) http.Handler {
|
||||||
|
defaultTimeout = timeout
|
||||||
|
scriptDir = scrDir
|
||||||
return http.HandlerFunc(webhookHandler)
|
return http.HandlerFunc(webhookHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
// Get script location
|
// Get script location
|
||||||
p := strings.TrimPrefix(r.URL.Path, "/")
|
p := strings.TrimPrefix(r.URL.Path, "/")
|
||||||
script, err := hook.ResolveScript(p)
|
script, err := tools.ResolveScript(scriptDir, p)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error.Println(err.Error())
|
logger.Error.Println(err.Error())
|
||||||
http.Error(w, err.Error(), http.StatusNotFound)
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package hook
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
@ -9,17 +9,9 @@ import (
|
||||||
"github.com/ncarlier/webhookd/pkg/logger"
|
"github.com/ncarlier/webhookd/pkg/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
scriptsdir = os.Getenv("APP_SCRIPTS_DIR")
|
|
||||||
)
|
|
||||||
|
|
||||||
// ResolveScript is resolving the target script.
|
// ResolveScript is resolving the target script.
|
||||||
func ResolveScript(p string) (string, error) {
|
func ResolveScript(dir, name string) (string, error) {
|
||||||
if scriptsdir == "" {
|
script := path.Join(dir, fmt.Sprintf("%s.sh", name))
|
||||||
scriptsdir = "scripts"
|
|
||||||
}
|
|
||||||
|
|
||||||
script := path.Join(scriptsdir, fmt.Sprintf("%s.sh", p))
|
|
||||||
logger.Debug.Println("Resolving script: ", script, "...")
|
logger.Debug.Println("Resolving script: ", script, "...")
|
||||||
if _, err := os.Stat(script); os.IsNotExist(err) {
|
if _, err := os.Stat(script); os.IsNotExist(err) {
|
||||||
return "", errors.New("Script not found: " + script)
|
return "", errors.New("Script not found: " + script)
|
Loading…
Reference in New Issue
Block a user