-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger.go
181 lines (148 loc) · 4.94 KB
/
swagger.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"bytes"
_ "embed"
"encoding/json"
"io"
"net/http"
"strings"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/wzshiming/openapi/spec"
"github.com/Luzifer/twitch-bot/v3/plugins"
)
var (
swaggerDoc = spec.OpenAPI{
OpenAPI: "3.0.3", // This generator uses v3 of OpenAPI standard
Info: &spec.Info{
Title: "Twitch-Bot public API",
Version: "v1",
},
Servers: []*spec.Server{
{URL: "./", Description: "Current bot instance"},
},
Paths: make(spec.Paths),
Components: &spec.Components{
Responses: map[string]*spec.Response{
"genericErrorResponse": spec.TextPlainResponse(nil).WithDescription("An error occurred: See error message"),
"inputErrorResponse": spec.TextPlainResponse(nil).WithDescription("Data sent to API is invalid: See error message"),
"notFoundResponse": spec.TextPlainResponse(nil).WithDescription("Document was not found or insufficient permissions"),
},
},
}
//go:embed swagger.html
swaggerHTML []byte
)
func init() {
secConfigEditor := spec.APIKeyAuth("Authorization", spec.InHeader)
secConfigEditor.Description = "Authorization token issued by Twitch"
secWriteAuth := spec.APIKeyAuth("Authorization", spec.InHeader)
secWriteAuth.Description = "Authorization token stored in the config"
swaggerDoc.Components.SecuritySchemes = map[string]*spec.SecurityScheme{
"configEditor": secConfigEditor,
"writeAuth": secWriteAuth,
}
}
func handleSwaggerHTML(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/html")
if _, err := io.Copy(w, bytes.NewReader(swaggerHTML)); err != nil {
http.Error(w, errors.Wrap(err, "writing frontend").Error(), http.StatusInternalServerError)
}
}
func handleSwaggerRequest(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(swaggerDoc); err != nil {
http.Error(w, errors.Wrap(err, "rendering documentation").Error(), http.StatusInternalServerError)
}
}
//nolint:funlen,gocyclo // Makes no sense to split just to spare a little complexity
func registerSwaggerRoute(route plugins.HTTPRouteRegistrationArgs) error {
fullPath := strings.Join([]string{
"",
route.Module,
strings.TrimLeft(route.Path, "/"),
}, "/")
pi, ok := swaggerDoc.Paths[fullPath]
if !ok {
pi = &spec.PathItem{}
for _, param := range route.RouteParams {
pi.Parameters = append(
pi.Parameters,
spec.PathParam(param.Name, spec.StringProperty()).WithDescription(param.Description),
)
}
swaggerDoc.Paths[fullPath] = pi
}
op := &spec.Operation{
Summary: route.Name,
Description: route.Description,
Tags: []string{route.Module},
Responses: map[string]*spec.Response{
"204": spec.TextPlainResponse(nil).WithDescription("Successful execution without response object"),
"404": spec.RefResponse("notFoundResponse"),
"500": spec.RefResponse("genericErrorResponse"),
},
}
switch {
case route.RequiresEditorsAuth:
op.Security = []map[string]spec.SecurityRequirement{
{"configEditor": {}},
}
case route.RequiresWriteAuth:
op.Security = []map[string]spec.SecurityRequirement{
{"writeAuth": {}},
}
}
switch route.ResponseType {
case plugins.HTTPRouteResponseTypeJSON:
op.Responses["200"] = spec.JSONResponse(nil).WithDescription("Successful execution with JSON object response")
case plugins.HTTPRouteResponseTypeMultiple:
op.Responses["200"] = (&spec.Response{}).WithDescription("Successful execution with variable response based on Accept header")
for _, a := range route.Accept {
op.Responses["200"].AddContent(a, &spec.MediaType{Schema: nil})
}
case plugins.HTTPRouteResponseTypeNo200:
// We don't add a 200 then
case plugins.HTTPRouteResponseTypeTextPlain:
op.Responses["200"] = spec.TextPlainResponse(nil).WithDescription("Successful execution with plain text response")
}
for _, param := range route.QueryParams {
var ps *spec.Schema
switch param.Type {
case "bool", "boolean":
ps = spec.BooleanProperty()
case "int", "int64":
ps = spec.Int64Property()
case "duration", "string":
ps = spec.StringProperty()
default:
log.WithFields(log.Fields{"module": route.Module, "type": param.Type}).Warn("Module registered unhandled query-param type")
ps = spec.StringProperty()
}
specParam := spec.QueryParam(param.Name, ps).
WithDescription(param.Description)
specParam.Required = param.Required
op.Parameters = append(
op.Parameters,
specParam,
)
}
switch route.Method {
case http.MethodDelete:
pi.Delete = op
case http.MethodGet:
pi.Get = op
case http.MethodPatch:
op.Responses["400"] = spec.RefResponse("inputErrorResponse")
pi.Patch = op
case http.MethodPost:
op.Responses["400"] = spec.RefResponse("inputErrorResponse")
pi.Post = op
case http.MethodPut:
op.Responses["400"] = spec.RefResponse("inputErrorResponse")
pi.Put = op
default:
return errors.Errorf("assignment for %q is not implemented", route.Method)
}
return nil
}