initial hopefully work setup
This commit is contained in:
parent
ad8b83ac61
commit
6307923dd9
4 changed files with 70 additions and 38 deletions
9
main.go
9
main.go
|
|
@ -2,12 +2,13 @@ package main
|
|||
|
||||
import (
|
||||
"log"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
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)
|
||||
http.HandleFunc("/", alertReceiver)
|
||||
log.Printf("Server starting on http://localhost:%d", cfg.Port)
|
||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil))
|
||||
}
|
||||
|
|
|
|||
34
parser.go
34
parser.go
|
|
@ -5,40 +5,6 @@ import (
|
|||
"encoding/json"
|
||||
)
|
||||
|
||||
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 ParseAlert(payload []byte) (string) {
|
||||
var data Payload
|
||||
p1 := json.Unmarshal(payload, &data)
|
||||
|
|
|
|||
30
server.go
Normal file
30
server.go
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func alertReceiver(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20))
|
||||
if err != nil {
|
||||
http.Error(w, "Payload exceeded maximum size. Are you really sending alerts?", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
if len(body) == 0 {
|
||||
http.Error(w, "Empty payload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
payload := ParseAlert(body)
|
||||
fmt.Printf(payload)
|
||||
|
||||
}
|
||||
35
types.go
Normal file
35
types.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
|
||||
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"`
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue