-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
143 lines (127 loc) · 4.04 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
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
138
139
140
141
142
143
package oasis
import (
"net/http"
"github.com/evgenymarkov/oasis/openapi3"
"github.com/evgenymarkov/oasis/rendering"
)
// API struct is a wrapper on top of multiplexer for registering operations.
type API struct {
mux *http.ServeMux
config *APIConfig
document *openapi3.Document
}
// NewAPI method creates new API struct and fills it up.
func NewAPI(
mux *http.ServeMux,
config *APIConfig,
document *openapi3.Document,
) *API {
mux.HandleFunc(
http.MethodGet+" "+config.DocumentPath,
rendering.NewDocumentHandler(document),
)
mux.HandleFunc(
http.MethodGet+" "+config.SwaggerUIPath,
rendering.NewSwaggerUIHandler(rendering.SwaggerUIConfig{
BaseURL: config.SwaggerUIPath,
PageTitle: config.SwaggerUITitle,
Document: document,
}),
)
for _, staticFile := range rendering.GetSwaggerUIStaticFiles() {
mux.HandleFunc(
http.MethodGet+" "+config.SwaggerUIPath+"/"+staticFile,
rendering.NewSwaggerUIStaticHandler(staticFile),
)
}
return &API{
mux: mux,
config: config,
document: document,
}
}
// Document method returns pointer to serializable openapi3.Document.
// This document can be saved in the file system and used for code generation.
func (a *API) Document() *openapi3.Document {
return a.document
}
// Get method registers an handler for HTTP GET requests matching the pattern
// and updates operations table in OpenAPI document.
func (a *API) Get(
pattern string,
handler http.HandlerFunc,
operation *openapi3.Operation,
) {
a.mux.HandleFunc(http.MethodGet+" "+pattern, handler)
a.document.AddOperation(pattern, http.MethodGet, operation)
}
// Head method registers an handler for HTTP HEAD requests matching the pattern
// and updates operations table in OpenAPI document.
func (a *API) Head(
pattern string,
handler http.HandlerFunc,
operation *openapi3.Operation,
) {
a.mux.HandleFunc(http.MethodHead+" "+pattern, handler)
a.document.AddOperation(pattern, http.MethodHead, operation)
}
// Post method registers an handler for HTTP POST requests matching the pattern
// and updates operations table in OpenAPI document.
func (a *API) Post(
pattern string,
handler http.HandlerFunc,
operation *openapi3.Operation,
) {
a.mux.HandleFunc(http.MethodPost+" "+pattern, handler)
a.document.AddOperation(pattern, http.MethodPost, operation)
}
// Put method registers an handler for HTTP PUT requests matching the pattern
// and updates operations table in OpenAPI document.
func (a *API) Put(
pattern string,
handler http.HandlerFunc,
operation *openapi3.Operation,
) {
a.mux.HandleFunc(http.MethodPut+" "+pattern, handler)
a.document.AddOperation(pattern, http.MethodPut, operation)
}
// Patch method registers an handler for HTTP PATCH requests matching the pattern
// and updates operations table in OpenAPI document.
func (a *API) Patch(
pattern string,
handler http.HandlerFunc,
operation *openapi3.Operation,
) {
a.mux.HandleFunc(http.MethodPatch+" "+pattern, handler)
a.document.AddOperation(pattern, http.MethodPatch, operation)
}
// Delete method registers an handler for HTTP DELETE requests matching the pattern
// and updates operations table in OpenAPI document.
func (a *API) Delete(
pattern string,
handler http.HandlerFunc,
operation *openapi3.Operation,
) {
a.mux.HandleFunc(http.MethodDelete+" "+pattern, handler)
a.document.AddOperation(pattern, http.MethodDelete, operation)
}
// Options method registers an handler for HTTP OPTIONS requests matching the pattern
// and updates operations table in OpenAPI document.
func (a *API) Options(
pattern string,
handler http.HandlerFunc,
operation *openapi3.Operation,
) {
a.mux.HandleFunc(http.MethodOptions+" "+pattern, handler)
a.document.AddOperation(pattern, http.MethodOptions, operation)
}
// Trace method registers an handler for HTTP TRACE requests matching the pattern
// and updates operations table in OpenAPI document.
func (a *API) Trace(
pattern string,
handler http.HandlerFunc,
operation *openapi3.Operation,
) {
a.mux.HandleFunc(http.MethodTrace+" "+pattern, handler)
a.document.AddOperation(pattern, http.MethodTrace, operation)
}