This repository has been archived by the owner on Mar 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
69 lines (59 loc) · 2.69 KB
/
handler.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
// Package httpctx provides handlers to work with http and contexts.
package httpctx
import (
"net/http"
"golang.org/x/net/context"
)
// Objects implementing the Handler interface can be
// registered to serve a particular path or subtree
// in the HTTP server.
//
// ServeHTTP should write reply headers and data to the ResponseWriter
// and then return. Returning signals that the request is finished
// and that the HTTP server can move on to the next request on
// the connection.
//
// If ServeHTTP panics, the server (the caller of ServeHTTP) assumes
// that the effect of the panic was isolated to the active request.
// It recovers the panic, logs a stack trace to the server error log,
// and hangs up the connection.
type Handler interface {
ServeHTTP(context.Context, http.ResponseWriter, *http.Request)
}
// The HandlerFunc type is an adapter to allow the use of
// ordinary functions as HTTP handlers. If f is a function
// with the appropriate signature, HandlerFunc(f) is a
// Handler object that calls f.
type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request)
func (f HandlerFunc) ServeHTTP(ctx context.Context, w http.ResponseWriter, r *http.Request) {
f(ctx, w, r)
}
// OldHandler converts from a http.Handler to a httpctx.Handler
func OldHandler(h http.Handler) Handler {
return HandlerFunc(func(_ context.Context, w http.ResponseWriter, r *http.Request) { h.ServeHTTP(w, r) })
}
// OldHandleFunc converts from a http.HandlerFunc to a httpctx.HandlerFunc
func OldHandleFunc(f func(http.ResponseWriter, *http.Request)) HandlerFunc {
return HandlerFunc(func(_ context.Context, w http.ResponseWriter, r *http.Request) { f(w, r) })
}
func rootHandler(ctx context.Context, h Handler) http.Handler {
if h == nil {
return nil
}
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(ctx, w, r)
})
}
// ListenAndServe listens on the TCP network address addr and then calls Serve
// with handler to handle requests on incoming connections.
func ListenAndServe(ctx context.Context, addr string, handler Handler) error {
return http.ListenAndServe(addr, rootHandler(ctx, handler))
}
// ListenAndServeTLS acts identically to ListenAndServe, except that it
// expects HTTPS connections. Additionally, files containing a certificate and
// matching private key for the server must be provided. If the certificate
// is signed by a certificate authority, the certFile should be the concatenation
// of the server's certificate, any intermediates, and the CA's certificate.
func ListenAndServeTLS(ctx context.Context, addr string, certFile string, keyFile string, handler Handler) error {
return http.ListenAndServeTLS(addr, certFile, keyFile, rootHandler(ctx, handler))
}