From d00ef1dbfa7e7c32d1ca205373afafa374c97bfd Mon Sep 17 00:00:00 2001 From: Viacheslav Poturaev Date: Fri, 30 Aug 2019 22:42:01 +0200 Subject: [PATCH] Enable swguicdn build tag --- README.md | 8 ++++++++ v3/cdn.go | 12 ++++++++++++ v3/embedded.go | 12 ++++++++++++ v3/gen/gen.go | 1 + v3/handler.go | 13 +++++++------ v3/index.tpl.go | 10 +++++----- v3/static.go | 12 +++++++----- 7 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 v3/cdn.go create mode 100644 v3/embedded.go diff --git a/README.md b/README.md index c8e5cc3..0707333 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,14 @@ func main() { } ``` +## Use CDN for assets + +In order to reduce binary size you can import `github.com/swaggest/swgui/v3cdn` to use CDN hosted assets. + +Also you can use `swguicdn` build tag to enable CDN mode for `github.com/swaggest/swgui/v3` import. + +Be aware that CDN mode may be considered inappropriate for security or networking reasons. + ## Run as standalone server Install `swgui-server` diff --git a/v3/cdn.go b/v3/cdn.go new file mode 100644 index 0000000..308d8d2 --- /dev/null +++ b/v3/cdn.go @@ -0,0 +1,12 @@ +// +build swguicdn + +package v3 + +import "net/http" + +var staticServer http.Handler + +const ( + assetsBase = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.23.6/" + faviconBase = "https://petstore.swagger.io/" +) diff --git a/v3/embedded.go b/v3/embedded.go new file mode 100644 index 0000000..5c7b80e --- /dev/null +++ b/v3/embedded.go @@ -0,0 +1,12 @@ +// +build !swguicdn + +package v3 + +import "github.com/shurcooL/httpgzip" + +var staticServer = httpgzip.FileServer(assets, httpgzip.FileServerOptions{}) + +const ( + assetsBase = "{{ .BasePath }}" + faviconBase = "{{ .BasePath }}" +) diff --git a/v3/gen/gen.go b/v3/gen/gen.go index 04a93cb..38ef213 100644 --- a/v3/gen/gen.go +++ b/v3/gen/gen.go @@ -11,6 +11,7 @@ func main() { var fs http.FileSystem = http.Dir("./v3/static") err := vfsgen.Generate(fs, vfsgen.Options{ + BuildTags: "!swguicdn", PackageName: "v3", Filename: "v3/static.go", }) diff --git a/v3/handler.go b/v3/handler.go index 83ab2da..67d7926 100644 --- a/v3/handler.go +++ b/v3/handler.go @@ -5,12 +5,9 @@ import ( "html/template" "net/http" - "github.com/shurcooL/httpgzip" "github.com/swaggest/swgui" ) -var staticServer = httpgzip.FileServer(assets, httpgzip.FileServerOptions{}) - // Handler handle swagger UI request type Handler struct { swgui.Config @@ -31,7 +28,9 @@ func NewHandler(title, swaggerJSONPath string, basePath string) *Handler { j, _ := json.Marshal(h.Config) h.ConfigJson = template.JS(j) h.tpl, _ = template.New("index").Parse(indexTpl) - h.staticServer = http.StripPrefix(basePath, staticServer) + if staticServer != nil { + h.staticServer = http.StripPrefix(basePath, staticServer) + } return h } @@ -43,13 +42,15 @@ func NewHandlerWithConfig(config swgui.Config) *Handler { j, _ := json.Marshal(h.Config) h.ConfigJson = template.JS(j) h.tpl, _ = template.New("index").Parse(indexTpl) - h.staticServer = http.StripPrefix(h.BasePath, staticServer) + if staticServer != nil { + h.staticServer = http.StripPrefix(h.BasePath, staticServer) + } return h } // ServeHTTP implement http.Handler interface, to handle swagger UI request func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != h.BasePath { + if r.URL.Path != h.BasePath && h.staticServer != nil { h.staticServer.ServeHTTP(w, r) return } diff --git a/v3/index.tpl.go b/v3/index.tpl.go index 6c58f00..eca3257 100644 --- a/v3/index.tpl.go +++ b/v3/index.tpl.go @@ -6,9 +6,9 @@ var indexTpl = ` {{ .Title }} - Swagger UI - - - + + +