diff --git a/config.go b/config.go new file mode 100644 index 0000000..9cfa67a --- /dev/null +++ b/config.go @@ -0,0 +1,36 @@ +package main + +import "git.sr.ht/~emersion/go-scfg" + +type config struct { + HTTPAddress string + LogLevel string +} + +func readConfig(path string) (*config, error) { + cfg, err := scfg.Load(path) + if err != nil { + return nil, err + } + + config := new(config) + // Set default values + config.HTTPAddress = "127.0.0.1:8080" + config.LogLevel = "info" + + d := cfg.Get("log-level") + if d != nil { + if err := d.ParseParams(&config.LogLevel); err != nil { + return nil, err + } + } + + d = cfg.Get("http-address") + if d != nil { + if err := d.ParseParams(&config.HTTPAddress); err != nil { + return nil, err + } + } + + return config, nil +} diff --git a/go.mod b/go.mod index b7e9b75..5bd8b3f 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,10 @@ module git.xenrox.net/~xenrox/ntfy-alertmanager go 1.19 -require git.xenrox.net/~xenrox/go-log v0.0.0-20221009121411-e93f4223b622 +require ( + git.sr.ht/~emersion/go-scfg v0.0.0-20211215104734-c2c7a15d6c99 + git.xenrox.net/~xenrox/go-log v0.0.0-20221009121411-e93f4223b622 + golang.org/x/text v0.3.7 +) -require golang.org/x/text v0.3.7 +require github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect diff --git a/go.sum b/go.sum index b636876..7ca4fec 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,10 @@ +git.sr.ht/~emersion/go-scfg v0.0.0-20211215104734-c2c7a15d6c99 h1:1s8n5uisqkR+BzPgaum6xxIjKmzGrTykJdh+Y3f5Xao= +git.sr.ht/~emersion/go-scfg v0.0.0-20211215104734-c2c7a15d6c99/go.mod h1:t+Ww6SR24yYnXzEWiNlOY0AFo5E9B73X++10lrSpp4U= git.xenrox.net/~xenrox/go-log v0.0.0-20221009121411-e93f4223b622 h1:XxHbaQOQllWEMoeSLocFWPyqMPWCDUDatDEtIdYIXhI= git.xenrox.net/~xenrox/go-log v0.0.0-20221009121411-e93f4223b622/go.mod h1:d98WFDHGpxaEThKue5CfGtr9OrWgbaApprt3GH+OM4s= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= +github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= diff --git a/main.go b/main.go index 4d46241..628f13a 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "flag" "fmt" "net/http" "strings" @@ -13,6 +14,7 @@ import ( ) type receiver struct { + cfg *config logger *log.Logger } @@ -100,10 +102,23 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) { } func main() { + var configPath string + flag.StringVar(&configPath, "config", "/etc/ntfy-alertmanager/config", "config file path") + flag.Parse() + logger := log.NewDefaultLogger() - receiver := &receiver{logger: logger} + cfg, err := readConfig(configPath) + if err != nil { + logger.Fatalf("config: %v", err) + } + + if err := logger.ParseLevel(cfg.LogLevel); err != nil { + logger.Errorf("config: %v", err) + } + + receiver := &receiver{cfg: cfg, logger: logger} http.HandleFunc("/", receiver.handleWebhooks) - logger.Fatal(http.ListenAndServe("127.0.0.1:8080", nil)) + logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil)) }