Skip to content

Commit

Permalink
Replace go-bindata with Go embed
Browse files Browse the repository at this point in the history
Co-authored-by: nrwiersma <[email protected]>
  • Loading branch information
skwair and nrwiersma authored Sep 15, 2021
1 parent 7ff13c3 commit 70359e5
Show file tree
Hide file tree
Showing 27 changed files with 142 additions and 205 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Build webui
run: |
make generate-webui
tar czvf webui.tar.gz ./static/
tar czvf webui.tar.gz ./webui/static/
- name: Artifact webui
uses: actions/upload-artifact@v2
Expand Down Expand Up @@ -66,9 +66,6 @@ jobs:
key: ${{ runner.os }}-build-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-build-go-

- name: Installing dependencies
run: go install github.com/containous/go-bindata/[email protected]

- name: Artifact webui
uses: actions/download-artifact@v2
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test-unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ jobs:
key: ${{ runner.os }}-test-unit-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-test-unit-go-

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

- name: Tests
run: make test-unit
9 changes: 3 additions & 6 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ jobs:
key: ${{ runner.os }}-validate-go-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-validate-go-

- name: Installing dependencies
run: go install github.com/containous/go-bindata/[email protected]

- name: Install golangci-lint ${{ env.GOLANGCI_LINT_VERSION }}
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin ${GOLANGCI_LINT_VERSION}

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

- name: Avoid generating webui
run: mkdir -p webui/static && touch webui/static/index.html

- name: Validate
run: make validate

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

- name: Installing dependencies
run: go install github.com/containous/go-bindata/[email protected]

- name: go generate
run: |
go generate
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
.DS_Store
/dist
/webui/.tmp/
/webui/static/
/site/
/docs/site/
/static/
/autogen/
/traefik
/traefik.toml
/traefik.yml
Expand Down
5 changes: 2 additions & 3 deletions .semaphore/semaphore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ global_job_config:
- export GOPROXY=https://proxy.golang.org,direct
- curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "${GOPATH}/bin" v1.41.1
- curl -sfL https://install.goreleaser.com/github.com/goreleaser/goreleaser.sh | bash -s -- -b "${GOPATH}/bin"
- go install github.com/containous/go-bindata/[email protected]
- checkout
- cache restore traefik-$(checksum go.sum)

Expand All @@ -41,7 +40,7 @@ blocks:
- name: Test Integration Container
commands:
- make pull-images
- mkdir -p static # Avoid to generate webui
- mkdir -p webui/static && touch webui/static/index.html # Avoid generating webui
- PRE_TARGET="" make binary
- make test-integration-container
- df -h
Expand All @@ -61,7 +60,7 @@ blocks:
jobs:
- name: Test Integration Host
commands:
- mkdir -p static # Avoid to generate webui
- mkdir -p webui/static && touch webui/static/index.html # Avoid generating webui
- make test-integration-host
epilogue:
always:
Expand Down
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ build-webui-image:

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

## Build the linux binary
Expand Down Expand Up @@ -117,7 +117,7 @@ validate: $(PRE_TARGET)

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

## Build a Docker Traefik image
Expand Down
5 changes: 0 additions & 5 deletions build.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ RUN mkdir -p /usr/local/bin \
&& curl -fL https://download.docker.com/linux/static/stable/x86_64/docker-${DOCKER_VERSION}.tgz \
| tar -xzC /usr/local/bin --transform 's#^.+/##x'

# Download go-bindata binary to bin folder in $GOPATH
RUN mkdir -p /usr/local/bin \
&& curl -fsSL -o /usr/local/bin/go-bindata https://github.com/containous/go-bindata/releases/download/v1.0.0/go-bindata \
&& chmod +x /usr/local/bin/go-bindata

# Download golangci-lint binary to bin folder in $GOPATH
RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.41.1

Expand Down
6 changes: 0 additions & 6 deletions cmd/traefik/traefik.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ import (
"time"

"github.com/coreos/go-systemd/daemon"
assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/go-acme/lego/v4/challenge"
gokitmetrics "github.com/go-kit/kit/metrics"
"github.com/sirupsen/logrus"
"github.com/traefik/paerser/cli"
"github.com/traefik/traefik/v2/autogen/genstatic"
"github.com/traefik/traefik/v2/cmd"
"github.com/traefik/traefik/v2/cmd/healthcheck"
cmdVersion "github.com/traefik/traefik/v2/cmd/version"
Expand Down Expand Up @@ -109,10 +107,6 @@ func runCmd(staticConfiguration *static.Configuration) error {
log.WithoutContext().Debugf("Static configuration loaded %s", string(jsonConf))
}

if staticConfiguration.API != nil && staticConfiguration.API.Dashboard {
staticConfiguration.API.DashboardAssets = &assetfs.AssetFS{Asset: genstatic.Asset, AssetInfo: genstatic.AssetInfo, AssetDir: genstatic.AssetDir, Prefix: "static"}
}

if staticConfiguration.Global.CheckNewVersion {
checkNewVersion()
}
Expand Down
12 changes: 1 addition & 11 deletions docs/content/contributing/building-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ Requirements:

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

!!! tip "Source Directory"

Expand Down Expand Up @@ -101,18 +100,9 @@ Requirements:

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

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).

```bash
cd ~/go/src/github.com/traefik/traefik

# Get go-bindata. (Important: the ellipses are required.)
GO111MODULE=off go get github.com/containous/go-bindata/...
```

