/
api.go
82 lines (70 loc) · 2.85 KB
/
api.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
package rest
import (
"context"
"fmt"
"io"
"net/http"
)
// API is the root of a REST API abstraction.
// The two main responsibilities are specification generation (GenerateSpec function), and
// Server handler generation (GenerateServer function).
type API struct {
ID string
Version string
Description string
Title string
Host string
BasePath string
ResourceCollection
}
const (
// URIReservedChar is the reserved char set as in RFC 3986 (https://tools.ietf.org/html/rfc3986#section-2.2)
URIReservedChar = ":/?#[]@!$&'()*+,;="
// ResourceReservedChar is URIReservedChar + "{}" that represents the charset that is not allowed on resource names.
// The curly brackets are reserved for internally denoting a path URI parameter, also used by API specification formats.
ResourceReservedChar = URIReservedChar + "{}"
)
// NewAPI creates a new API.
func NewAPI(basePath, hostname, title, version string) API {
return API{BasePath: basePath, Host: hostname, Title: title, Version: version}
}
// GenerateSpec will generate the API specification using APISpecGenerator interface implementation (g),
// and will write into a io.Writer implementation (w)
func (a API) GenerateSpec(w io.Writer, g APISpecGenerator) {
g.GenerateAPISpec(w, a)
}
// GenerateServer generates a http.Handler using a ServerGenerator implementation (g)
func (a API) GenerateServer(g ServerGenerator) http.Handler {
resourcesCheck(a.resources)
server := g.GenerateServer(a)
return inputGetFunctionsMiddleware(g.GetURIParam(), server)
}
func inputGetFunctionsMiddleware(getURIParamFunc func(r *http.Request, key string) string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), InputContextKey("uriparamfunc"), getURIParamFunc)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func resourcesCheck(res map[string]Resource) {
for _, resource := range res {
for _, m := range resource.methods {
for _, resp := range m.Responses() {
httpResponseCodeCheck(resp.Code(), m.HTTPMethod, resource.path)
parameterOperationCheck(m, resource.path)
}
}
resourcesCheck(resource.resources)
}
}
// An invalid code will panic in an implementation of http server (see checkWriteHeaderCode function on https://golang.org/src/net/http/server.go)
// We will check this before the server is up and running, and avoid an unexpected panic.
func httpResponseCodeCheck(code int, httpMethod string, path string) {
if code < 100 || code > 999 {
panic(fmt.Sprintf("GenerateServer check error: invalid response code %v on method: %v of resource: %v", code, httpMethod, path))
}
}
func parameterOperationCheck(m *Method, path string) {
if m.MethodOperation.Operation == nil {
panic(fmt.Sprintf("GenerateServer check error: resource %s method %s doesn't have an operation.", path, m.HTTPMethod))
}
}