Embedded the frontend in the binary, and built it in the build script

`npm run sync` in web/ copies the build output (web/dist, from npm run build)
to internal/frontend/dist, which the binary embeds. Go can't embed from
outside the package's directory (web/ is also outside the Go module), hence
the copy. Only dist/.gitkeep is committed, so a fresh clone still compiles,
and a binary built without syncing first serves no frontend.

job-search serve serves server.frontend.dir if it is set, or else the
embedded build, or warns that there is no frontend. scripts/build.sh builds
and syncs the frontend before building, running npm ci first for a release
or when web/node_modules is missing.
This commit is contained in:
bwilliams committed 2026-10-16 16:03:15 +01:00
1 parent 1832ea7437
commit 5e6c011ecb
12 files changed
+104 -11

No files matched your search

+15 -4
View File
@@ -31,7 +31,9 @@ func NewCommand(opts *cli.Options) *cobra.Command {
Use: "serve",
Short: "Serve the REST API, WebSocket and frontend, and fetch from sources",
Long: `Serve the REST API under /api, the WebSocket endpoint under /ws, and the
frontend's files from server.frontend.dir under / if it is set.
frontend under /: the files in server.frontend.dir if it is set, or else the
build embedded in the binary (copied there by npm run sync in web/ before
building), if there is one.
It also runs a worker for every enabled source, as fetcher service does,
saving what they fetch to database.file if it is set, and pushing an
@@ -196,9 +198,10 @@ set, the API serves the stored opportunities under /api/opportunities.`,
}
// serverOptions returns the server's options for conf: the access log, if
// server.access_log is set, and the frontend's files, if server.frontend.dir
// is (checked first). closeAccessLog closes the access log; call it once the
// server has stopped.
// server.access_log is set, and the frontend's files: those in
// server.frontend.dir if it is set (checked first), or else the build
// embedded in the binary, if there is one. closeAccessLog closes the access
// log; call it once the server has stopped.
func serverOptions(
conf *config.JSCServer,
) ([]server.Option, func() error, error) {
@@ -225,7 +228,15 @@ func serverOptions(
)
}
options = append(options, server.WithFrontend(files))
return options, closeAccessLog, nil
}
files := frontend.Embedded()
if files == nil {
slog.Warn("no frontend embedded and server.frontend.dir not set")
return options, closeAccessLog, nil
}
options = append(options, server.WithFrontend(files))
return options, closeAccessLog, nil
}