Read log level and bind address from config file
This commit is contained in:
parent
0bfa2cf7b9
commit
8347d6410f
4 changed files with 65 additions and 4 deletions
36
config.go
Normal file
36
config.go
Normal file
|
@ -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
|
||||||
|
}
|
8
go.mod
8
go.mod
|
@ -2,6 +2,10 @@ module git.xenrox.net/~xenrox/ntfy-alertmanager
|
||||||
|
|
||||||
go 1.19
|
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
|
||||||
|
|
6
go.sum
6
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 h1:XxHbaQOQllWEMoeSLocFWPyqMPWCDUDatDEtIdYIXhI=
|
||||||
git.xenrox.net/~xenrox/go-log v0.0.0-20221009121411-e93f4223b622/go.mod h1:d98WFDHGpxaEThKue5CfGtr9OrWgbaApprt3GH+OM4s=
|
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 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||||
|
|
19
main.go
19
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -13,6 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type receiver struct {
|
type receiver struct {
|
||||||
|
cfg *config
|
||||||
logger *log.Logger
|
logger *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,10 +102,23 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
var configPath string
|
||||||
|
flag.StringVar(&configPath, "config", "/etc/ntfy-alertmanager/config", "config file path")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
logger := log.NewDefaultLogger()
|
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)
|
http.HandleFunc("/", receiver.handleWebhooks)
|
||||||
logger.Fatal(http.ListenAndServe("127.0.0.1:8080", nil))
|
logger.Fatal(http.ListenAndServe(cfg.HTTPAddress, nil))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue