Skip to content

Commit

Permalink
fix: use 'embed' to package WebUI sources instead of go.rice
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Feb 16, 2024
1 parent 11bd9e0 commit e3ba5cf
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 63 deletions.
2 changes: 0 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ builds:
goarch:
- amd64
- arm64
hooks:
post: rice append --exec {{ .Path }}

archives:
- format: tar.gz
Expand Down
27 changes: 2 additions & 25 deletions backrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/garethgeorge/backrest/internal/orchestrator"
"github.com/garethgeorge/backrest/internal/resticinstaller"
"github.com/garethgeorge/backrest/internal/rotatinglog"
"github.com/garethgeorge/backrest/webui"
"github.com/mattn/go-colorable"
"go.etcd.io/bbolt"
"go.uber.org/zap"
Expand Down Expand Up @@ -92,34 +93,10 @@ func main() {
apiAuthenticationHandler := api.NewAuthenticationHandler(authenticator)

mux := http.NewServeMux()

if box, err := WebUIBox(); err == nil {
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path += "index.html"
}
f, err := box.Open(r.URL.Path + ".gz")
if err == nil {
defer f.Close()
w.Header().Set("Content-Encoding", "gzip")
http.ServeContent(w, r, r.URL.Path, box.Time(), f)
return
}
f, err = box.Open(r.URL.Path)
if err == nil {
defer f.Close()
http.ServeContent(w, r, r.URL.Path, box.Time(), f)
return
}
http.Error(w, "Not found", http.StatusNotFound)
}))
} else {
zap.S().Warnf("Error loading static assets, not serving UI: %v", err)
}

mux.Handle(v1connect.NewAuthenticationHandler(apiAuthenticationHandler))
backrestHandlerPath, backrestHandler := v1connect.NewBackrestHandler(apiBackrestHandler)
mux.Handle(backrestHandlerPath, auth.RequireAuthentication(backrestHandler, authenticator))
mux.Handle("/", webui.Handler())

// Serve the HTTP gateway
server := &http.Server{
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.21

require (
connectrpc.com/connect v1.14.0
github.com/GeertJohan/go.rice v1.0.3
github.com/alessio/shellescape v1.4.2
github.com/gitploy-io/cronexpr v0.2.2
github.com/golang-jwt/jwt/v5 v5.2.0
Expand All @@ -22,7 +21,6 @@ require (
)

require (
github.com/daaku/go.zipexe v1.0.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
connectrpc.com/connect v1.14.0 h1:PDS+J7uoz5Oui2VEOMcfz6Qft7opQM9hPiKvtGC01pA=
connectrpc.com/connect v1.14.0/go.mod h1:uoAq5bmhhn43TwhaKdGKN/bZcGtzPW1v+ngDTn5u+8s=
github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0=
github.com/GeertJohan/go.rice v1.0.3 h1:k5viR+xGtIhF61125vCE1cmJ5957RQGXG6dmbaWZSmI=
github.com/GeertJohan/go.rice v1.0.3/go.mod h1:XVdrU4pW00M4ikZed5q56tPf1v2KwnIKeIdc9CBYNt4=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/alessio/shellescape v1.4.2 h1:MHPfaU+ddJ0/bYWpgIeUnQUqKrlJ1S7BfEYPM4uEoM0=
github.com/alessio/shellescape v1.4.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/daaku/go.zipexe v1.0.2 h1:Zg55YLYTr7M9wjKn8SY/WcpuuEi+kR2u4E8RhvpyXmk=
github.com/daaku/go.zipexe v1.0.2/go.mod h1:5xWogtqlYnfBXkSB1o9xysukNP9GTvaNkqzUZbt3Bw8=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gitploy-io/cronexpr v0.2.2 h1:Au+wK6FqmOLAF7AkW6q4gnrNXTe3rEW97XFZ4chy0xs=
Expand All @@ -25,21 +19,17 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A=
github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM=
github.com/nkovacs/streamquote v1.0.0/go.mod h1:BN+NaZ2CmdKqUuTUXUEm9j95B2TRbpOWpxbJYzzgUsc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA=
go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
Expand Down
62 changes: 62 additions & 0 deletions webui/webui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package webui

import (
"bytes"
"fmt"
"io"
"io/fs"
"net/http"
"strings"
)

func Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/") {
r.URL.Path += "index.html"
}

f, err := content.Open(contentPrefix + r.URL.Path + ".gz")
if err == nil {
defer f.Close()
w.Header().Set("Content-Encoding", "gzip")
serveFile(f, w, r, r.URL.Path)
return
}

f, err = content.Open(contentPrefix + r.URL.Path)
if err == nil {
defer f.Close()
serveFile(f, w, r, r.URL.Path)
return
}

http.Error(w, "Not found", http.StatusNotFound)
})
}

func serveFile(f fs.File, w http.ResponseWriter, r *http.Request, path string) {
data, err := io.ReadAll(f)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

stat, err := f.Stat()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

http.ServeContent(w, r, path, stat.ModTime(), bytes.NewReader(data))
}

func init() {
files, err := fs.ReadDir(content, contentPrefix)
if err != nil {
panic("expected embedded files for WebUI")
}

if len(files) == 0 {
panic(fmt.Sprintf("expected >0 embedded files for WebUI, got %d", len(files)))
}
}
14 changes: 14 additions & 0 deletions webui/webuinix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build linux || darwin
// +build linux darwin

//go:generate sh -c "rm -rf ./dist && UI_OS=unix npm run build && gzip ./dist/*"

package webui

import (
"embed"
)

//go:embed dist
var content embed.FS
var contentPrefix = "dist"
12 changes: 12 additions & 0 deletions webui/webuiwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build windows
// +build windows

//go:generate sh -c "rm -rf ./dist-windows && UI_OS=unix npm run build-windows && gzip ./dist-windows/*"

package webui

import "embed"

//go:embed dist-windows/*
var content embed.FS
var contentPrefix = "dist-windows"
12 changes: 0 additions & 12 deletions webuinix.go

This file was deleted.

12 changes: 0 additions & 12 deletions webuiwin.go

This file was deleted.

0 comments on commit e3ba5cf

Please sign in to comment.