aboutsummaryrefslogtreecommitdiff
path: root/cmd/httpdebug/main.go
blob: 8162bc2371b2fd1117c5c5191b5168688009b665 (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
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")
)

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>")
		}

		fmt.Println("---")
		r.Write(io.MultiWriter(w, os.Stdout))

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

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