MVP
This commit is contained in:
92
main.go
92
main.go
@@ -1,27 +1,103 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
// fmt.Fprintf(w, "Hello! you've requested %s\n", r.URL.Path)
|
||||
// })
|
||||
type PDAlert struct {
|
||||
RoutingKey string `json:"routing_key"`
|
||||
EventAction string `json:"event_action"`
|
||||
Payload PDPayload `json:"payload"`
|
||||
}
|
||||
|
||||
http.Handle("/", http.FileServer(http.Dir("./static")))
|
||||
type PDPayload struct {
|
||||
Summary string `json:"summary"`
|
||||
Source string `json:"source"`
|
||||
Severity string `json:"severity"`
|
||||
}
|
||||
|
||||
type PHandler struct {
|
||||
next http.Handler
|
||||
}
|
||||
|
||||
func NewPHandler(next http.Handler) *PHandler {
|
||||
return &PHandler{
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (ph *PHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("invalid form: %s\n", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
msg := r.Form.Get("msg")
|
||||
if msg == "" {
|
||||
ph.next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
err = json.NewEncoder(buf).Encode(PDAlert{
|
||||
RoutingKey: "63e451a6e5f84309d08d439bfe5efab5",
|
||||
EventAction: "trigger",
|
||||
Payload: PDPayload{
|
||||
Summary: msg,
|
||||
Source: "urlparam",
|
||||
Severity: "critical",
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create PD request: %s\n", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", "https://events.pagerduty.com/v2/enqueue", buf)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to create HTTP request: %s\n", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
c := &http.Client{}
|
||||
res, err := c.Do(req)
|
||||
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("error from PD: %s\n", err), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
res.Body.Close()
|
||||
|
||||
if res.StatusCode != 202 {
|
||||
http.Error(w, fmt.Sprintf("error from PD: %s", string(body)), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte("page sent\n"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.Handle("/", NewPHandler(http.FileServer(http.Dir("./static"))))
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
port = "80"
|
||||
}
|
||||
|
||||
bindAddr := fmt.Sprintf(":%s", port)
|
||||
fmt.Printf("==> Server listening at %s 🚀\n", bindAddr)
|
||||
bind := fmt.Sprintf(":%s", port)
|
||||
log.Printf("listening on %s", bind)
|
||||
|
||||
if err := http.ListenAndServe(bindAddr, nil); err != nil {
|
||||
if err := http.ListenAndServe(bind, nil); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user