-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
133 lines (121 loc) · 3.95 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package jape
import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/julienschmidt/httprouter"
)
// AuthMiddleware enforces HTTP Basic Authentication on the provided handler.
func AuthMiddleware(handler http.Handler, requiredPass string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if _, password, ok := req.BasicAuth(); !ok || password != requiredPass {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
handler.ServeHTTP(w, req)
})
}
// A Context contains the values relevant to an HTTP handler.
type Context struct {
ResponseWriter http.ResponseWriter
Request *http.Request
PathParams httprouter.Params
}
// Encode writes the JSON encoding of v to the response body.
func (c Context) Encode(v interface{}) {
c.ResponseWriter.Header().Set("Content-Type", "application/json")
// encode nil slices as [] and nil maps as {} (instead of null)
if val := reflect.ValueOf(v); val.Kind() == reflect.Slice && val.Len() == 0 {
c.ResponseWriter.Write([]byte("[]\n"))
return
} else if val.Kind() == reflect.Map && val.Len() == 0 {
c.ResponseWriter.Write([]byte("{}\n"))
return
}
enc := json.NewEncoder(c.ResponseWriter)
enc.SetIndent("", "\t")
enc.Encode(v)
}
// Decode decodes the JSON of the request body into v. If decoding fails, Decode
// writes an error to the response body and returns false.
func (c Context) Decode(v interface{}) bool {
if err := json.NewDecoder(c.Request.Body).Decode(v); err != nil {
http.Error(c.ResponseWriter, fmt.Sprintf("couldn't decode request type (%T): %v", v, err), http.StatusBadRequest)
return false
}
return true
}
// PathParam returns the value of a path parameter. If the parameter is
// undefined, it returns the empty string.
func (c Context) PathParam(param string) string {
return c.PathParams.ByName(param)
}
// DecodeParam decodes the specified path parameter into v, which must be a
// pointer value with one of the following methods:
//
// UnmarshalText([]byte) error
// LoadString(string) error
//
// If decoding fails, DecodeParam writes an error to the response body and
// returns false.
func (c Context) DecodeParam(param string, v interface{}) bool {
var err error
switch v := v.(type) {
case interface{ UnmarshalText([]byte) error }:
err = v.UnmarshalText([]byte(c.PathParam(param)))
case interface{ LoadString(string) error }:
err = v.LoadString(c.PathParam(param))
default:
panic("unsupported type")
}
if err != nil {
http.Error(c.ResponseWriter, fmt.Sprintf("couldn't parse param %q: %v", param, err), http.StatusBadRequest)
return false
}
return true
}
// Check conditionally writes an error. If err is non-nil, Check prefixes it
// with msg, writes it to the response body, and returns true. Otherwise it
// returns false.
func (c Context) Check(msg string, err error) bool {
if err != nil {
http.Error(c.ResponseWriter, fmt.Sprintf("%v: %v", msg, err), http.StatusInternalServerError)
return true
}
return false
}
// A Handler handles HTTP requests.
type Handler func(Context)
func adaptor(h Handler) httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
h(Context{ResponseWriter: w, Request: req, PathParams: ps})
}
}
// Mux returns an http.Handler for the provided set of routes. The map keys must
// contain both the method and path of the route, separated by whitespace, e.g.
// "GET /foo/:bar".
func Mux(routes map[string]Handler) *httprouter.Router {
router := httprouter.New()
for path, h := range routes {
fs := strings.Fields(path)
if len(fs) != 2 {
panic(fmt.Sprintf("invalid route %q", path))
}
method, path := fs[0], fs[1]
switch method {
case "GET":
router.GET(path, adaptor(h))
case "POST":
router.POST(path, adaptor(h))
case "PUT":
router.PUT(path, adaptor(h))
case "DELETE":
router.DELETE(path, adaptor(h))
default:
panic(fmt.Sprintf("unhandled method %q", method))
}
}
return router
}