Skip to content

Commit 9f6ed83

Browse files
committed
fix(cli): embed static files prevent 404 when running on other computers
1 parent 75d60ec commit 9f6ed83

File tree

5 files changed

+15
-9
lines changed

5 files changed

+15
-9
lines changed

cli/cmd/embed.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cmd
2+
3+
import "embed"
4+
5+
//go:embed static/*
6+
var staticFiles embed.FS

cli/cmd/http.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import (
77
"net/http"
88
"os"
99
"os/signal"
10-
"path/filepath"
11-
"runtime"
1210
"syscall"
11+
"io/fs"
1312
)
1413

1514
type OAuthResponse struct {
@@ -25,15 +24,16 @@ type OAuthResponseError struct {
2524
}
2625

2726
func httpServer(newAuthChan chan string, statusChan chan GuppyStatus, serverDoneChan chan bool) {
28-
// Get the directory of the current file
29-
_, filename, _, _ := runtime.Caller(0)
30-
dir := filepath.Dir(filename)
31-
32-
// Construct the path relative to the current file
33-
staticDir := filepath.Join(dir, "../static")
27+
// Create a sub-filesystem that only sees contents inside the static directory
28+
subFS, err := fs.Sub(staticFiles, "static")
29+
if err != nil {
30+
slog.Error("Failed to create sub filesystem", "err", err)
31+
return
32+
}
33+
fsys := http.FileServer(http.FS(subFS))
3434

3535
mux := http.NewServeMux()
36-
mux.Handle("GET /", http.FileServer(http.Dir(staticDir)))
36+
mux.Handle("GET /", fsys)
3737
mux.HandleFunc("POST /auth", authHttp(newAuthChan))
3838
mux.HandleFunc("GET /status", statusHttp(statusChan, serverDoneChan))
3939

0 commit comments

Comments
 (0)