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:
1 parent
1832ea7437
commit
5e6c011ecb
12 files changed
+104
-11
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -179,7 +179,7 @@ The `server` section is split into a number of sub-sections:
|
||||
|
||||
| Key | Type | Default | Notes |
|
||||
|---|---|---|---|
|
||||
| `server.frontend.dir` | string | | Directory of the frontend's build output, holding `index.html`; empty: no frontend is served (there is no embedded build yet) |
|
||||
| `server.frontend.dir` | string | | Directory of the frontend's build output, holding `index.html`; empty: the build embedded in the binary (see `npm run sync` in `web/`), or no frontend if it has none |
|
||||
| `server.frontend.immutable_prefix` | string | | Where Vite emits content-hashed files, cached for a year; empty: `/assets/` |
|
||||
| `server.frontend.index_cache_control` | string | | `Cache-Control` of HTML and other files without a hash; empty: `no-cache` |
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ const JSCKServerFrontend string = "frontend"
|
||||
|
||||
// JSCServerFrontend configures how the server serves the frontend. Dir is
|
||||
// the directory of the frontend's build output (holding index.html); empty
|
||||
// means no frontend is served, as there is no embedded build yet.
|
||||
// means the build embedded in the binary, or no frontend if it has none.
|
||||
// ImmutablePrefix is where Vite emits content-hashed files, cached for a
|
||||
// year; empty means "/assets/". IndexCacheControl is the Cache-Control of
|
||||
// HTML and other files without a hash; empty means "no-cache".
|
||||
|
||||
Vendored
Whitespace-only changes.
@@ -0,0 +1,24 @@
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
// dist holds the frontend's build output, copied from web/dist by `npm run
|
||||
// sync` in web/. Only dist/.gitkeep is committed (Go can't embed a missing
|
||||
// directory), so a binary built without syncing first has no frontend.
|
||||
//
|
||||
//go:embed all:dist
|
||||
var dist embed.FS
|
||||
|
||||
// Embedded returns the frontend's build output embedded in the binary, or
|
||||
// nil if none was synced before it was built (there is no index.html).
|
||||
func Embedded() fs.FS {
|
||||
files, err := fs.Sub(dist, "dist")
|
||||
if err != nil || CheckFS(files) != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return files
|
||||
}
|
||||
Reference in new issue
Block a user