From c40777b689d798dd348e7becbc0a1c7e56857bf6 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 26 Nov 2024 13:37:13 -0600 Subject: [PATCH] Factor out pagerduty --- main.go | 64 ++++++------------------------------------------ pagerduty.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 56 deletions(-) create mode 100644 pagerduty.go diff --git a/main.go b/main.go index dbda2f4..73587ee 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,8 @@ package main import ( - "bytes" - "encoding/json" "fmt" "html/template" - "io" "log" "net/http" "os" @@ -14,22 +11,10 @@ import ( "github.com/openai/openai-go" ) -type PDAlert struct { - RoutingKey string `json:"routing_key"` - EventAction string `json:"event_action"` - Payload PDPayload `json:"payload"` -} - -type PDPayload struct { - Summary string `json:"summary"` - Source string `json:"source"` - Severity string `json:"severity"` -} - type PHandler struct { - tmpl *template.Template - routingKey string - mux *http.ServeMux + tmpl *template.Template + pd *pdClient + mux *http.ServeMux } func NewPHandler(routingKey string) (*PHandler, error) { @@ -45,9 +30,9 @@ func NewPHandler(routingKey string) (*PHandler, error) { } ph := &PHandler{ - tmpl: tmpl, - routingKey: routingKey, - mux: http.NewServeMux(), + tmpl: tmpl, + pd: newPDClient(routingKey), + mux: http.NewServeMux(), } ph.mux.HandleFunc("/{$}", ph.serveRoot) @@ -80,44 +65,11 @@ func (ph *PHandler) serveRoot(w http.ResponseWriter, r *http.Request) { return } - buf := &bytes.Buffer{} - err = json.NewEncoder(buf).Encode(PDAlert{ - RoutingKey: ph.routingKey, - EventAction: "trigger", - Payload: PDPayload{ - Summary: m, - Source: r.RemoteAddr, - Severity: "critical", - }, - }) - + err = ph.pd.sendAlert(m) if err != nil { - sendError(w, http.StatusInternalServerError, "Create PD request: %s", err) + sendError(w, http.StatusInternalServerError, "Send PD alert: %s", err) return } - - req, err := http.NewRequest("POST", "https://events.pagerduty.com/v2/enqueue", buf) - if err != nil { - sendError(w, http.StatusInternalServerError, "Create HTTP request: %s", err) - return - } - - c := &http.Client{} - res, err := c.Do(req) - - if err != nil { - sendError(w, http.StatusInternalServerError, "Call PagerDuty: %s", err) - return - } - - body, _ := io.ReadAll(res.Body) - _ = res.Body.Close() - - if res.StatusCode != 202 { - sendError(w, http.StatusInternalServerError, "PagerDuty error: %s", string(body)) - return - } - sendResponse(w, "Page sent") } diff --git a/pagerduty.go b/pagerduty.go new file mode 100644 index 0000000..30ec697 --- /dev/null +++ b/pagerduty.go @@ -0,0 +1,69 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" +) + +type pdAlert struct { + RoutingKey string `json:"routing_key"` + EventAction string `json:"event_action"` + Payload pdPayload `json:"payload"` +} + +type pdPayload struct { + Summary string `json:"summary"` + Source string `json:"source"` + Severity string `json:"severity"` +} + +type pdClient struct { + c *http.Client + routingKey string +} + +func newPDClient(routingKey string) *pdClient { + return &pdClient{ + c: &http.Client{}, + routingKey: routingKey, + } +} + +func (pd *pdClient) sendAlert(msg string) error { + buf := &bytes.Buffer{} + err := json.NewEncoder(buf).Encode(pdAlert{ + RoutingKey: pd.routingKey, + EventAction: "trigger", + Payload: pdPayload{ + Summary: msg, + Source: "p", + Severity: "critical", + }, + }) + + if err != nil { + return fmt.Errorf("Encode PD alert: %s", err) + } + + req, err := http.NewRequest("POST", "https://events.pagerduty.com/v2/enqueue", buf) + if err != nil { + return fmt.Errorf("Create HTTP request: %s", err) + } + + resp, err := pd.c.Do(req) + if err != nil { + return fmt.Errorf("Call PagerDuty: %s", err) + } + + defer resp.Body.Close() + body, _ := io.ReadAll(resp.Body) + + if resp.StatusCode != 202 { + return fmt.Errorf("PagerDuty error: %s", string(body)) + } + + return nil +}