From 6de9c7ba1efe9035edf23202cf2663ff06e41722 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 13 Jun 2019 22:32:04 +0000 Subject: cmd: add whoisfront CGI script for some automation --- cmd/whoisfront/main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 cmd/whoisfront/main.go (limited to 'cmd') diff --git a/cmd/whoisfront/main.go b/cmd/whoisfront/main.go new file mode 100644 index 0000000..13901d1 --- /dev/null +++ b/cmd/whoisfront/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net/http" + "net/http/cgi" + + "within.website/x/internal" + "within.website/x/web/switchcounter" +) + +var ( + switchCounterURL = flag.String("switch-counter-url", "", "the webhook for switchcounter.science") + + sc switchcounter.API +) + +func main() { + internal.HandleStartup() + + sc = switchcounter.NewHTTPClient(*switchCounterURL) + + err := cgi.Serve(http.HandlerFunc(handle)) + if err != nil { + log.Fatal(err) + } +} + +func handle(w http.ResponseWriter, r *http.Request) { + req := sc.Status() + resp, err := http.DefaultClient.Do(req) + if err != nil { + panic(err) + } + err = switchcounter.Validate(resp) + if err != nil { + panic(err) + } + var st switchcounter.Status + err = json.NewDecoder(resp.Body).Decode(&st) + if err != nil { + panic(err) + } + + fmt.Fprintln(w, st.Front) +} -- cgit v1.2.3