aboutsummaryrefslogtreecommitdiff
path: root/cmd/httpdebug/main.go
blob: 15943b730cb30b26693e26dafc76fe07185d1c54 (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
51
52
package main

import (
	"flag"
	"fmt"
	"io"
	"log"
	"log/slog"
	"net/http"
	"os"
	"strings"

	"within.website/x/internal"
)

var (
	bind   = flag.String("bind", ":3000", "TCP port to bind to")
	silent = flag.Bool("silent", false, "if set, don't log http headers")
)

func main() {
	internal.HandleStartup()

	mux := http.NewServeMux()

	mux.HandleFunc("/.within/health", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "OK")
	})

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		contains := strings.Contains(r.Header.Get("Accept"), "text/html")

		if contains {
			w.Header().Add("Content-Type", "text/html")
			fmt.Fprint(w, "<pre id=\"main\"><code>")
		}

		if !*silent {
			fmt.Println("---")
			r.Header.Write(io.MultiWriter(w, os.Stdout))
		} else {
			r.Header.Write(w)
		}

		if contains {
			fmt.Fprintln(w, "</pre></code>")
		}
	})

	slog.Info("listening", "url", "http://localhost"+*bind)
	log.Fatal(http.ListenAndServe(*bind, mux))
}