Skip to content

Commit

Permalink
Use go1.16 embed to store static assets (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Dec 27, 2020
1 parent dbace59 commit 4840a28
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 108 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ update:
curl https://raw.githubusercontent.com/swagger-api/swagger-ui/$(SWAGGER_UI_VERSION)/dist/oauth2-redirect.html -o ./v3/static/oauth2-redirect.html
curl https://raw.githubusercontent.com/swagger-api/swagger-ui/$(SWAGGER_UI_VERSION)/dist/favicon-32x32.png -o ./v3/static/favicon-32x32.png
curl https://raw.githubusercontent.com/swagger-api/swagger-ui/$(SWAGGER_UI_VERSION)/dist/favicon-16x16.png -o ./v3/static/favicon-16x16.png
rm -rf ./v3/static/*.gz
go run ./v3/gen/gen.go
zopfli --i50 ./v3/static/*.js && rm -f ./v3/static/*.js
zopfli --i50 ./v3/static/*.css && rm -f ./v3/static/*.css
zopfli --i50 ./v3/static/*.html && rm -f ./v3/static/*.html
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
Package `swgui` (Swagger UI) provides HTTP handler to serve Swagger UI.
All assets are embedded in Go source code, so just build and run.

Static assets for `v3` are built from Swagger UI [v3.35.1](https://github.com/swagger-api/swagger-ui/releases/tag/v3.35.1).
Static assets for `v3` are built from Swagger UI [v3.38.0](https://github.com/swagger-api/swagger-ui/releases/tag/v3.38.0).

[CDN-based](https://cdnjs.com/libraries/swagger-ui) `v3cdn` uses Swagger UI [v3.35.1](https://github.com/swagger-api/swagger-ui/releases/tag/v3.35.1).
[CDN-based](https://cdnjs.com/libraries/swagger-ui) `v3cdn` uses Swagger UI [v3.38.0](https://github.com/swagger-api/swagger-ui/releases/tag/v3.38.0).

## How to use

Expand Down
4 changes: 0 additions & 4 deletions generate.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ require (
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd
github.com/vearutop/statigz v1.0.0
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/tools v0.0.0-20190830172400-56125e7d709e // indirect
Expand Down
19 changes: 17 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
github.com/andybalholm/brotli v1.0.1 h1:KqhlKozYbRtJvsPrrEeXcO+N2l6NYT5A2QAFmSULpEc=
github.com/andybalholm/brotli v1.0.1/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
github.com/bool64/dev v0.1.10 h1:4L6eLD+qo1QgWDy+Y7OhJxi/gLwOAuV1rd07noMc3dU=
github.com/bool64/dev v0.1.10/go.mod h1:pn52JC52uSgpazChx9CeXyG+S3sW2V36HHoLNBbscdg=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0 h1:mj/nMDAwTBiaCqMEs4cYCqF7pO6Np7vhy1D1wcQGz+E=
github.com/shurcooL/httpgzip v0.0.0-20190720172056-320755c1c1b0/go.mod h1:919LwcH0M7/W4fcZ0/jy0qGght1GIhqyS/EgWGH2j5Q=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0=
github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vearutop/statigz v1.0.0 h1:x6AjXC2837Tdl8/69Kwro1UftpDmIKciIqSKuIvV6Gk=
github.com/vearutop/statigz v1.0.0/go.mod h1:A6aweyvs+WWmDz2WNRxT5wcpNKtKXk6QBDAAPMxIk0o=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 h1:eDrdRpKgkcCqKZQwyZRyeFZgfqt37SL7Kv3tok06cKE=
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190830172400-56125e7d709e h1:jlbOw43fjYcUlLDKNNmKTeCQFyjkKEW/iUK7w7017XE=
golang.org/x/tools v0.0.0-20190830172400-56125e7d709e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 5 additions & 1 deletion swgui-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"log"
"net/http"

"github.com/swaggest/swgui/v3"
Expand All @@ -28,5 +29,8 @@ func main() {

http.Handle("/", h)
fmt.Printf("Listening at %s:%d\n", host, port)
http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil)
err := http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil)
if err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion v3/cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import "net/http"
var staticServer http.Handler

const (
assetsBase = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.35.1/"
assetsBase = "https://cdnjs.cloudflare.com/ajax/libs/swagger-ui/3.38.0/"
faviconBase = "https://petstore.swagger.io/"
)
2 changes: 1 addition & 1 deletion v3/embedded.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build !swguicdn
// +build !swguicdn,!go1.16

package v3

Expand Down
15 changes: 15 additions & 0 deletions v3/embedded_go1.16.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build go1.16,!swguicdn

package v3

import (
"github.com/swaggest/swgui/v3/static"
"github.com/vearutop/statigz"
)

var staticServer = statigz.FileServer(static.FS)

const (
assetsBase = "{{ .BasePath }}"
faviconBase = "{{ .BasePath }}"
)
2 changes: 1 addition & 1 deletion v3/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {
var fs http.FileSystem = http.Dir("./v3/static")

err := vfsgen.Generate(fs, vfsgen.Options{
BuildTags: "!swguicdn",
BuildTags: "!swguicdn,!go1.16",
PackageName: "v3",
Filename: "v3/static.go",
})
Expand Down
26 changes: 17 additions & 9 deletions v3/static.go

Large diffs are not rendered by default.

74 changes: 0 additions & 74 deletions v3/static/oauth2-redirect.html

This file was deleted.

Binary file added v3/static/oauth2-redirect.html.gz
Binary file not shown.
11 changes: 11 additions & 0 deletions v3/static/static.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// +build go1.16

// Package static contains files to embed.
package static

import (
"embed"
)

//go:embed *.png *.gz
var FS embed.FS
3 changes: 0 additions & 3 deletions v3/static/swagger-ui-bundle.js

This file was deleted.

Binary file added v3/static/swagger-ui-bundle.js.gz
Binary file not shown.
3 changes: 0 additions & 3 deletions v3/static/swagger-ui-standalone-preset.js

This file was deleted.

Binary file added v3/static/swagger-ui-standalone-preset.js.gz
Binary file not shown.
4 changes: 0 additions & 4 deletions v3/static/swagger-ui.css

This file was deleted.

Binary file added v3/static/swagger-ui.css.gz
Binary file not shown.
3 changes: 0 additions & 3 deletions v3/static/swagger-ui.js

This file was deleted.

Binary file added v3/static/swagger-ui.js.gz
Binary file not shown.

0 comments on commit 4840a28

Please sign in to comment.