Skip to content

Commit 70359e5

Browse files
skwairnrwiersma
andauthored
Replace go-bindata with Go embed
Co-authored-by: nrwiersma <[email protected]>
1 parent 7ff13c3 commit 70359e5

27 files changed

+142
-205
lines changed

.github/workflows/build.yaml

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
- name: Build webui
2525
run: |
2626
make generate-webui
27-
tar czvf webui.tar.gz ./static/
27+
tar czvf webui.tar.gz ./webui/static/
2828
2929
- name: Artifact webui
3030
uses: actions/upload-artifact@v2
@@ -66,9 +66,6 @@ jobs:
6666
key: ${{ runner.os }}-build-go-${{ hashFiles('**/go.sum') }}
6767
restore-keys: ${{ runner.os }}-build-go-
6868

69-
- name: Installing dependencies
70-
run: go install github.com/containous/go-bindata/[email protected]
71-
7269
- name: Artifact webui
7370
uses: actions/download-artifact@v2
7471
with:

.github/workflows/test-unit.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ jobs:
3939
key: ${{ runner.os }}-test-unit-go-${{ hashFiles('**/go.sum') }}
4040
restore-keys: ${{ runner.os }}-test-unit-go-
4141

42-
- name: Installing dependencies
43-
run: go install github.com/containous/go-bindata/[email protected]
42+
- name: Avoid generating webui
43+
run: mkdir -p webui/static && touch webui/static/index.html
4444

4545
- name: Tests
4646
run: make test-unit

.github/workflows/validate.yaml

+3-6
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ jobs:
4141
key: ${{ runner.os }}-validate-go-${{ hashFiles('**/go.sum') }}
4242
restore-keys: ${{ runner.os }}-validate-go-
4343

44-
- name: Installing dependencies
45-
run: go install github.com/containous/go-bindata/[email protected]
46-
4744
- name: Install golangci-lint ${{ env.GOLANGCI_LINT_VERSION }}
4845
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION}
4946

5047
- name: Install missspell ${{ env.MISSSPELL_VERSION }}
5148
run: curl -sfL https://raw.githubusercontent.com/client9/misspell/master/install-misspell.sh | sh -s -- -b $(go env GOPATH)/bin ${MISSSPELL_VERSION}
5249

50+
- name: Avoid generating webui
51+
run: mkdir -p webui/static && touch webui/static/index.html
52+
5353
- name: Validate
5454
run: make validate
5555

@@ -81,9 +81,6 @@ jobs:
8181
key: ${{ runner.os }}-validate-generate-go-${{ hashFiles('**/go.sum') }}
8282
restore-keys: ${{ runner.os }}-validate-generate-go-
8383

84-
- name: Installing dependencies
85-
run: go install github.com/containous/go-bindata/[email protected]
86-
8784
- name: go generate
8885
run: |
8986
go generate

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
.DS_Store
66
/dist
77
/webui/.tmp/
8+
/webui/static/
89
/site/
910
/docs/site/
10-
/static/
11-
/autogen/
1211
/traefik
1312
/traefik.toml
1413
/traefik.yml

.semaphore/semaphore.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ global_job_config:
2727
- export GOPROXY=https://proxy.golang.org,direct
2828
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "${GOPATH}/bin" v1.41.1
2929
- curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | bash -s -- -b "${GOPATH}/bin"
30-
- go install github.com/containous/go-bindata/[email protected]
3130
- checkout
3231
- cache restore traefik-$(checksum go.sum)
3332

@@ -41,7 +40,7 @@ blocks:
4140
- name: Test Integration Container
4241
commands:
4342
- make pull-images
44-
- mkdir -p static # Avoid to generate webui
43+
- mkdir -p webui/static && touch webui/static/index.html # Avoid generating webui
4544
- PRE_TARGET="" make binary
4645
- make test-integration-container
4746
- df -h
@@ -61,7 +60,7 @@ blocks:
6160
jobs:
6261
- name: Test Integration Host
6362
commands:
64-
- mkdir -p static # Avoid to generate webui
63+
- mkdir -p webui/static && touch webui/static/index.html # Avoid generating webui
6564
- make test-integration-host
6665
epilogue:
6766
always:

Makefile

+6-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ build-webui-image:
5959

