-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
68 lines (58 loc) · 1.59 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
package main
import (
"log"
"net/http"
"golang.org/x/oauth2"
)
// Error represents a handler error. It provides methods for a HTTP status
// code and embeds the built-in error interface.
type Error interface {
error
Status() int
}
// StatusError represents an error with an associated HTTP status code.
type StatusError struct {
Code int
Err error
}
// Allows StatusError to satisfy the error interface.
func (se StatusError) Error() string {
return se.Err.Error()
}
// Status returns our HTTP status code.
func (se StatusError) Status() int {
return se.Code
}
// Env represents handler dependencies
type Env struct {
DB Store
OAuthConf *oauth2.Config
}
// Handler represents an HTTP handler that can return errors
// and access dependencies in a type-safe way
type Handler struct {
*Env
h func(e *Env, w http.ResponseWriter, r *http.Request) error
}
// NewHandler allocates a new handler
func NewHandler(e *Env, handlerFunc func(e *Env, w http.ResponseWriter, r *http.Request) error) Handler {
return Handler{e, handlerFunc}
}
func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
err := h.h(h.Env, w, r)
if err != nil {
log.Printf("ERROR: %v", err)
switch e := err.(type) {
case Error:
// We can retrieve the status here and write out a specific
// HTTP status code.
log.Printf("HTTP %d - %s", e.Status(), e)
http.Error(w, e.Error(), e.Status())
default:
// Any error types we don't specifically look out for default
// to serving a HTTP 500
http.Error(w, http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError)
}
}
}