```bash
# Generate UI static files
rm -rf static/ autogen/; make generate-webui
rm -rf ./webui/static/; make generate-webui

# required to merge non-code components into the final binary,
# such as the web dashboard/UI
Expand Down
4 changes: 0 additions & 4 deletions exp.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ RUN apk --update upgrade \
&& update-ca-certificates \
&& rm -rf /var/cache/apk/*

RUN mkdir -p /usr/local/bin \
&& curl -fsSL -o /usr/local/bin/go-bindata https://github.com/containous/go-bindata/releases/download/v1.0.0/go-bindata \
&& chmod +x /usr/local/bin/go-bindata

WORKDIR /go/src/github.com/traefik/traefik

# Download go modules
Expand Down
4 changes: 0 additions & 4 deletions generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
//go:generate mkdir -p autogen
//go:generate rm -vf autogen/genstatic/gen.go
//go:generate mkdir -p static
//go:generate go-bindata -pkg genstatic -nocompress -o autogen/genstatic/gen.go ./static/...
//go:generate go run ./internal/

package main
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/docker/libtrust v0.0.0-20160708172513-aabc10ec26b7 // indirect
github.com/donovanhide/eventsource v0.0.0-20170630084216-b8f31a59085e // indirect
github.com/eapache/channels v1.1.0
github.com/elazarl/go-bindata-assetfs v1.0.0
github.com/fatih/structs v1.1.0
github.com/gambol99/go-marathon v0.0.0-20180614232016-99a156b96fb2
github.com/go-acme/lego/v4 v4.4.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,6 @@ github.com/elastic/go-sysinfo v1.1.1/go.mod h1:i1ZYdU10oLNfRzq4vq62BEwD2fH8KaWh6
github.com/elastic/go-windows v1.0.0 h1:qLURgZFkkrYyTTkvYpsZIgf83AUsdIHfvlJaqaZ7aSY=
github.com/elastic/go-windows v1.0.0/go.mod h1:TsU0Nrp7/y3+VwE82FoZF8gC/XFg/Elz6CcloAxnPgU=
github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/elazarl/go-bindata-assetfs v1.0.0 h1:G/bYguwHIzWq9ZoyUQqrjTmJbbYn3j3CKKpKinvZLFk=
github.com/elazarl/go-bindata-assetfs v1.0.0/go.mod h1:v+YaWX3bdea5J/mo8dSETolEo7R71Vk1u8bnjau5yw4=
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
Expand Down
4 changes: 1 addition & 3 deletions pkg/anonymize/anonymize.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ func reset(field reflect.Value, name string) error {
}
}
case reflect.Interface:
if !field.IsNil() {
return reset(field.Elem(), "")
}
return fmt.Errorf("reset not supported for interface type (for %s field)", name)
default:
// Primitive type
field.Set(reflect.Zero(field.Type()))
Expand Down
13 changes: 0 additions & 13 deletions pkg/anonymize/anonymize_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"testing"
"time"

assetfs "github.com/elazarl/go-bindata-assetfs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ptypes "github.com/traefik/paerser/types"
Expand Down Expand Up @@ -778,18 +777,6 @@ func TestDo_staticConfiguration(t *testing.T) {
Insecure: true,
Dashboard: true,
Debug: true,
DashboardAssets: &assetfs.AssetFS{
Asset: func(path string) ([]byte, error) {
return nil, nil
},
AssetDir: func(path string) ([]string, error) {
return nil, nil
},
AssetInfo: func(path string) (os.FileInfo, error) {
return nil, nil
},
Prefix: "fii",
},
}

config.Metrics = &types.Metrics{
Expand Down
59 changes: 0 additions & 59 deletions pkg/api/dashboard.go

This file was deleted.

68 changes: 68 additions & 0 deletions pkg/api/dashboard/dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package dashboard

import (
"io/fs"
"net/http"
"net/url"

"github.com/gorilla/mux"
"github.com/traefik/traefik/v2/webui"
)

// Handler expose dashboard routes.
type Handler struct {
assets fs.FS // optional assets, to override the webui.FS default
}

// Append adds dashboard routes on the given router, optionally using the given
// assets (or webui.FS otherwise).
func Append(router *mux.Router, customAssets fs.FS) {
assets := customAssets
if assets == nil {
assets = webui.FS
}
// Expose dashboard
router.Methods(http.MethodGet).
Path("/").
HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
http.Redirect(resp, req, safePrefix(req)+"/dashboard/", http.StatusFound)
})

router.Methods(http.MethodGet).
PathPrefix("/dashboard/").
HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// allow iframes from our domains only
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
http.StripPrefix("/dashboard/", http.FileServer(http.FS(assets))).ServeHTTP(w, r)
})
}

func (g Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
assets := g.assets
if assets == nil {
assets = webui.FS
}
// allow iframes from our domains only
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-src
w.Header().Set("Content-Security-Policy", "frame-src 'self' https://traefik.io https://*.traefik.io;")
http.FileServer(http.FS(assets)).ServeHTTP(w, r)
}

func safePrefix(req *http.Request) string {
prefix := req.Header.Get("X-Forwarded-Prefix")
if prefix == "" {
return ""
}

parse, err := url.Parse(prefix)
if err != nil {
return ""
}

if parse.Host != "" {
return ""
}

return parse.Path
}
Loading

0 comments on commit 70359e5

Please sign in to comment.