-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
119 lines (101 loc) · 2.72 KB
/
main.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bytes"
_ "embed"
"fmt"
"html/template"
"io"
"math"
"math/rand"
"net/http"
"os"
"strings"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
func handleErr(message string, err error) {
if err != nil {
fmt.Printf("[Error] %s: %s\n", message, err)
os.Exit(1)
}
}
var (
//go:embed assets/cf_logo.png
imgContents []byte
)
func serveImgFactory() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/jpeg")
w.Header().Set("X-Http-Version", r.Proto)
_, err := io.Copy(w, bytes.NewBuffer(imgContents))
handleErr("Writing image", err)
}
}
type TemplateArgs struct {
HttpVersion string
Images template.HTML
Script template.JS
}
func generateImage() string {
fingerprint := rand.Int63n(math.MaxInt64)
return fmt.Sprintf( /* language=html */ `<img src="/images/test_%[1]d.png" alt="Test Image %[1]d" height="20" />`, fingerprint)
}
func generateImages(quantity int) string {
var sb strings.Builder
for i := 0; i < quantity; i++ {
sb.WriteString(generateImage())
}
return sb.String()
}
var (
//go:embed assets/load_time.js
loadTimeScript string
//go:embed assets/index.gohtml
indexHTML string
)
func serveHTML(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
templateArgs := TemplateArgs{
HttpVersion: r.Proto,
Images: template.HTML(generateImages(1000)),
Script: template.JS(loadTimeScript),
}
err := template.Must(template.New("").Parse(indexHTML)).Execute(w, templateArgs)
handleErr("Writing HTML", err)
}
func serveHttp2(address string) {
http2Server := &http.Server{Addr: address}
fmt.Printf("Listening on https://%s", address)
err := http2Server.ListenAndServeTLS("server.crt", "server.key")
handleErr("Serving H2", err)
}
func serveH2c(address string) {
http2Server := &http.Server{Addr: address, Handler: h2c.NewHandler(http.DefaultServeMux, &http2.Server{})}
fmt.Printf("Listening on http://%s", address)
err := http2Server.ListenAndServe()
handleErr("Serving H2C", err)
}
func serveHttp1(address string) {
http2Server := &http.Server{Addr: address}
fmt.Printf("Listening on http://%s", address)
err := http2Server.ListenAndServe()
handleErr("Serving HTTP1", err)
}
func main() {
http.HandleFunc("/", serveHTML)
serveImg := serveImgFactory()
http.HandleFunc("/images/", serveImg)
proto := strings.ToLower(os.Getenv("PROTO"))
port := os.Getenv("PORT")
address := fmt.Sprintf("0.0.0.0:%s", port)
switch proto {
case "h2":
serveHttp2(address)
case "h2c":
serveH2c(address)
case "http1":
serveHttp1(address)
default:
fmt.Println("No protocol set. Specify PROTO environment variable. Valid values: 'h2', 'h2c', 'http1'.")
}
}