From aad3bc95497e7ebf2dc69d21d5d53dc769345a88 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 12 Jan 2020 02:49:55 +0000 Subject: cmd/whoisfront: push switches to Mi --- cmd/whoisfront/main.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'cmd') diff --git a/cmd/whoisfront/main.go b/cmd/whoisfront/main.go index 1ae9f51..9139af0 100644 --- a/cmd/whoisfront/main.go +++ b/cmd/whoisfront/main.go @@ -2,6 +2,7 @@ package main import ( + "bytes" "encoding/json" "flag" "fmt" @@ -16,6 +17,7 @@ import ( var ( switchCounterURL = flag.String("switch-counter-url", "", "the webhook for switchcounter.science") + miToken = flag.String("mi-token", "", "Mi token") sc switchcounter.API ) @@ -31,6 +33,23 @@ func main() { } } +func miSwitch(to string) error { + req, err := http.NewRequest(http.MethodGet, "https://mi.within.website/switches/switch", bytes.NewBuffer([]byte(to))) + if err != nil { + return err + } + req.Header.Add("Authorization", *miToken) + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("wanted %d, got: %s", http.StatusOK, resp.Status) + } + return nil +} + func handle(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodPost { front, err := ioutil.ReadAll(r.Body) @@ -47,6 +66,12 @@ func handle(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err) } + + err = miSwitch(string(front)) + if err != nil { + panic(err) + } + w.Header().Set("Content-Type", "text/plain") fmt.Fprint(w, string(front)) return -- cgit v1.2.3