aboutsummaryrefslogtreecommitdiff
path: root/cmd/uncle-ted/main.go
blob: 4ded5457ebc229326981b58bf41efbc20b201497 (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
package main

import (
	"embed"
	"flag"
	"log"
	"log/slog"
	"net/http"

	"github.com/NYTimes/gziphandler"
	"within.website/x/internal"
)

var (
	bind = flag.String("bind", ":2836", "TCP port to bind on")

	//go:embed bomb.txt.gz.gz
	kaboom []byte

	//go:embed bee-movie.txt
	static embed.FS
)

func main() {
	internal.HandleStartup()

	beeMovieHDLR, err := gziphandler.GzipHandlerWithOpts(gziphandler.CompressionLevel(9))
	if err != nil {
		log.Fatal(err)
	}

	http.Handle("/bee-movie", beeMovieHDLR(http.HandlerFunc(beeMovie)))
	http.HandleFunc("/gzip-bomb", gzipBomb)

	slog.Info("started up", "url", "http://localhost"+*bind)
	log.Fatal(http.ListenAndServe(*bind, nil))
}

func beeMovie(w http.ResponseWriter, r *http.Request) {
	http.ServeFileFS(w, r, static, "bee-movie.txt")
}

func gzipBomb(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	w.Header().Set("Content-Encoding", "gzip")
	w.Header().Set("Transfer-Encoding", "gzip")

	w.WriteHeader(http.StatusOK)
	w.Write(kaboom)
}