aboutsummaryrefslogtreecommitdiff
path: root/cmd/whoisfront/main.go
blob: 73ec55689af8f0e8837db44622c928d19257fc6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Command whoisfront is a simple CGI wrapper to switchcounter.science. This is used in some internal tooling.
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.Fprint(w, st.Front)
}