-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.go
107 lines (81 loc) · 1.94 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package stdapi
import (
"context"
"crypto/tls"
"net"
"net/http"
"github.com/convox/logger"
"github.com/gorilla/mux"
"github.com/pkg/errors"
)
type RecoverFunc func(error, *Context)
type Server struct {
Check HandlerFunc
Hostname string
Logger *logger.Logger
Recover RecoverFunc
Router *Router
Wrapper func(h http.Handler) http.Handler
middleware []Middleware
server http.Server
}
func (s *Server) HandleNotFound(fn HandlerFunc) {
s.Router.HandleNotFound(fn)
}
func (s *Server) Listen(proto, addr string) error {
s.Logger.At("listen").Logf("hostname=%q proto=%q addr=%q", s.Hostname, proto, addr)
l, err := net.Listen("tcp", addr)
if err != nil {
return errors.WithStack(err)
}
switch proto {
case "h2", "https", "tls":
config := &tls.Config{
MinVersion: tls.VersionTLS12,
}
if proto == "h2" {
config.NextProtos = []string{"h2"}
}
cert, err := generateSelfSignedCertificate(s.Hostname)
if err != nil {
return errors.WithStack(err)
}
config.Certificates = append(config.Certificates, cert)
l = tls.NewListener(l, config)
}
var h http.Handler
if s.Wrapper != nil {
h = s.Wrapper(s)
} else {
h = s
}
s.server = http.Server{Handler: h}
return s.server.Serve(l)
}
func (s *Server) Shutdown(ctx context.Context) error {
return s.server.Shutdown(ctx)
}
func (s *Server) MatcherFunc(fn mux.MatcherFunc) *Router {
return s.Router.MatcherFunc(fn)
}
func (s *Server) Route(method, path string, fn HandlerFunc) Route {
return s.Router.Route(method, path, fn)
}
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.Router.ServeHTTP(w, r)
}
func (s *Server) Subrouter(prefix string, fn func(*Router)) *Router {
r := &Router{
Parent: s.Router,
Router: s.Router.PathPrefix(prefix).Subrouter(),
Server: s,
}
fn(r)
return r
}
func (s *Server) Use(mw Middleware) {
s.Router.Use(mw)
}
func (s *Server) UseHandlerFunc(fn http.HandlerFunc) {
s.Router.UseHandlerFunc(fn)
}