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
134
135
136
137
|
package sleepy
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
)
const (
GET = "GET"
POST = "POST"
PUT = "PUT"
DELETE = "DELETE"
)
// GetSupported is the interface that provides the Get
// method a resource must support to receive HTTP GETs.
type GetSupported interface {
Get(url.Values, http.Header) (int, interface{}, http.Header)
}
// PostSupported is the interface that provides the Post
// method a resource must support to receive HTTP POSTs.
type PostSupported interface {
Post(url.Values, http.Header) (int, interface{}, http.Header)
}
// PutSupported is the interface that provides the Put
// method a resource must support to receive HTTP PUTs.
type PutSupported interface {
Put(url.Values, http.Header) (int, interface{}, http.Header)
}
// DeleteSupported is the interface that provides the Delete
// method a resource must support to receive HTTP DELETEs.
type DeleteSupported interface {
Delete(url.Values, http.Header) (int, interface{}, http.Header)
}
// An API manages a group of resources by routing requests
// to the correct method on a matching resource and marshalling
// the returned data to JSON for the HTTP response.
//
// You can instantiate multiple APIs on separate ports. Each API
// will manage its own set of resources.
type API struct {
mux *http.ServeMux
muxInitialized bool
}
// NewAPI allocates and returns a new API.
func NewAPI() *API {
return &API{}
}
func (api *API) requestHandler(resource interface{}) http.HandlerFunc {
return func(rw http.ResponseWriter, request *http.Request) {
if request.ParseForm() != nil {
rw.WriteHeader(http.StatusBadRequest)
return
}
var handler func(url.Values, http.Header) (int, interface{}, http.Header)
switch request.Method {
case GET:
if resource, ok := resource.(GetSupported); ok {
handler = resource.Get
}
case POST:
if resource, ok := resource.(PostSupported); ok {
handler = resource.Post
}
case PUT:
if resource, ok := resource.(PutSupported); ok {
handler = resource.Put
}
case DELETE:
if resource, ok := resource.(DeleteSupported); ok {
handler = resource.Delete
}
}
if handler == nil {
rw.WriteHeader(http.StatusMethodNotAllowed)
return
}
code, data, header := handler(request.Form, request.Header)
content, err := json.MarshalIndent(data, "", " ")
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
return
}
for name, values := range header {
for _, value := range values {
rw.Header().Add(name, value)
}
}
rw.WriteHeader(code)
rw.Write(content)
}
}
// Mux returns the http.ServeMux used by an API. If a ServeMux has
// does not yet exist, a new one will be created and returned.
func (api *API) Mux() *http.ServeMux {
if api.muxInitialized {
return api.mux
} else {
api.mux = http.NewServeMux()
api.muxInitialized = true
return api.mux
}
}
// AddResource adds a new resource to an API. The API will route
// requests that match one of the given paths to the matching HTTP
// method on the resource.
func (api *API) AddResource(resource interface{}, paths ...string) {
for _, path := range paths {
api.Mux().HandleFunc(path, api.requestHandler(resource))
}
}
// Start causes the API to begin serving requests on the given port.
func (api *API) Start(port int) error {
if !api.muxInitialized {
return errors.New("You must add at least one resource to this API.")
}
portString := fmt.Sprintf(":%d", port)
return http.ListenAndServe(portString, api.Mux())
}
|