Skip to content

Commit

Permalink
fix: install scripts and improved asset compression
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Dec 24, 2023
1 parent fba6c8d commit b8c2e81
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ before:
hooks:
- go mod tidy
- go generate ./...
- sh -c "ls && cd ./webui && npm i && npm run build"
- sh -c "ls && cd ./webui && npm i && npm run build && gzip ./dist/*"

builds:
- main: .
Expand Down
20 changes: 19 additions & 1 deletion backrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,25 @@ func main() {
mux := http.NewServeMux()

if box, err := rice.FindBox("webui/dist"); err == nil {
mux.Handle("/", http.FileServer(box.HTTPBox()))
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)
}
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ module github.com/garethgeorge/backrest
go 1.21

require (
connectrpc.com/connect v1.14.0
github.com/GeertJohan/go.rice v1.0.3
github.com/NYTimes/gziphandler v1.1.1
github.com/gitploy-io/cronexpr v0.2.2
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1
github.com/hashicorp/go-multierror v1.1.1
github.com/mattn/go-colorable v0.1.13
github.com/natefinch/atomic v1.0.1
go.etcd.io/bbolt v1.3.8
go.uber.org/zap v1.26.0
golang.org/x/crypto v0.17.0
golang.org/x/net v0.19.0
google.golang.org/genproto/googleapis/api v0.0.0-20231212172506-995d672761c0
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.32.0
)

require (
connectrpc.com/connect v1.14.0 // indirect
github.com/daaku/go.zipexe v1.0.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.0.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ connectrpc.com/connect v1.14.0/go.mod h1:uoAq5bmhhn43TwhaKdGKN/bZcGtzPW1v+ngDTn5
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/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c=
github.com/daaku/go.zipexe v1.0.2 h1:Zg55YLYTr7M9wjKn8SY/WcpuuEi+kR2u4E8RhvpyXmk=
github.com/daaku/go.zipexe v1.0.2/go.mod h1:5xWogtqlYnfBXkSB1o9xysukNP9GTvaNkqzUZbt3Bw8=
Expand Down
40 changes: 19 additions & 21 deletions install.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#! /bin/bash

if [ "$EUID" -ne "0" ]; then
echo "Please run as root e.g. sudo ./install.sh"
exit
fi

install_unix() {
echo "Installing backrest to /usr/local/bin"
mkdir -p /usr/local/bin
cp $(ls -1 backrest | head -n 1) /usr/local/bin
sudo mkdir -p /usr/local/bin
sudo cp $(ls -1 backrest | head -n 1) /usr/local/bin
}

create_systemd_service() {
Expand All @@ -19,7 +15,7 @@ create_systemd_service() {

echo "Creating systemd service at /etc/systemd/system/backrest.service"

cat > /etc/systemd/system/backrest.service <<- EOM
sudo cat > /etc/systemd/system/backrest.service <<- EOM
[Unit]
Description=Backrest Service
After=network.target
Expand All @@ -33,30 +29,32 @@ ExecStart=/usr/local/bin/backrest
[Install]
WantedBy=multi-user.target
EOM

echo "Reloading systemd daemon"
systemctl daemon-reload
sudo systemctl daemon-reload
}

create_launchd_plist() {
echo "Creating launchd plist at /Library/LaunchAgents/com.backrest.plist"

cat > /Library/LaunchAgents/com.backrest.plist <<- EOM
sudo tee /Library/LaunchAgents/com.backrest.plist > /dev/null <<- EOM
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.backrest</string>
<key>Program</key>
<string>/usr/local/bin/backrest</string>
<key>RunAtLoad</key>
<true/>
<dict>
<key>Label</key>
<string>com.backrest</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/backrest</string>
</array>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/tmp/backrest.log</string>
<key>StandardErrorPath</key>
<string>/tmp/backrest.log</string>
</dict>
</dict>
</plist>
EOM
}
Expand All @@ -78,9 +76,9 @@ elif [ "$OS" = "Linux" ]; then
install_unix
create_systemd_service
echo "Enabling systemd service backrest.service"
systemctl enable backrest
systemctl start backrest
sudo systemctl enable backrest
sudo systemctl start backrest
else
echo "Unknown OS: $OS. This script only supports Darwin and Linux."
exit 1
fi
fi
17 changes: 6 additions & 11 deletions uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#! /bin/bash

if [ "$EUID" -ne "0" ]; then
echo "Please run as root e.g. sudo ./install.sh"
exit
fi

uninstall_unix() {
echo "Uninstalling backrest from /usr/local/bin/backrest"
rm -f /usr/local/bin/backrest
sudo rm -f /usr/local/bin/backrest
}

remove_systemd_service() {
Expand All @@ -17,19 +12,19 @@ remove_systemd_service() {
fi

echo "Removing systemd service at /etc/systemd/system/backrest.service"
systemctl stop backrest
systemctl disable backrest
rm -f /etc/systemd/system/backrest.service
sudo systemctl stop backrest
sudo systemctl disable backrest
sudo rm -f /etc/systemd/system/backrest.service

echo "Reloading systemd daemon"
systemctl daemon-reload
sudo systemctl daemon-reload
}

remove_launchd_plist() {
echo "Removing launchd plist at /Library/LaunchAgents/com.backrest.plist"

launchctl unload /Library/LaunchAgents/com.backrest.plist || true
rm /Library/LaunchAgents/com.backrest.plist
sudo rm /Library/LaunchAgents/com.backrest.plist
}

OS=$(uname -s)
Expand Down
2 changes: 1 addition & 1 deletion webui/.proxyrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/api": {
"/v1.Backrest": {
"target": "http://localhost:9898",
"secure": false
}
Expand Down

0 comments on commit b8c2e81

Please sign in to comment.