Skip to content

Commit

Permalink
Embed web files 1.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
iobear committed Feb 4, 2024
1 parent f6118fe commit a3d60e5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/workflows/e2e-single-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/...
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
3 changes: 1 addition & 2 deletions build/package/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
18 changes: 18 additions & 0 deletions cmd/dashgoat/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package main

import (
"io/fs"
"log"
"net/http"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -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)
}
15 changes: 12 additions & 3 deletions cmd/dashgoat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
package main

import (
"embed"
"flag"
"fmt"
"net/http"
"os"
"strings"

"github.com/labstack/echo-contrib/echoprometheus"
Expand All @@ -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
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion cmd/dashgoat/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const Version string = "v1.5.0"
const Version string = "v1.5.1"

0 comments on commit a3d60e5

Please sign in to comment.