From d231c34c505f5b6c0c7ab315392180a2274fef15 Mon Sep 17 00:00:00 2001 From: Zhomart Mukhamejanov Date: Fri, 13 Jan 2023 15:28:47 -0800 Subject: [PATCH] Add ScriptSrcs to add extra scripts URLs to load Summary: This allows importing plugins from local and external hosts. ``` r.Get("/swagger/*", httpSwagger.Handler( httpSwagger.ScriptSrc("/static/swagger-helper.js"), httpSwagger.ScriptSrc("https://unpkg.com/swagger-ui-plugin-disable-try-it-out-without-servers@1.0.0/build/index.js"), httpSwagger.Plugins([]string{"DisableTryItOutWithoutServersPlugin"}, httpSwagger.URL("http://localhost:1323/swagger/doc.json"), //The url pointing to API definition )) ``` Closes #83 Test Plan: Updated existing tests. Added example in example/go-chi/ --- example/go-chi/docs/docs.go | 71 +++++---------------------- example/go-chi/main.go | 4 ++ example/go-chi/static/myplugin.js | 80 +++++++++++++++++++++++++++++++ swagger.go | 11 +++++ swagger_test.go | 11 +++++ 5 files changed, 119 insertions(+), 58 deletions(-) create mode 100644 example/go-chi/static/myplugin.js diff --git a/example/go-chi/docs/docs.go b/example/go-chi/docs/docs.go index 0e14e39..7c63032 100644 --- a/example/go-chi/docs/docs.go +++ b/example/go-chi/docs/docs.go @@ -1,17 +1,10 @@ -// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT +// Package docs GENERATED BY SWAG; DO NOT EDIT // This file was generated by swaggo/swag package docs -import ( - "bytes" - "encoding/json" - "strings" - "text/template" +import "github.com/swaggo/swag" - "github.com/swaggo/swag" -) - -var doc = `{ +const docTemplate = `{ "schemes": {{ marshal .Schemes }}, "swagger": "2.0", "info": { @@ -34,56 +27,18 @@ var doc = `{ "paths": {} }` -type swaggerInfo struct { - Version string - Host string - BasePath string - Schemes []string - Title string - Description string -} - // SwaggerInfo holds exported Swagger Info so clients can modify it -var SwaggerInfo = swaggerInfo{ - Version: "1.0", - Host: "petstore.swagger.io", - BasePath: "/v2", - Schemes: []string{}, - Title: "Swagger Example API", - Description: "This is a sample server Petstore server.", -} - -type s struct{} - -func (s *s) ReadDoc() string { - sInfo := SwaggerInfo - sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1) - - t, err := template.New("swagger_info").Funcs(template.FuncMap{ - "marshal": func(v interface{}) string { - a, _ := json.Marshal(v) - return string(a) - }, - "escape": func(v interface{}) string { - // escape tabs - str := strings.Replace(v.(string), "\t", "\\t", -1) - // replace " with \", and if that results in \\", replace that with \\\" - str = strings.Replace(str, "\"", "\\\"", -1) - return strings.Replace(str, "\\\\\"", "\\\\\\\"", -1) - }, - }).Parse(doc) - if err != nil { - return doc - } - - var tpl bytes.Buffer - if err := t.Execute(&tpl, sInfo); err != nil { - return doc - } - - return tpl.String() +var SwaggerInfo = &swag.Spec{ + Version: "1.0", + Host: "petstore.swagger.io", + BasePath: "/v2", + Schemes: []string{}, + Title: "Swagger Example API", + Description: "This is a sample server Petstore server.", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, } func init() { - swag.Register(swag.Name, &s{}) + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) } diff --git a/example/go-chi/main.go b/example/go-chi/main.go index 436cb95..9f73bf1 100644 --- a/example/go-chi/main.go +++ b/example/go-chi/main.go @@ -29,6 +29,10 @@ func main() { r.Get("/swagger/*", httpSwagger.Handler( httpSwagger.BeforeScript("console.log('before 1')"), httpSwagger.BeforeScript("console.log('before 2')"), + httpSwagger.ScriptSrc("https://unpkg.com/htm@3.1.1/dist/htm.js"), + httpSwagger.ScriptSrc("https://unpkg.com/react@17.0.2/umd/react.production.min.js"), + httpSwagger.ScriptSrc("/static/myplugin.js"), + httpSwagger.Plugins([]string{"MyPlugin"}), httpSwagger.URL("http://localhost:1323/swagger/doc.json"), //The url pointing to API definition )) diff --git a/example/go-chi/static/myplugin.js b/example/go-chi/static/myplugin.js new file mode 100644 index 0000000..a564b1c --- /dev/null +++ b/example/go-chi/static/myplugin.js @@ -0,0 +1,80 @@ +// Swagger UI Version: 4.15.5 + +'use strict'; + +function _typeof(obj) { + if (typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol') { + _typeof = function _typeof(obj) { + return typeof obj; + }; + } else { + _typeof = function _typeof(obj) { + return obj && + typeof Symbol === 'function' && + obj.constructor === Symbol && + obj !== Symbol.prototype + ? 'symbol' + : typeof obj; + }; + } + return _typeof(obj); +} + +// From: https://raw.githubusercontent.com/chilts/umd-template/master/template.js +(function (f) { + // module name and requires + var name = 'MyPlugin'; + var requires = []; // CommonJS + + if ( + (typeof exports === 'undefined' ? 'undefined' : _typeof(exports)) === 'object' && + typeof module !== 'undefined' + ) { + module.exports = f.apply( + null, + requires.map(function (r) { + return require(r); + }) + ); // RequireJS + } else if (typeof define === 'function' && define.amd) { + define(requires, f); // ` +func ScriptSrc(src string) func(*Config) { + return func(c *Config) { + c.ScriptSrcs = append(c.ScriptSrcs, src) + } +} + // UIConfig specifies additional SwaggerUIBundle config object properties. func UIConfig(props map[string]string) func(*Config) { return func(c *Config) { @@ -259,6 +267,9 @@ const indexTempl = ` +{{- range $script := .ScriptSrcs }} + +{{- end}} + +