6060
## Generate WebUI
6161
generate-webui:
62-
if [ ! -d "static" ]; then \
62+
if [ ! -d "webui/static" ]; then \
6363
$(MAKE) build-webui-image; \
64-
mkdir -p static; \
65-
docker run --rm -v "$$PWD/static":'/src/static' traefik-webui npm run build:nc; \
66-
docker run --rm -v "$$PWD/static":'/src/static' traefik-webui chown -R $(shell id -u):$(shell id -g) ../static; \
67-
echo 'For more information show `webui/readme.md`' > $$PWD/static/DONT-EDIT-FILES-IN-THIS-DIRECTORY.md; \
64+
mkdir -p webui/static; \
65+
docker run --rm -v "$$PWD/webui/static":'/src/webui/static' traefik-webui npm run build:nc; \
66+
docker run --rm -v "$$PWD/webui/static":'/src/webui/static' traefik-webui chown -R $(shell id -u):$(shell id -g) ./static; \
67+
echo 'For more information show `webui/readme.md`' > $$PWD/webui/static/DONT-EDIT-FILES-IN-THIS-DIRECTORY.md; \
6868
fi
6969

7070
## Build the linux binary
@@ -117,7 +117,7 @@ validate: $(PRE_TARGET)
117117

118118
## Clean up static directory and build a Docker Traefik image
119119
build-image: binary
120-
rm -rf static
120+
rm -rf webui/static
121121
docker build -t $(TRAEFIK_IMAGE) .
122122

123123
## Build a Docker Traefik image

build.Dockerfile

-5
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ RUN mkdir -p /usr/local/bin \
1313
&& curl -fL https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_VERSION}.tgz \
1414
| tar -xzC /usr/local/bin --transform 's#^.+/##x'
1515

16-
# Download go-bindata binary to bin folder in $GOPATH
17-
RUN mkdir -p /usr/local/bin \
18-
&& curl -fsSL -o /usr/local/bin/go-bindata https://github.com/containous/go-bindata/releases/download/v1.0.0/go-bindata \
19-
&& chmod +x /usr/local/bin/go-bindata
20-
2116
# Download golangci-lint binary to bin folder in $GOPATH
2217
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.41.1
2318

cmd/traefik/traefik.go

