2020-03-31 23:02:16 -07:00
|
|
|
package huma
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2020-08-26 22:25:47 -07:00
|
|
|
|
|
|
|
"github.com/Jeffail/gabs/v2"
|
|
|
|
"github.com/go-chi/chi"
|
2020-03-31 23:02:16 -07:00
|
|
|
)
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
// Resource represents an API resource attached to a router at a specific path
|
|
|
|
// (URI template). Resources can have operations or subresources attached to
|
|
|
|
// them.
|
2020-03-31 23:02:16 -07:00
|
|
|
type Resource struct {
|
2020-04-13 22:47:40 -07:00
|
|
|
path string
|
2020-08-26 22:25:47 -07:00
|
|
|
mux chi.Router
|
|
|
|
router *Router
|
2020-04-13 22:47:40 -07:00
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
subResources []*Resource
|
|
|
|
operations []*Operation
|
2020-04-13 22:47:40 -07:00
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
tags []string
|
2022-05-09 12:17:02 -07:00
|
|
|
|
|
|
|
hidden bool
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:21:01 -08:00
|
|
|
func (r *Resource) toOpenAPI(components *oaComponents) *gabs.Container {
|
2022-05-09 12:17:02 -07:00
|
|
|
if r.hidden {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
doc := gabs.New()
|
2020-03-31 23:02:16 -07:00
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
for _, sub := range r.subResources {
|
2020-12-18 16:21:01 -08:00
|
|
|
doc.Merge(sub.toOpenAPI(components))
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
for _, op := range r.operations {
|
2020-12-18 16:21:01 -08:00
|
|
|
opValue := op.toOpenAPI(components)
|
2020-10-02 11:36:29 -07:00
|
|
|
|
|
|
|
if len(r.tags) > 0 {
|
|
|
|
opValue.Set(r.tags, "tags")
|
|
|
|
}
|
|
|
|
|
|
|
|
doc.Set(opValue, r.path, strings.ToLower(op.method))
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
return doc
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
// Operation creates a new HTTP operation with the given method at this resource.
|
2020-09-17 20:45:12 -07:00
|
|
|
func (r *Resource) Operation(method, operationID, docs string, responses ...Response) *Operation {
|
|
|
|
op := newOperation(r, method, operationID, docs, responses)
|
2020-08-26 22:25:47 -07:00
|
|
|
r.operations = append(r.operations, op)
|
2020-04-23 09:00:54 -07:00
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
return op
|
2020-04-23 09:00:54 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
// Post creates a new HTTP POST operation at this resource.
|
2020-09-17 20:45:12 -07:00
|
|
|
func (r *Resource) Post(operationID, docs string, responses ...Response) *Operation {
|
|
|
|
return r.Operation(http.MethodPost, operationID, docs, responses...)
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
// Head creates a new HTTP HEAD operation at this resource.
|
2020-09-17 20:45:12 -07:00
|
|
|
func (r *Resource) Head(operationID, docs string, responses ...Response) *Operation {
|
|
|
|
return r.Operation(http.MethodHead, operationID, docs, responses...)
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
// Get creates a new HTTP GET operation at this resource.
|
2020-09-17 20:45:12 -07:00
|
|
|
func (r *Resource) Get(operationID, docs string, responses ...Response) *Operation {
|
|
|
|
return r.Operation(http.MethodGet, operationID, docs, responses...)
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
// Put creates a new HTTP PUT operation at this resource.
|
2020-09-17 20:45:12 -07:00
|
|
|
func (r *Resource) Put(operationID, docs string, responses ...Response) *Operation {
|
|
|
|
return r.Operation(http.MethodPut, operationID, docs, responses...)
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
// Patch creates a new HTTP PATCH operation at this resource.
|
2020-09-17 20:45:12 -07:00
|
|
|
func (r *Resource) Patch(operationID, docs string, responses ...Response) *Operation {
|
|
|
|
return r.Operation(http.MethodPatch, operationID, docs, responses...)
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-26 22:25:47 -07:00
|
|
|
// Delete creates a new HTTP DELETE operation at this resource.
|
2020-09-17 20:45:12 -07:00
|
|
|
func (r *Resource) Delete(operationID, docs string, responses ...Response) *Operation {
|
|
|
|
return r.Operation(http.MethodDelete, operationID, docs, responses...)
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-08-27 10:00:25 -07:00
|
|
|
// Middleware adds a new standard middleware to this resource, so it will
|
2020-08-26 22:25:47 -07:00
|
|
|
// apply to requests at the resource's path (including any subresources).
|
|
|
|
// Middleware can also be applied at the router level to apply to all requests.
|
2020-08-27 10:00:25 -07:00
|
|
|
func (r *Resource) Middleware(middlewares ...func(next http.Handler) http.Handler) {
|
2020-08-26 22:25:47 -07:00
|
|
|
r.mux.Use(middlewares...)
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
|
|
|
|
2020-09-10 08:52:46 -07:00
|
|
|
// SubResource creates a new resource attached to this resource. The passed
|
|
|
|
// path will be appended to the resource's existing path. The path can
|
|
|
|
// include parameters, e.g. `/things/{thing-id}`. Each resource path must
|
|
|
|
// be unique.
|
|
|
|
func (r *Resource) SubResource(path string) *Resource {
|
2020-08-26 22:25:47 -07:00
|
|
|
sub := &Resource{
|
2020-09-10 08:52:46 -07:00
|
|
|
path: r.path + path,
|
|
|
|
mux: r.mux.Route(path, nil),
|
2022-02-23 10:46:14 -08:00
|
|
|
router: r.router,
|
2020-08-26 22:25:47 -07:00
|
|
|
subResources: []*Resource{},
|
|
|
|
operations: []*Operation{},
|
|
|
|
tags: append([]string{}, r.tags...),
|
|
|
|
}
|
|
|
|
|
|
|
|
r.subResources = append(r.subResources, sub)
|
|
|
|
|
|
|
|
return sub
|
2020-03-31 23:02:16 -07:00
|
|
|
}
|
2020-06-15 08:40:38 -07:00
|
|
|
|
2020-08-27 10:00:25 -07:00
|
|
|
// Tags appends to the list of tags, used for documentation.
|
|
|
|
func (r *Resource) Tags(names ...string) {
|
2020-08-26 22:25:47 -07:00
|
|
|
r.tags = append(r.tags, names...)
|
2020-06-15 08:40:38 -07:00
|
|
|
}
|
2022-05-09 12:17:02 -07:00
|
|
|
|
|
|
|
// Hidden removes this resource from the OpenAPI spec and documentation. It
|
|
|
|
// is intended to be used for things like health check endpoints.
|
|
|
|
func (r *Resource) Hidden() {
|
|
|
|
r.hidden = true
|
|
|
|
}
|
2022-07-26 12:35:07 +08:00
|
|
|
|
|
|
|
// GetMux gets the Chi router for this resource.
|
|
|
|
func (r *Resource) GetMux() chi.Router {
|
|
|
|
return r.mux
|
|
|
|
}
|