-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
52 lines (43 loc) · 1007 Bytes
/
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
package httpdump
import (
"fmt"
"net/http"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
type ServeOpts struct {
Port int
Ip string
Cert string
Key string
Format string
}
func Serve(opts ServeOpts) error {
handler := NewHandler(opts.Format)
addr := fmt.Sprintf("%s:%d", opts.Ip, opts.Port)
if len(opts.Cert) > 0 {
if len(opts.Key) == 0 {
return fmt.Errorf("cert is specified, but key is not")
}
return serveH2(handler, addr, opts.Cert, opts.Key)
} else {
return serveH2c(handler, addr)
}
}
func serveH2c(h http.Handler, addr string) error {
h2s := &http2.Server{}
server := &http.Server{
Addr: addr,
Handler: h2c.NewHandler(h, h2s),
}
fmt.Printf("Listening http://%s...\n", addr)
return server.ListenAndServe()
}
func serveH2(h http.Handler, addr string, cert string, key string) error {
server := &http.Server{
Addr: addr,
Handler: h,
}
fmt.Printf("Listening https://%s...\n", addr)
return server.ListenAndServeTLS(cert, key)
}