-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ import (
1616
"time"
1717

1818
"github.com/coreos/go-systemd/daemon"
19-
assetfs "github.com/elazarl/go-bindata-assetfs"
2019
"github.com/go-acme/lego/v4/challenge"
2120
gokitmetrics "github.com/go-kit/kit/metrics"
2221
"github.com/sirupsen/logrus"
2322
"github.com/traefik/paerser/cli"
24-
"github.com/traefik/traefik/v2/autogen/genstatic"
2523
"github.com/traefik/traefik/v2/cmd"
2624
"github.com/traefik/traefik/v2/cmd/healthcheck"
2725
cmdVersion "github.com/traefik/traefik/v2/cmd/version"
@@ -109,10 +107,6 @@ func runCmd(staticConfiguration *static.Configuration) error {
109107
log.WithoutContext().Debugf("Static configuration loaded %s", string(jsonConf))
110108
}
111109

112-
if staticConfiguration.API != nil && staticConfiguration.API.Dashboard {
113-
staticConfiguration.API.DashboardAssets = &assetfs.AssetFS{Asset: genstatic.Asset, AssetInfo: genstatic.AssetInfo, AssetDir: genstatic.AssetDir, Prefix: "static"}
114-
}
115-
116110
if staticConfiguration.Global.CheckNewVersion {
117111
checkNewVersion()
118112
}

docs/content/contributing/building-testing.md

+1-11
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ Requirements:
6464

6565
- `go` v1.16+
6666
- environment variable `GO111MODULE=on`
67-
- [go-bindata](https://github.com/containous/go-bindata) `GO111MODULE=off go get -u github.com/containous/go-bindata/...`
6867

6968
!!! tip "Source Directory"
7069

@@ -101,18 +100,9 @@ Requirements:
101100

102101
Once you've set up your go environment and cloned the source repository, you can build Traefik.
103102

104-
Beforehand, you need to get [go-bindata](https://github.com/containous/go-bindata) (the first time) in order to be able to use the `go generate` command (which is part of the build process).
105-
106-
```bash
107-
cd ~/go/src/github.com/traefik/traefik
108-
109-
# Get go-bindata. (Important: the ellipses are required.)
110-
GO111MODULE=off go get github.com/containous/go-bindata/...
111-
```
112-
113103
```bash
114104
# Generate UI static files
115-
rm -rf static/ autogen/; make generate-webui
105+
rm -rf ./webui/static/; make generate-webui
116106

117107
# required to merge non-code components into the final binary,
118108
# such as the web dashboard/UI

exp.Dockerfile

-4
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ RUN apk --update upgrade \
1919
&& update-ca-certificates \
2020
&& rm -rf /var/cache/apk/*
2121

22-
RUN mkdir -p /usr/local/bin \
23-
&& curl -fsSL -o /usr/local/bin/go-bindata https://github.com/containous/go-bindata/releases/download/v1.0.0/go-bindata \
24-
&& chmod +x /usr/local/bin/go-bindata
25-
2622
WORKDIR /go/src/github.com/traefik/traefik
2723

2824
# Download go modules

generate.go

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
//go:generate mkdir -p autogen
2-
//go:generate rm -vf autogen/genstatic/gen.go
3-
//go:generate mkdir -p static
4-
//go:generate go-bindata -pkg genstatic -nocompress -o autogen/genstatic/gen.go ./static/...
51
//go:generate go run ./internal/
62

73
package main

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ require (
2727
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
2828
github.com/donovanhide/eventsource v0.0.0-20170630084216-b8f31a59085e // indirect
2929
github.com/eapache/channels v1.1.0
30-
github.com/elazarl/go-bindata-assetfs v1.0.0
3130
github.com/fatih/structs v1.1.0
3231
github.com/gambol99/go-marathon v0.0.0-20180614232016-99a156b96fb2
3332
github.com/go-acme/lego/v4 v4.4.0

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,6 @@ github.com/elastic/go-sysinfo v1.1.1/go.mod h1:i1ZYdU10oLNfRzq4vq62BEwD2fH8KaWh6
311311
github.com/elastic/go-windows v1.0.0 h1:qLURgZFkkrYyTTkvYpsZIgf83AUsdIHfvlJaqaZ7aSY=
312312
github.com/elastic/go-windows v1.0.0/go.mod h1:TsU0Nrp7/y3+VwE82FoZF8gC/XFg/Elz6CcloAxnPgU=
313313
github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
314-
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
315-
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
316314
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
317315
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
318316
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=

pkg/anonymize/anonymize.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,7 @@ func reset(field reflect.Value, name string) error {
160160
}
161161
}
162162
case reflect.Interface:
163-
if !field.IsNil() {
164-
return reset(field.Elem(), "")
165-
}
163+
return fmt.Errorf("reset not supported for interface type (for %s field)", name)
166164
default:
167165
// Primitive type
168166
field.Set(reflect.Zero(field.Type()))

pkg/anonymize/anonymize_config_test.go

-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"testing"
88
"time"
99

10-
assetfs "github.com/elazarl/go-bindata-assetfs"
1110
"github.com/stretchr/testify/assert"
1211
"github.com/stretchr/testify/require"
1312
ptypes "github.com/traefik/paerser/types"
@@ -778,18 +777,6 @@ func TestDo_staticConfiguration(t *testing.T) {
778777
Insecure: true,
779778
Dashboard: true,
780779
Debug: true,
781-
DashboardAssets: &assetfs.AssetFS{
782-
Asset: func(path string) ([]byte, error) {
783-
return nil, nil
784-
},
785-
AssetDir: func(path string) ([]string, error) {
786-
return nil, nil
787-
},
788-
AssetInfo: func(path string) (os.FileInfo, error) {
789-
return nil, nil
790-
},
791-
Prefix: "fii",
792-
},
793780
}
794781

795782
config.Metrics = &types.Metrics{

pkg/api/dashboard.go

-59
This file was deleted.

pkg/api/dashboard/dashboard.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package dashboard
2+
3+
import (
4+
"io/fs"
5+
"net/http"
6+
"net/url"
7+
8+
"github.com/gorilla/mux"
9+
"github.com/traefik/traefik/v2/webui"
10+
)
11+
12+
// Handler expose dashboard routes.
13+
type Handler struct {
14+
assets fs.FS // optional assets, to override the webui.FS default
15+
}
16+
17+
// Append adds dashboard routes on the given router, optionally using the given
18+
// assets (or webui.FS otherwise).
19+
func Append(router *mux.Router, customAssets fs.FS) {
20+
assets := customAssets
21+
if assets == nil {
22+
assets = webui.FS
23+
}
24+
// Expose dashboard
25+
router.Methods(http.MethodGet).
26+
Path("/").
27+
HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
28+
http.Redirect(resp, req, safePrefix(req)+"/dashboard/", http.StatusFound)
29+
})
30+
31+
router.Methods(http.MethodGet).
32+
PathPrefix("/dashboard/").
33+
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
34+
// allow iframes from our domains only
35+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
36+
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
37+
http.StripPrefix("/dashboard/", http.FileServer(http.FS(assets))).ServeHTTP(w, r)
38+
})
39+
}
40+
41+
func (g Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
42+
assets := g.assets
43+
if assets == nil {
44+
assets = webui.FS
45+
}
46+
// allow iframes from our domains only
47+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
48+
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
49+
http.FileServer(http.FS(assets)).ServeHTTP(w, r)
50+
}
51+
52+
func safePrefix(req *http.Request) string {
53+
prefix := req.Header.Get("X-Forwarded-Prefix")
54+
if prefix == "" {
55+
return ""
56+
}
57+
58+
parse, err := url.Parse(prefix)
59+
if err != nil {
60+
return ""
61+
}
62+
63+
if parse.Host != "" {
64+
return ""
65+
}
66+
67+
return parse.Path
68+
}

0 commit comments

Comments
 (0)