load config
This commit is contained in:
commit
566c2a1a76
3 changed files with 163 additions and 0 deletions
95
config.go
Normal file
95
config.go
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
//"github.com/nbd-wtf/go-nostr"
|
||||
//"github.com/nbd-wtf/go-nostr/nip19"
|
||||
"fiatjaf.com/nostr"
|
||||
"fiatjaf.com/nostr/nip19"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Port int
|
||||
NSec string
|
||||
NPub string
|
||||
Relays []string
|
||||
}
|
||||
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
log.Fatal("Fatal: %s", e)
|
||||
}
|
||||
}
|
||||
|
||||
func LoadConfig() *Config {
|
||||
var nsecfile string
|
||||
var config Config
|
||||
var portarg int
|
||||
var nsecarg string
|
||||
var npubarg string
|
||||
var relayargs string
|
||||
|
||||
flag.IntVar(&portarg, "port", 49160, "HTTP listen port. Defaults to 49160/TCP. Can be set through WEBHOOK_PORT env")
|
||||
flag.StringVar(&nsecarg, "nsec", "", "Path to file containing bech32-encoded nsec. Can be set through NOSTR_NSEC_FILE env or generated on first run")
|
||||
flag.StringVar(&npubarg, "npub", "", "Bech32-encoded npub of the account who will receive notifications. Can be set through NOSTR_NPUB env. You can also set a by alert target by setting the \"npub\" label on the alert properties.")
|
||||
flag.StringVar(&relayargs, "relays", "", "Comma separated list of relays you want to send your notifications to. Can be set through NOSTR_RELAYS env.")
|
||||
flag.Parse()
|
||||
|
||||
port, ok := os.LookupEnv("WEBHOOK_PORT")
|
||||
if ok {
|
||||
port, err := strconv.Atoi(port)
|
||||
check(err)
|
||||
config.Port = port
|
||||
} else {
|
||||
config.Port = portarg
|
||||
}
|
||||
|
||||
nsecvar, ok := os.LookupEnv("NOSTR_NSEC_FILE")
|
||||
if ok {
|
||||
nsecfile = nsecvar
|
||||
}
|
||||
if nsecarg != "" {
|
||||
nsecfile = nsecarg
|
||||
}
|
||||
|
||||
if nsecfile != "" {
|
||||
data, err := os.ReadFile(nsecfile)
|
||||
check(err)
|
||||
nsec := strings.TrimSpace(string(data))
|
||||
prefix, decoded, err := nip19.Decode(nsec)
|
||||
sk := decoded.(nostr.SecretKey)
|
||||
check(err)
|
||||
if prefix != "nsec" {
|
||||
log.Fatalf("not an nsec, got %s", prefix)
|
||||
}
|
||||
|
||||
pk := nostr.GetPublicKey(sk)
|
||||
npub := nip19.EncodeNpub(pk)
|
||||
log.Printf("Loading private key from file %s. Will send alerts from %s", nsecfile, npub)
|
||||
config.NSec = nsec
|
||||
} else {
|
||||
sk := nostr.Generate()
|
||||
pk := nostr.GetPublicKey(sk)
|
||||
nsec := nip19.EncodeNsec(sk)
|
||||
npub := nip19.EncodeNpub(pk)
|
||||
log.Printf("Using random private key. Will send alerts from %s", npub)
|
||||
config.NSec = nsec
|
||||
}
|
||||
|
||||
npub, ok := os.LookupEnv("NOSTR_NPUB")
|
||||
if ok {
|
||||
config.NPub = npub
|
||||
} else {
|
||||
config.NPub = npubarg
|
||||
}
|
||||
|
||||
relays, ok := os.LookupEnv("NOSTR_RELAYS")
|
||||
if ok {
|
||||
config.Relays = strings.Split(relays, ",")
|
||||
}
|
||||
return &config
|
||||
}
|
||||
13
main.go
Normal file
13
main.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := LoadConfig()
|
||||
log.Printf("Port: %s", cfg.Port)
|
||||
log.Printf("Nsec: %s", cfg.NSec)
|
||||
log.Printf("Npub: %s", cfg.NPub)
|
||||
log.Printf("Relays: %s", cfg.Relays)
|
||||
}
|
||||
55
parser.dis
Normal file
55
parser.dis
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Alert struct {
|
||||
Status string `json:"status"`
|
||||
StartsAt string `json:"startsAt"`
|
||||
EndsAt string `json:"endsAt"`
|
||||
GeneratorURL string `json:"generatorURL"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
SilenceURL string `json:"silenceURL"`
|
||||
DashboardURL string `json:"dashboardURL"`
|
||||
PanelURL string `json:"panelURL"`
|
||||
ValueString string `json:"valueString"`
|
||||
OrgId int `json:"orgId"`
|
||||
Labels map[string]any `json:"labels"`
|
||||
Annotations map[string]any `json:"annotations"`
|
||||
values map[int]any `json:"values"`
|
||||
}
|
||||
|
||||
type Payload struct {
|
||||
Receiver string `json:"receiver"`
|
||||
Status string `json:"firing"`
|
||||
ExternalURL string `json:"externalURL"`
|
||||
Version string `json:"version"`
|
||||
GroupKey string `json:"groupKey"`
|
||||
TruncatedAlerts int `json:"truncatedAlerts"`
|
||||
OrgID int `json:"orgId"`
|
||||
Title string `json:"title"`
|
||||
State string `json"state"`
|
||||
Message string `json:"message"`
|
||||
Alerts []Alert `json:"alerts"`
|
||||
GroupLabels map[string]any `json:"groupLabels"`
|
||||
CommonLabels map[string]any `json:"groupLabels"`
|
||||
CommonAnnotations map[string]any `json:"commonAnnotations"`
|
||||
}
|
||||
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
data, err := os.ReadFile("./alert.json")
|
||||
check(err)
|
||||
var payload Payload
|
||||
p1 := json.Unmarshal([]byte(data), &payload)
|
||||
check(p1)
|
||||
fmt.Printf("Full payload: %+v\n", payload)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue