Skip to content

Commit

Permalink
Merge pull request #6 from swaggest/swguicdn
Browse files Browse the repository at this point in the history
Enable swguicdn build tag
  • Loading branch information
vearutop authored Aug 30, 2019
2 parents 64828b0 + d00ef1d commit 88bb89e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 16 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
12 changes: 12 additions & 0 deletions v3/cdn.go
Original file line number Diff line number Diff line change
@@ -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/"
)
12 changes: 12 additions & 0 deletions v3/embedded.go
Original file line number Diff line number Diff line change
@@ -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 }}"
)
1 change: 1 addition & 0 deletions v3/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down
13 changes: 7 additions & 6 deletions v3/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions v3/index.tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ var indexTpl = `
<head>
<meta charset="UTF-8">
<title>{{ .Title }} - Swagger UI</title>
<link rel="stylesheet" type="text/css" href="{{ .BasePath }}swagger-ui.css">
<link rel="icon" type="image/png" href="{{ .BasePath }}favicon-32x32.png" sizes="32x32"/>
<link rel="icon" type="image/png" href="{{ .BasePath }}favicon-16x16.png" sizes="16x16"/>
<link rel="stylesheet" type="text/css" href="` + assetsBase + `swagger-ui.css">
<link rel="icon" type="image/png" href="` + faviconBase + `favicon-32x32.png" sizes="32x32"/>
<link rel="icon" type="image/png" href="` + faviconBase + `favicon-16x16.png" sizes="16x16"/>
<style>
html {
box-sizing: border-box;
Expand All @@ -32,8 +32,8 @@ var indexTpl = `
<body>
<div id="swagger-ui"></div>
<script src="{{ .BasePath }}swagger-ui-bundle.js"></script>
<script src="{{ .BasePath }}swagger-ui-standalone-preset.js"></script>
<script src="` + assetsBase + `swagger-ui-bundle.js"></script>
<script src="` + assetsBase + `swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function () {
Expand Down
12 changes: 7 additions & 5 deletions v3/static.go

Large diffs are not rendered by default.

0 comments on commit 88bb89e

Please sign in to comment.