Skip to content

Commit

Permalink
feat: go:embed static assets (#1733)
Browse files Browse the repository at this point in the history
* feat: go:embed static assets

Go 1.16 introduced the ability to embed files within a generated binary directly with the go tool chain. This simplifies our dependencies and the significantly improves the development workflow for future developers.

Key points to note:

Due to the inability to embed files that do not reside within the local package we need to duplicate our `config.template.yml` within `internal/configuration`.

To avoid issues with the development workflow empty mock files have been included within `internal/server/public_html`. These are substituted with the respective generated files during the CI/CD and build workflows.

* fix(suites): increase ldap suite test timeout

* fix(server): fix swagger asset CSP
  • Loading branch information
nightah committed Feb 21, 2021
1 parent 8bc7ef5 commit 74721a9
Show file tree
Hide file tree
Showing 25 changed files with 535 additions and 187 deletions.
10 changes: 3 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =======================================
# ===== Build image for the backend =====
# =======================================
FROM golang:1.15.7-alpine AS builder-backend
FROM golang:1.16-alpine AS builder-backend

ARG BUILD_TAG
ARG BUILD_COMMIT
Expand All @@ -17,14 +17,10 @@ RUN go mod download

COPY cmd cmd
COPY internal internal
COPY public_html public_html

# Prepare static files to be embedded in Go binary
RUN go get -u aletheia.icu/broccoli && \
cd internal/configuration && \
go generate . && \
cd ../server && \
go generate .
RUN rm -rf internal/server/public_html
COPY public_html internal/server/public_html

# Set the build version and time
RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \
Expand Down
10 changes: 3 additions & 7 deletions Dockerfile.arm32v7
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =======================================
# ===== Build image for the backend =====
# =======================================
FROM golang:1.15.7-alpine AS builder-backend
FROM golang:1.16-alpine AS builder-backend

ARG BUILD_TAG
ARG BUILD_COMMIT
Expand All @@ -20,14 +20,10 @@ RUN go mod download

COPY cmd cmd
COPY internal internal
COPY public_html public_html

# Prepare static files to be embedded in Go binary
RUN go get -u aletheia.icu/broccoli && \
cd internal/configuration && \
go generate . && \
cd ../server && \
go generate .
RUN rm -rf internal/server/public_html
COPY public_html internal/server/public_html

# Set the build version and time
RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \
Expand Down
10 changes: 3 additions & 7 deletions Dockerfile.arm64v8
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# =======================================
# ===== Build image for the backend =====
# =======================================
FROM golang:1.15.7-alpine AS builder-backend
FROM golang:1.16-alpine AS builder-backend

ARG BUILD_TAG
ARG BUILD_COMMIT
Expand All @@ -20,14 +20,10 @@ RUN go mod download

COPY cmd cmd
COPY internal internal
COPY public_html public_html

# Prepare static files to be embedded in Go binary
RUN go get -u aletheia.icu/broccoli && \
cd internal/configuration && \
go generate . && \
cd ../server && \
go generate .
RUN rm -rf internal/server/public_html
COPY public_html internal/server/public_html

# Set the build version and time
RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \
Expand Down
12 changes: 4 additions & 8 deletions Dockerfile.coverage
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN yarn install --frozen-lockfile && INLINE_RUNTIME_CHUNK=false yarn coverage
# =======================================
# ===== Build image for the backend =====
# =======================================
FROM golang:1.15.7-alpine AS builder-backend
FROM golang:1.16-alpine AS builder-backend

ARG BUILD_TAG
ARG BUILD_COMMIT
Expand All @@ -23,20 +23,16 @@ RUN apk --no-cache add gcc musl-dev
WORKDIR /go/src/app

COPY go.mod go.sum config.template.yml ./
COPY --from=builder-frontend /node/src/app/build public_html
COPY api public_html/api

RUN go mod download

COPY cmd cmd
COPY internal internal

# Prepare static files to be embedded in Go binary
RUN go get -u aletheia.icu/broccoli && \
cd internal/configuration && \
go generate . && \
cd ../server && \
go generate .
RUN rm -rf internal/server/public_html
COPY --from=builder-frontend /node/src/app/build internal/server/public_html
COPY api internal/server/public_html/api

# Set the build version and time
RUN echo "Write tag ${BUILD_TAG} and commit ${BUILD_COMMIT} in binary." && \
Expand Down
39 changes: 17 additions & 22 deletions cmd/authelia-scripts/cmd_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ func buildFrontend() {
log.Fatal(err)
}

err = os.Rename("web/build", "./public_html")
cmd = utils.CommandWithStdout("rm", "-rf", "internal/server/public_html")

err = cmd.Run()
if err != nil {
log.Fatal(err)
}

err = os.Rename("web/build", "internal/server/public_html")
if err != nil {
log.Fatal(err)
}
Expand All @@ -56,14 +63,14 @@ func buildSwagger() {
log.Fatal(err)
}

cmd = utils.CommandWithStdout("cp", "-r", "api", "public_html")
cmd = utils.CommandWithStdout("cp", "-r", "api", "internal/server/public_html")

err = cmd.Run()
if err != nil {
log.Fatal(err)
}

cmd = utils.CommandWithStdout("tar", "-C", swaggerDirectory, "--exclude=index.html", "--strip-components=2", "-xf", "v"+swaggerVer+".tar.gz", "swagger-ui-"+swaggerVer+"/dist")
cmd = utils.CommandWithStdout("tar", "-C", "internal/server/public_html/api", "--exclude=index.html", "--strip-components=2", "-xf", "v"+swaggerVer+".tar.gz", "swagger-ui-"+swaggerVer+"/dist")

err = cmd.Run()
if err != nil {
Expand All @@ -78,32 +85,20 @@ func buildSwagger() {
}
}

func generateEmbeddedAssets() {
cmd := utils.CommandWithStdout("go", "get", "-u", "aletheia.icu/broccoli")

err := cmd.Run()
if err != nil {
func cleanAssets() {
if err := os.Rename("internal/server/public_html", OutputDir+"/public_html"); err != nil {
log.Fatal(err)
}

cmd = utils.CommandWithStdout("go", "generate", ".")
cmd.Dir = "internal/configuration"
cmd := utils.CommandWithStdout("mkdir", "-p", "internal/server/public_html/api")

err = cmd.Run()
if err != nil {
if err := cmd.Run(); err != nil {
log.Fatal(err)
}

cmd = utils.CommandWithStdout("go", "generate", ".")
cmd.Dir = "internal/server"

err = cmd.Run()
if err != nil {
log.Fatal(err)
}
cmd = utils.CommandWithStdout("bash", "-c", "touch internal/server/public_html/{index.html,api/index.html,api/openapi.yml}")

err = os.Rename("./public_html", OutputDir+"/public_html")
if err != nil {
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
Expand All @@ -128,6 +123,6 @@ func Build(cobraCmd *cobra.Command, args []string) {
buildSwagger()

log.Debug("Building Authelia Go binary...")
generateEmbeddedAssets()
buildAutheliaBinary()
cleanAssets()
}
1 change: 0 additions & 1 deletion cmd/authelia-scripts/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ var IntermediateDockerImageName = "authelia:dist"
const masterTag = "master"
const stringFalse = "false"
const stringTrue = "true"
const swaggerDirectory = "public_html/api"
const webDirectory = "web"
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module github.com/authelia/authelia

go 1.15
go 1.16

require (
aletheia.icu/broccoli/fs v0.0.0-20200420200651-c5ac961a357a
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/Gurpartap/logrus-stack v0.0.0-20170710170904-89c00d8a28f4
github.com/Workiva/go-datastructures v1.0.52
Expand Down
Loading

0 comments on commit 74721a9

Please sign in to comment.