From 249b2cb7bc1428f8bc48ef196b5c4728747d154a Mon Sep 17 00:00:00 2001 From: tgiachi Date: Thu, 12 Nov 2026 15:56:34 +0100 Subject: [PATCH] feat: embed the built web app in sondad behind the webui build tag internal/webui carries web/'s build (written to internal/webui/dist by Vite, git-ignored) when sondad is built with -tags webui. Without the tag the app is absent and a plain go build and go test still work with no Node. main wires it into the router. --- .gitignore | 2 ++ cmd/sondad/main.go | 2 ++ internal/webui/embed_dist.go | 23 +++++++++++++++++++++++ internal/webui/embed_none.go | 10 ++++++++++ internal/webui/webui_test.go | 13 +++++++++++++ web/vite.config.ts | 6 ++++++ 6 files changed, 56 insertions(+) create mode 100644 internal/webui/embed_dist.go create mode 100644 internal/webui/embed_none.go create mode 100644 internal/webui/webui_test.go diff --git a/.gitignore b/.gitignore index 4869cfd..08be5c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ /bin/ +# The built web app, embedded in the binary by a build with -tags webui: an artifact, never committed. +/internal/webui/dist/ *.test *.out sonda.toml diff --git a/cmd/sondad/main.go b/cmd/sondad/main.go index a705fce..4a2ad4e 100644 --- a/cmd/sondad/main.go +++ b/cmd/sondad/main.go @@ -38,6 +38,7 @@ import ( "codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/internal/taste" "codeberg.org/squidd/sonda/internal/usersettings" + "codeberg.org/squidd/sonda/internal/webui" "codeberg.org/squidd/sonda/migrations" ) @@ -284,6 +285,7 @@ func run() error { About: descriptions, Settings: userSettings, Streams: streaming.NewService(libraryRepo), + Web: webui.FS(), }) // No WriteTimeout: audio streaming (later) needs long responses. diff --git a/internal/webui/embed_dist.go b/internal/webui/embed_dist.go new file mode 100644 index 0000000..4d4794f --- /dev/null +++ b/internal/webui/embed_dist.go @@ -0,0 +1,23 @@ +//go:build webui + +// Package webui carries the built web app inside the binary. +package webui + +import ( + "embed" + "io/fs" +) + +// dist is what `npm run build` writes in web/: build the app first, then build with -tags webui. +// +//go:embed all:dist +var dist embed.FS + +// FS is the built web app, with index.html at its root. +func FS() fs.FS { + sub, err := fs.Sub(dist, "dist") + if err != nil { + return nil + } + return sub +} diff --git a/internal/webui/embed_none.go b/internal/webui/embed_none.go new file mode 100644 index 0000000..3a3a895 --- /dev/null +++ b/internal/webui/embed_none.go @@ -0,0 +1,10 @@ +//go:build !webui + +// Package webui carries the built web app inside the binary. +package webui + +import "io/fs" + +// FS is nil in a build without the app (no webui tag), so that a plain go build and go test +// work without building the web app first. The server then shows a page saying how to build it. +func FS() fs.FS { return nil } diff --git a/internal/webui/webui_test.go b/internal/webui/webui_test.go new file mode 100644 index 0000000..a22bce6 --- /dev/null +++ b/internal/webui/webui_test.go @@ -0,0 +1,13 @@ +//go:build !webui + +package webui + +import "testing" + +// A build without the webui tag has no app, and says so with a nil FS, not with an empty one that +// looks like an app. +func TestWithoutTheTagThereIsNoApp(t *testing.T) { + if FS() != nil { + t.Error("FS is not nil in a build without the webui tag") + } +} diff --git a/web/vite.config.ts b/web/vite.config.ts index d7fa32d..a9f79d6 100644 --- a/web/vite.config.ts +++ b/web/vite.config.ts @@ -12,6 +12,12 @@ export default defineConfig({ resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) }, }, + // The build goes where the Go server embeds it (build sondad with -tags webui). It is outside the web + // folder, so Vite has to be told that it may empty it. + build: { + outDir: '../internal/webui/dist', + emptyOutDir: true, + }, server: { proxy: { '/api': { target, changeOrigin: false } }, },