### Install go-swagger-ui Package Source: https://github.com/alexliesenfeld/go-swagger-ui/blob/main/README.md Use this command to install the go-swagger-ui package into your Go project. ```bash go get github.com/alexliesenfeld/go-swagger-ui ``` -------------------------------- ### Install swui CLI Application Source: https://github.com/alexliesenfeld/go-swagger-ui/blob/main/README.md Install the command-line interface tool for go-swagger-ui. This tool allows you to serve OpenAPI spec files in a browser. ```bash go install github.com/alexliesenfeld/go-swagger-ui/cmd/swui@latest ``` -------------------------------- ### Serve Swagger UI with Custom Options Source: https://github.com/alexliesenfeld/go-swagger-ui/blob/main/README.md This Go code snippet demonstrates how to create a new Swagger UI handler with custom options such as HTML title, spec URL, and enabling 'Try it out' functionality. It then registers this handler for the root path and starts an HTTP server. ```go package main import ( "fmt" swaggerui "github.com/alexliesenfeld/go-swagger-ui" "log" "net/http" ) func main() { http.HandleFunc("/", swaggerui.NewHandler( swaggerui.WithHTMLTitle("My Example Petstore API"), swaggerui.WithSpecURL("https://petstore.swagger.io/v2/swagger.json"), swaggerui.WithTryItOutEnabled(true), swaggerui.WithPersistAuthorization(true), )) fmt.Println("Starting server at port 8080") if err := http.ListenAndServe(":8080", nil); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Get Absolute File System Path Source: https://github.com/alexliesenfeld/go-swagger-ui/blob/main/swagger-ui/dist/README.md Retrieve the absolute path to the swagger-ui-dist directory on the file system. This is useful for configuring static file servers. ```javascript const swaggerUiAssetPath = require("swagger-ui-dist").getAbsoluteFSPath() // then instantiate server that serves files from the swaggerUiAssetPath ``` -------------------------------- ### Serve OpenAPI Spec File with swui CLI Source: https://github.com/alexliesenfeld/go-swagger-ui/blob/main/README.md Use the swui CLI tool to serve a local OpenAPI specification file. This will open the file in a new Swagger UI instance in your browser. The tool also supports live reloading for spec file changes. ```bash swui /path/to/openapi-spec.yaml ``` -------------------------------- ### Import Swagger UI Components Source: https://github.com/alexliesenfeld/go-swagger-ui/blob/main/swagger-ui/dist/README.md Import the main Swagger UI bundle and standalone preset components for use in your application. ```javascript import { SwaggerUIBundle, SwaggerUIStandalonePreset } from "swagger-ui-dist" ``` -------------------------------- ### OAuth2 Redirect Logic Source: https://github.com/alexliesenfeld/go-swagger-ui/blob/main/swagger-ui/dist/oauth2-redirect.html This JavaScript code processes the URL hash or query parameters to extract OAuth2 authorization codes or tokens. It validates the state parameter and calls back to the main Swagger UI window with the authentication results or errors. It handles different OAuth2 flow types and closes the redirect window upon completion. ```javascript 'use strict'; function run () { var oauth2 = window.opener.swaggerUIRedirectOauth2; var sentState = oauth2.state; var redirectUrl = oauth2.redirectUrl; var isValid, qp, arr; if (/code|token|error/.test(window.location.hash)) { qp = window.location.hash.substring(1).replace('?', '&'); } else { qp = location.search.substring(1); } arr = qp.split("&"); arr.forEach(function (v,i,_arr) { _arr[i] = '"' + v.replace('=', '":"') + '"';}); qp = qp ? JSON.parse('{' + arr.join() + '}', function (key, value) { return key === "" ? value : decodeURIComponent(value); } ) : {}; isValid = qp.state === sentState; if (( oauth2.auth.schema.get("flow") === "accessCode" || oauth2.auth.schema.get("flow") === "authorizationCode" || oauth2.auth.schema.get("flow") === "authorization_code" ) && !oauth2.auth.code) { if (!isValid) { oauth2.errCb({ authId: oauth2.auth.name, source: "auth", level: "warning", message: "Authorization may be unsafe, passed state was changed in server. The passed state wasn't returned from auth server." }); } if (qp.code) { delete oauth2.state; oauth2.auth.code = qp.code; oauth2.callback({auth: oauth2.auth, redirectUrl: redirectUrl}); } else { let oauthErrorMsg; if (qp.error) { oauthErrorMsg = "["+qp.error+"]: " + (qp.error_description ? qp.error_description+ ". " : "no accessCode received from the server. ") + (qp.error_uri ? "More info: "+qp.error_uri : ""); } oauth2.errCb({ authId: oauth2.auth.name, source: "auth", level: "error", message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server." }); } } else { oauth2.callback({auth: oauth2.auth, token: qp, isValid: isValid, redirectUrl: redirectUrl}); } window.close(); } if (document.readyState !== 'loading') { run(); } else { document.addEventListener('DOMContentLoaded', function () { run(); }); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.