summaryrefslogtreecommitdiff
path: root/vendor/github.com/go-openapi/runtime/middleware/ui_options.go
diff options
context:
space:
mode:
authorLibravatar Terin Stock <terinjokes@gmail.com>2025-03-09 17:47:56 +0100
committerLibravatar Terin Stock <terinjokes@gmail.com>2025-03-10 01:59:49 +0100
commit3ac1ee16f377d31a0fb80c8dae28b6239ac4229e (patch)
treef61faa581feaaeaba2542b9f2b8234a590684413 /vendor/github.com/go-openapi/runtime/middleware/ui_options.go
parent[chore] update URLs to forked source (diff)
downloadgotosocial-3ac1ee16f377d31a0fb80c8dae28b6239ac4229e.tar.xz
[chore] remove vendor
Diffstat (limited to 'vendor/github.com/go-openapi/runtime/middleware/ui_options.go')
-rw-r--r--vendor/github.com/go-openapi/runtime/middleware/ui_options.go173
1 files changed, 0 insertions, 173 deletions
diff --git a/vendor/github.com/go-openapi/runtime/middleware/ui_options.go b/vendor/github.com/go-openapi/runtime/middleware/ui_options.go
deleted file mode 100644
index b86efa008..000000000
--- a/vendor/github.com/go-openapi/runtime/middleware/ui_options.go
+++ /dev/null
@@ -1,173 +0,0 @@
-package middleware
-
-import (
- "bytes"
- "encoding/gob"
- "fmt"
- "net/http"
- "path"
- "strings"
-)
-
-const (
- // constants that are common to all UI-serving middlewares
- defaultDocsPath = "docs"
- defaultDocsURL = "/swagger.json"
- defaultDocsTitle = "API Documentation"
-)
-
-// uiOptions defines common options for UI serving middlewares.
-type uiOptions struct {
- // BasePath for the UI, defaults to: /
- BasePath string
-
- // Path combines with BasePath to construct the path to the UI, defaults to: "docs".
- Path string
-
- // SpecURL is the URL of the spec document.
- //
- // Defaults to: /swagger.json
- SpecURL string
-
- // Title for the documentation site, default to: API documentation
- Title string
-
- // Template specifies a custom template to serve the UI
- Template string
-}
-
-// toCommonUIOptions converts any UI option type to retain the common options.
-//
-// This uses gob encoding/decoding to convert common fields from one struct to another.
-func toCommonUIOptions(opts interface{}) uiOptions {
- var buf bytes.Buffer
- enc := gob.NewEncoder(&buf)
- dec := gob.NewDecoder(&buf)
- var o uiOptions
- err := enc.Encode(opts)
- if err != nil {
- panic(err)
- }
-
- err = dec.Decode(&o)
- if err != nil {
- panic(err)
- }
-
- return o
-}
-
-func fromCommonToAnyOptions[T any](source uiOptions, target *T) {
- var buf bytes.Buffer
- enc := gob.NewEncoder(&buf)
- dec := gob.NewDecoder(&buf)
- err := enc.Encode(source)
- if err != nil {
- panic(err)
- }
-
- err = dec.Decode(target)
- if err != nil {
- panic(err)
- }
-}
-
-// UIOption can be applied to UI serving middleware, such as Context.APIHandler or
-// Context.APIHandlerSwaggerUI to alter the defaut behavior.
-type UIOption func(*uiOptions)
-
-func uiOptionsWithDefaults(opts []UIOption) uiOptions {
- var o uiOptions
- for _, apply := range opts {
- apply(&o)
- }
-
- return o
-}
-
-// WithUIBasePath sets the base path from where to serve the UI assets.
-//
-// By default, Context middleware sets this value to the API base path.
-func WithUIBasePath(base string) UIOption {
- return func(o *uiOptions) {
- if !strings.HasPrefix(base, "/") {
- base = "/" + base
- }
- o.BasePath = base
- }
-}
-
-// WithUIPath sets the path from where to serve the UI assets (i.e. /{basepath}/{path}.
-func WithUIPath(pth string) UIOption {
- return func(o *uiOptions) {
- o.Path = pth
- }
-}
-
-// WithUISpecURL sets the path from where to serve swagger spec document.
-//
-// This may be specified as a full URL or a path.
-//
-// By default, this is "/swagger.json"
-func WithUISpecURL(specURL string) UIOption {
- return func(o *uiOptions) {
- o.SpecURL = specURL
- }
-}
-
-// WithUITitle sets the title of the UI.
-//
-// By default, Context middleware sets this value to the title found in the API spec.
-func WithUITitle(title string) UIOption {
- return func(o *uiOptions) {
- o.Title = title
- }
-}
-
-// WithTemplate allows to set a custom template for the UI.
-//
-// UI middleware will panic if the template does not parse or execute properly.
-func WithTemplate(tpl string) UIOption {
- return func(o *uiOptions) {
- o.Template = tpl
- }
-}
-
-// EnsureDefaults in case some options are missing
-func (r *uiOptions) EnsureDefaults() {
- if r.BasePath == "" {
- r.BasePath = "/"
- }
- if r.Path == "" {
- r.Path = defaultDocsPath
- }
- if r.SpecURL == "" {
- r.SpecURL = defaultDocsURL
- }
- if r.Title == "" {
- r.Title = defaultDocsTitle
- }
-}
-
-// serveUI creates a middleware that serves a templated asset as text/html.
-func serveUI(pth string, assets []byte, next http.Handler) http.Handler {
- return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
- if path.Clean(r.URL.Path) == pth {
- rw.Header().Set(contentTypeHeader, "text/html; charset=utf-8")
- rw.WriteHeader(http.StatusOK)
- _, _ = rw.Write(assets)
-
- return
- }
-
- if next != nil {
- next.ServeHTTP(rw, r)
-
- return
- }
-
- rw.Header().Set(contentTypeHeader, "text/plain")
- rw.WriteHeader(http.StatusNotFound)
- _, _ = rw.Write([]byte(fmt.Sprintf("%q not found", pth)))
- })
-}