From a3d60e584b07a74432f822f9052629a6c313ca21 Mon Sep 17 00:00:00 2001 From: Bjorn Jorgensen <20801364+iobear@users.noreply.github.com> Date: Sun, 4 Feb 2024 22:50:04 +0100 Subject: [PATCH] Embed web files 1.5.1 --- .github/workflows/e2e-single-test.yml | 1 + CHANGELOG.md | 4 ++++ build/package/Dockerfile | 3 +-- cmd/dashgoat/helper.go | 18 ++++++++++++++++++ cmd/dashgoat/main.go | 15 ++++++++++++--- cmd/dashgoat/version.go | 2 +- 6 files changed, 37 insertions(+), 6 deletions(-) diff --git a/.github/workflows/e2e-single-test.yml b/.github/workflows/e2e-single-test.yml index de0afab..2b4766d 100644 --- a/.github/workflows/e2e-single-test.yml +++ b/.github/workflows/e2e-single-test.yml @@ -24,6 +24,7 @@ jobs: run: | mkdir -p $GITHUB_WORKSPACE/dashgoatbuild cp -R cmd/dashgoat/* $GITHUB_WORKSPACE/dashgoatbuild/ + cp -R web $GITHUB_WORKSPACE/dashgoatbuild/ cp go.* $GITHUB_WORKSPACE/dashgoatbuild/ cd $GITHUB_WORKSPACE/dashgoatbuild go get -d -v github.com/labstack/echo/... diff --git a/CHANGELOG.md b/CHANGELOG.md index 894a10f..08f1d89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # Changelog +## [v1.5.1] - 2024-02-04 +New: + - Embed webfiles to single binary + ## [v1.5.0] - 2024-02-02 New: - Prometheus /metrics endpoint. diff --git a/build/package/Dockerfile b/build/package/Dockerfile index 98b9182..b76077f 100644 --- a/build/package/Dockerfile +++ b/build/package/Dockerfile @@ -3,6 +3,7 @@ RUN apk add build-base git WORKDIR /go/src/dashgoatbuild COPY cmd/dashgoat/* /go/src/dashgoatbuild/ COPY go.* /go/src/dashgoatbuild/ +COPY web/ /go/src/dashgoatbuild/web/ #COPY cmd/dashgoat/dashgoat.yaml /go/src/dashgoatbuild/ RUN go get -d -v github.com/labstack/echo/... RUN go get gopkg.in/validator.v2 @@ -15,8 +16,6 @@ FROM alpine:latest COPY --from=0 /go/src/dashgoatbuild/dashgoat /app/dashgoat #COPY --from=0 /go/src/dashgoatbuild/dashgoat.yaml /dashgoat.yaml -COPY web/ /web/ -#RUN ls -la /web/* EXPOSE 2000 diff --git a/cmd/dashgoat/helper.go b/cmd/dashgoat/helper.go index 7ade53a..6bd463c 100644 --- a/cmd/dashgoat/helper.go +++ b/cmd/dashgoat/helper.go @@ -7,6 +7,9 @@ package main import ( + "io/fs" + "log" + "net/http" "os" "strconv" "strings" @@ -92,3 +95,18 @@ func str2bool(str_to_convert string) bool { return boolValue } + +func getFileSystem(useOS bool) http.FileSystem { + if useOS { + log.Print("using live mode") + return http.FS(os.DirFS("web")) + } + + log.Print("using embed mode") + fsys, err := fs.Sub(embededFiles, "web") + if err != nil { + panic(err) + } + + return http.FS(fsys) +} diff --git a/cmd/dashgoat/main.go b/cmd/dashgoat/main.go index eb77bf3..31396ce 100644 --- a/cmd/dashgoat/main.go +++ b/cmd/dashgoat/main.go @@ -7,8 +7,11 @@ package main import ( + "embed" "flag" "fmt" + "net/http" + "os" "strings" "github.com/labstack/echo-contrib/echoprometheus" @@ -17,6 +20,9 @@ import ( "github.com/prometheus/client_golang/prometheus" ) +//go:embed web +var embededFiles embed.FS + var config Configer var buddy_cli Buddy var nsconfig string @@ -56,10 +62,13 @@ func main() { config.WebPath = "/" + config.WebPath } - e.HideBanner = true + // Serving embedded static files + useOS := len(os.Args) > 1 && os.Args[1] == "live" + assetHandler := http.FileServer(getFileSystem(useOS)) + e.GET("/", echo.WrapHandler(assetHandler)) + e.GET("/*", echo.WrapHandler(http.StripPrefix("/", assetHandler))) - //static files - e.Static(config.WebPath, "web") + e.HideBanner = true if config.WebLog == "on" { e.Use(middleware.Logger()) diff --git a/cmd/dashgoat/version.go b/cmd/dashgoat/version.go index 61e40a5..34ed6bb 100644 --- a/cmd/dashgoat/version.go +++ b/cmd/dashgoat/version.go @@ -1,3 +1,3 @@ package main -const Version string = "v1.5.0" +const Version string = "v1.5.1"