-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
93 lines (77 loc) · 1.92 KB
/
server.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
const (
defaultH2Port = "8443"
defaultH2CPort = "8080"
)
func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
// h2spec only requires that we handle GET and POST.
switch r.Method {
case "GET":
for k, v := range r.URL.Query() {
log.Printf("%s: %s\n", k, v)
}
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Received a GET request\n"))
w.Write([]byte(strings.Repeat("Here's some data.", 1024)))
case "POST":
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("ReadAll error: %v\n", err)
return
}
defer r.Body.Close()
log.Printf("Request Body: %q\n", reqBody)
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Received a POST request\n"))
default:
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte(http.StatusText(http.StatusNotImplemented)))
}
}
func lookupEnv(key, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}
func main() {
http.HandleFunc("/", handler)
h2Addr := fmt.Sprintf(":%s", lookupEnv("H2PORT", defaultH2Port))
h2cAddr := fmt.Sprintf(":%s", lookupEnv("H2CPORT", defaultH2CPort))
go func() {
// H2C Server
server := &http.Server{
Addr: h2cAddr,
Handler: h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handler(w, r)
}), &http2.Server{}),
}
if err := server.ListenAndServe(); err != nil {
log.Fatal("ListenAndServe: ", err)
}
}()
go func() {
// H2 Server
err := http.ListenAndServeTLS(h2Addr, "/etc/service-certs/tls.crt", "/etc/service-certs/tls.key", nil)
if err != nil {
log.Fatal("ListenAndServeTLS: ", err)
}
}()
log.Printf("ListenAndServe: %s\n", h2cAddr)
log.Printf("ListenAndServeTLS: %s\n", h2Addr)
select {}
}