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
51
52
53
54
55
56
57
|
// Command whoisfront is a simple CGI wrapper to switchcounter.science. This is used in some internal tooling.
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/http/cgi"
"os"
"within.website/x/internal"
)
var (
miTokenPath = flag.String("mi-token-path", "", "Mi token path")
)
func main() {
internal.HandleStartup()
err := cgi.Serve(http.HandlerFunc(handle))
if err != nil {
log.Fatal(err)
}
}
func handle(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequest(http.MethodGet, "https://mi.within.website/api/switches/current/text", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
token, err := os.ReadFile(*miTokenPath)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
req.Header.Add("Authorization", string(token))
req.Header.Add("Accept", "text/plain")
resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if resp.StatusCode != http.StatusOK {
http.Error(w, fmt.Sprintf("bad status code: %d", resp.StatusCode), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain")
io.Copy(w, resp.Body)
}
|