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.
This commit is contained in:
tgiachi 2026-11-12 15:56:34 +01:00
commit 249b2cb7bc
6 changed files with 56 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,4 +1,6 @@
/bin/ /bin/
# The built web app, embedded in the binary by a build with -tags webui: an artifact, never committed.
/internal/webui/dist/
*.test *.test
*.out *.out
sonda.toml sonda.toml

View file

@ -38,6 +38,7 @@ import (
"codeberg.org/squidd/sonda/internal/streaming" "codeberg.org/squidd/sonda/internal/streaming"
"codeberg.org/squidd/sonda/internal/taste" "codeberg.org/squidd/sonda/internal/taste"
"codeberg.org/squidd/sonda/internal/usersettings" "codeberg.org/squidd/sonda/internal/usersettings"
"codeberg.org/squidd/sonda/internal/webui"
"codeberg.org/squidd/sonda/migrations" "codeberg.org/squidd/sonda/migrations"
) )
@ -284,6 +285,7 @@ func run() error {
About: descriptions, About: descriptions,
Settings: userSettings, Settings: userSettings,
Streams: streaming.NewService(libraryRepo), Streams: streaming.NewService(libraryRepo),
Web: webui.FS(),
}) })
// No WriteTimeout: audio streaming (later) needs long responses. // No WriteTimeout: audio streaming (later) needs long responses.

View file

@ -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
}

View file

@ -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 }

View file

@ -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")
}
}

View file

@ -12,6 +12,12 @@ export default defineConfig({
resolve: { resolve: {
alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) }, 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: { server: {
proxy: { '/api': { target, changeOrigin: false } }, proxy: { '/api': { target, changeOrigin: false } },
}, },