feat(docker): serve the whole app from a single container
- serve the built client from the server via serve-static (CLIENT_DIR), falling back to index.html outside /api, /mcp, /docs and /health - replace server and client Dockerfiles and nginx with one root Dockerfile and a single liqa compose service on port 13000 - use Playwright's chromium-headless-shell with only its required libs and DejaVu/Liberation fonts instead of Debian chromium and Noto - clear the npm cache in the runtime image - bump version to 1.19.0
This commit is contained in:
1 parent
8935783371
commit
a4bfa3117f
18 files changed
+343
-104
No files matched your search
@@ -7,3 +7,4 @@ node_modules
|
|||||||
**/*.log
|
**/*.log
|
||||||
data
|
data
|
||||||
keys
|
keys
|
||||||
|
**/*.tsbuildinfo
|
||||||
+6
-1
@@ -7,7 +7,8 @@ KEYS_DIR=keys
|
|||||||
# SQLite database path
|
# SQLite database path
|
||||||
DB_PATH=data/sessions.db
|
DB_PATH=data/sessions.db
|
||||||
|
|
||||||
# Playwright executable (set in Docker; leave empty to use system default)
|
# Chromium binary to use instead of Playwright's own (not needed in Docker,
|
||||||
|
# which ships Playwright's chromium-headless-shell)
|
||||||
# PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium
|
# PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium
|
||||||
|
|
||||||
# Session lifecycle
|
# Session lifecycle
|
||||||
@@ -19,3 +20,7 @@ SESSION_DELETE_CLOSED_DAYS=7
|
|||||||
# Playwright trace recording for scenario runs: on | retain-on-failure | off
|
# Playwright trace recording for scenario runs: on | retain-on-failure | off
|
||||||
# (traces often reach ~10 MB per run; can be overridden per run in the run modal)
|
# (traces often reach ~10 MB per run; can be overridden per run in the run modal)
|
||||||
PLAYWRIGHT_TRACE_MODE=off
|
PLAYWRIGHT_TRACE_MODE=off
|
||||||
|
|
||||||
|
# Directory with the built client to serve at / (set to "client" in the Docker image;
|
||||||
|
# leave empty to not serve the UI, e.g. when using the Vite dev server)
|
||||||
|
# CLIENT_DIR=
|
||||||
@@ -2,6 +2,15 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## 1.19.0 - 2026-09-30
|
||||||
|
|
||||||
|
Changes since 1.18.0:
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- **Breaking (deployment):** the whole application now runs from a single image and container. The server serves the built client itself (new `CLIENT_DIR` setting, set by the image) next to the API, MCP and Swagger, so the UI moved from http://localhost:13001 to **http://localhost:13000**. The root `Dockerfile` replaces `server/Dockerfile`, `client/Dockerfile` and `client/nginx.conf`, and `compose.yaml` has a single `liqa` service. When upgrading, run `docker compose up -d --build --remove-orphans` to remove the old `server` and `client` containers.
|
||||||
|
- The image uses Playwright's headless-only Chromium build (`chromium-headless-shell`, always matching the installed Playwright version) with only the libraries it needs, instead of Debian's desktop `chromium` package. Fonts are now DejaVu and Liberation (metric-compatible with Arial/Times) instead of Noto. Together with clearing the npm cache, this shrinks the image from 1.76 GB to 1.07 GB.
|
||||||
|
|
||||||
## 1.18.0 - 2026-09-30
|
## 1.18.0 - 2026-09-30
|
||||||
|
|
||||||
Changes since 1.17.0:
|
Changes since 1.17.0:
|
||||||
|
|||||||
@@ -15,18 +15,14 @@ It is an npm workspace with two packages:
|
|||||||
- [docs/scenario.md](docs/scenario.md) - scenario execution, script `context` API, versioning, export/import, Playwright traces
|
- [docs/scenario.md](docs/scenario.md) - scenario execution, script `context` API, versioning, export/import, Playwright traces
|
||||||
- [docs/mcp.md](docs/mcp.md) - MCP tool reference
|
- [docs/mcp.md](docs/mcp.md) - MCP tool reference
|
||||||
- [CHANGELOG.md](CHANGELOG.md) - release notes per version
|
- [CHANGELOG.md](CHANGELOG.md) - release notes per version
|
||||||
|
- [Dockerfile](Dockerfile) - single image: builds client and server, runtime serves both
|
||||||
- [.env.example](.env.example) - all configuration variables (validated in `server/src/config/app.config.ts`)
|
- [.env.example](.env.example) - all configuration variables (validated in `server/src/config/app.config.ts`)
|
||||||
|
|
||||||
## Default deployment (docker compose)
|
## Default deployment (docker compose)
|
||||||
|
|
||||||
`compose.yaml` at the repo root runs two containers:
|
`compose.yaml` at the repo root runs a single `liqa` container, built from the root `Dockerfile`, at http://localhost:13000. The server serves everything: the UI at `/` (the built client, via `CLIENT_DIR`), the REST API at `/api/v1`, MCP at `/mcp`, Swagger at `/docs`. It reads `.env` and mounts `./data` (SQLite DB `data/liqa.db` + stored files) and `./keys`.
|
||||||
|
|
||||||
| Service | URL | Notes |
|
Rebuild and restart: `docker compose up -d --build`. Check with `docker compose ps`, `curl localhost:13000/health` and `docker compose logs liqa`.
|
||||||
|---|---|---|
|
|
||||||
| `server` | http://localhost:13000 | Built from `server/Dockerfile`; reads `.env`; mounts `./data` (SQLite DB `data/liqa.db` + stored files) and `./keys` |
|
|
||||||
| `client` | http://localhost:13001 | Built from `client/Dockerfile`; nginx serves the SPA and proxies `/api/` to `server:3000` |
|
|
||||||
|
|
||||||
Rebuild and restart: `docker compose up -d --build`. Check with `docker compose ps`, `curl localhost:13000/health` and `docker compose logs server`.
|
|
||||||
|
|
||||||
The live database is `data/liqa.db`. Because the schema is synced on startup, back it up before deploying entity changes.
|
The live database is `data/liqa.db`. Because the schema is synced on startup, back it up before deploying entity changes.
|
||||||
|
|
||||||
|
|||||||
+87
@@ -0,0 +1,87 @@
|
|||||||
|
# Single image serving the whole application: the NestJS server (API, MCP,
|
||||||
|
# scheduler) also serves the built client, so one container is enough.
|
||||||
|
|
||||||
|
# ── Client build ──────────────────────────────────────────────────────────────
|
||||||
|
FROM node:22-slim AS client-build
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Root lockfile + workspace manifest so npm ci can resolve the full graph
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY client/package.json ./client/
|
||||||
|
RUN npm ci -w client
|
||||||
|
|
||||||
|
COPY client ./client
|
||||||
|
RUN npm run -w client build
|
||||||
|
|
||||||
|
# ── Server build ──────────────────────────────────────────────────────────────
|
||||||
|
FROM node:22-slim AS server-build
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY server/package.json ./server/
|
||||||
|
RUN npm ci -w server
|
||||||
|
|
||||||
|
COPY server/nest-cli.json ./server/
|
||||||
|
COPY server/tsconfig*.json ./server/
|
||||||
|
COPY server/src ./server/src
|
||||||
|
RUN npm run -w server build
|
||||||
|
|
||||||
|
# ── Runtime ───────────────────────────────────────────────────────────────────
|
||||||
|
FROM node:22-slim AS runtime
|
||||||
|
|
||||||
|
# Shared libraries chrome-headless-shell links against (from `ldd`), plus
|
||||||
|
# fontconfig and basic fonts for text rendering. Playwright's `--with-deps`
|
||||||
|
# would also pull in xvfb (with Mesa/LLVM) and extra font packages, which
|
||||||
|
# headless-only use doesn't need.
|
||||||
|
RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||||
|
ca-certificates \
|
||||||
|
fontconfig \
|
||||||
|
fonts-dejavu-core \
|
||||||
|
fonts-liberation \
|
||||||
|
libasound2 \
|
||||||
|
libatk-bridge2.0-0 \
|
||||||
|
libatk1.0-0 \
|
||||||
|
libatspi2.0-0 \
|
||||||
|
libdbus-1-3 \
|
||||||
|
libexpat1 \
|
||||||
|
libgbm1 \
|
||||||
|
libglib2.0-0 \
|
||||||
|
libnspr4 \
|
||||||
|
libnss3 \
|
||||||
|
libx11-6 \
|
||||||
|
libxcb1 \
|
||||||
|
libxcomposite1 \
|
||||||
|
libxdamage1 \
|
||||||
|
libxext6 \
|
||||||
|
libxfixes3 \
|
||||||
|
libxkbcommon0 \
|
||||||
|
libxrandr2 \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY server/package.json ./server/
|
||||||
|
|
||||||
|
# Playwright's headless-only Chromium build, matching the installed Playwright
|
||||||
|
# version. All browser launches are headless, so the full desktop Chromium
|
||||||
|
# (GTK, Mesa/LLVM, icon themes) isn't needed.
|
||||||
|
ENV PLAYWRIGHT_BROWSERS_PATH=/ms-playwright
|
||||||
|
RUN npm ci --omit=dev -w server \
|
||||||
|
&& npx playwright install --only-shell chromium \
|
||||||
|
&& npm cache clean --force
|
||||||
|
|
||||||
|
COPY --from=server-build /app/server/dist ./dist
|
||||||
|
COPY --from=client-build /app/client/dist ./client
|
||||||
|
|
||||||
|
# Serve the built client at / (see server/src/common/client-static.ts)
|
||||||
|
ENV CLIENT_DIR=client
|
||||||
|
|
||||||
|
# Runtime directories (keys and SQLite DB mounted via volumes)
|
||||||
|
RUN mkdir -p data
|
||||||
|
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
CMD ["node", "dist/main"]
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
# ── Build stage ───────────────────────────────────────────────────────────────
|
|
||||||
FROM node:22-slim AS builder
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
# Copy root lockfile + workspace manifests so npm ci can resolve the full graph
|
|
||||||
COPY package*.json ./
|
|
||||||
COPY client/package.json ./client/
|
|
||||||
RUN npm ci -w client
|
|
||||||
|
|
||||||
COPY client ./client
|
|
||||||
RUN npm run -w client build
|
|
||||||
|
|
||||||
# ── Runtime stage ─────────────────────────────────────────────────────────────
|
|
||||||
FROM nginx:alpine AS runtime
|
|
||||||
|
|
||||||
COPY --from=builder /app/client/dist /usr/share/nginx/html
|
|
||||||
COPY client/nginx.conf /etc/nginx/conf.d/default.conf
|
|
||||||
|
|
||||||
EXPOSE 80
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
server {
|
|
||||||
listen 80;
|
|
||||||
root /usr/share/nginx/html;
|
|
||||||
index index.html;
|
|
||||||
|
|
||||||
location /api/ {
|
|
||||||
proxy_pass http://server:3000;
|
|
||||||
proxy_http_version 1.1;
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
||||||
}
|
|
||||||
|
|
||||||
location / {
|
|
||||||
try_files $uri $uri/ /index.html;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-10
@@ -1,9 +1,9 @@
|
|||||||
services:
|
services:
|
||||||
server:
|
liqa:
|
||||||
build:
|
build:
|
||||||
context: .
|
context: .
|
||||||
dockerfile: server/Dockerfile
|
|
||||||
ports:
|
ports:
|
||||||
|
# UI, REST API (/api/v1), MCP (/mcp) and Swagger (/docs)
|
||||||
- "13000:3000"
|
- "13000:3000"
|
||||||
env_file:
|
env_file:
|
||||||
- .env
|
- .env
|
||||||
@@ -13,11 +13,3 @@ services:
|
|||||||
# mount key files into the container
|
# mount key files into the container
|
||||||
- ./keys:/app/keys
|
- ./keys:/app/keys
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|
||||||
client:
|
|
||||||
build:
|
|
||||||
context: .
|
|
||||||
dockerfile: client/Dockerfile
|
|
||||||
ports:
|
|
||||||
- "13001:80"
|
|
||||||
restart: unless-stopped
|
|
||||||
@@ -41,9 +41,9 @@ MCP client (AI agent / VS Code Copilot)
|
|||||||
|
|
||||||
## Client (`client/`)
|
## Client (`client/`)
|
||||||
|
|
||||||
A React 19 + TypeScript SPA served separately by Vite during development. In Docker Compose the same build artefacts are served statically alongside the API.
|
A React 19 + TypeScript SPA served by the Vite dev server during development. In the Docker image the server serves the built client itself (`ServeStaticModule`, see `server/src/common/client-static.ts`), from the same origin as the API.
|
||||||
|
|
||||||
The client calls the server REST endpoints directly — there is no separate BFF layer. Routing uses HashRouter so the Vite proxy and server-side routes are never ambiguous.
|
The client calls the server REST endpoints directly — there is no separate BFF layer. Routing uses `BrowserRouter`; the server answers any GET outside `/api`, `/mcp`, `/docs` and `/health` with the client's `index.html`, so client-side routes survive a page refresh.
|
||||||
|
|
||||||
The sidebar footer contains the theme switcher and the app version (injected from the workspace root `package.json` at build time).
|
The sidebar footer contains the theme switcher and the app version (injected from the workspace root `package.json` at build time).
|
||||||
|
|
||||||
|
|||||||
+9
-2
@@ -30,7 +30,8 @@ Copy `.env.example` to `.env` and fill in the required values before running.
|
|||||||
| `DB_PATH` | `data/sessions.db` | SQLite database file |
|
| `DB_PATH` | `data/sessions.db` | SQLite database file |
|
||||||
| `SESSION_IDLE_TIMEOUT_MINUTES` | `30` | Idle timeout before open sessions are closed |
|
| `SESSION_IDLE_TIMEOUT_MINUTES` | `30` | Idle timeout before open sessions are closed |
|
||||||
| `SESSION_DELETE_CLOSED_DAYS` | `7` | Retention period for closed sessions |
|
| `SESSION_DELETE_CLOSED_DAYS` | `7` | Retention period for closed sessions |
|
||||||
| `PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH` | _(Playwright default)_ | Path to Chromium binary. Set automatically in Docker (`/usr/bin/chromium`). |
|
| `CLIENT_DIR` | *(empty)* | Directory with the built client to serve at `/` (the Docker image sets it to `client`); empty means the server doesn't serve the UI |
|
||||||
|
| `PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH` | _(Playwright default)_ | Path to a Chromium binary to use instead of Playwright's own. Not set in Docker: the image ships Playwright's `chromium-headless-shell` (matching the Playwright version) under `PLAYWRIGHT_BROWSERS_PATH=/ms-playwright`. |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -163,6 +164,10 @@ Both workspaces target zero errors. Run lint before committing.
|
|||||||
|
|
||||||
## Building the container
|
## Building the container
|
||||||
|
|
||||||
|
The root `Dockerfile` builds a single image with the whole application: it builds the client and the server in separate stages, and the runtime image runs the server, which also serves the built client (`CLIENT_DIR`).
|
||||||
|
|
||||||
|
The runtime image uses Playwright's headless-only Chromium build (`chromium-headless-shell`) with just the shared libraries it links against, plus DejaVu and Liberation fonts (Liberation is metric-compatible with Arial/Times, so pages lay out as they would with those fonts). All browser launches are headless, so the full desktop Chromium isn't installed. If a new Playwright version needs another library, `ldd` on the `chrome-headless-shell` binary in the image lists what's missing.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker compose build
|
docker compose build
|
||||||
```
|
```
|
||||||
@@ -189,7 +194,9 @@ Build and start in one step:
|
|||||||
docker compose up -d --build
|
docker compose up -d --build
|
||||||
```
|
```
|
||||||
|
|
||||||
The server API is exposed at **http://localhost:13000**.
|
Everything is served by the single `liqa` container at **http://localhost:13000**: the UI at `/`, the REST API at `/api/v1`, MCP at `/mcp`, Swagger at `/docs` and health at `/health`. Unknown paths outside those routes get the client's `index.html`, so client-side routes survive a page refresh.
|
||||||
|
|
||||||
|
When switching from the old two-container setup (`server` + `client`), remove the old containers with `docker compose up -d --build --remove-orphans`.
|
||||||
|
|
||||||
SQLite data is persisted in `./data/` and key files are mounted from `./keys/` — both directories are volume-mounted into the container.
|
SQLite data is persisted in `./data/` and key files are mounted from `./keys/` — both directories are volume-mounted into the container.
|
||||||
|
|
||||||
|
|||||||
Generated
+30
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "liqa",
|
"name": "liqa",
|
||||||
"version": "1.18.0",
|
"version": "1.19.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "liqa",
|
"name": "liqa",
|
||||||
"version": "1.18.0",
|
"version": "1.19.0",
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
"server",
|
"server",
|
||||||
"client"
|
"client"
|
||||||
@@ -3387,6 +3387,33 @@
|
|||||||
"tslib": "^2.1.0"
|
"tslib": "^2.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@nestjs/serve-static": {
|
||||||
|
"version": "5.0.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/@nestjs/serve-static/-/serve-static-5.0.5.tgz",
|
||||||
|
"integrity": "sha512-AhYx3N9aMwR2cb0w5Nlb5nHNYiAcF74ea/D/xna+PxlXwjmwGN/PpC/5fuMtOwmPBMgOTxNPOnB8C9LDZBSgyw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"path-to-regexp": "8.4.2"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@fastify/static": "^8.0.4 || ^9.0.0",
|
||||||
|
"@nestjs/common": "^11.0.2",
|
||||||
|
"@nestjs/core": "^11.0.2",
|
||||||
|
"express": "^5.0.1",
|
||||||
|
"fastify": "^5.2.1"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"@fastify/static": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"express": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"fastify": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@nestjs/swagger": {
|
"node_modules/@nestjs/swagger": {
|
||||||
"version": "11.2.6",
|
"version": "11.2.6",
|
||||||
"resolved": "https://registry.npmjs.org/@nestjs/swagger/-/swagger-11.2.6.tgz",
|
"resolved": "https://registry.npmjs.org/@nestjs/swagger/-/swagger-11.2.6.tgz",
|
||||||
@@ -16676,6 +16703,7 @@
|
|||||||
"@nestjs/core": "^11.1.18",
|
"@nestjs/core": "^11.1.18",
|
||||||
"@nestjs/platform-express": "^11.1.18",
|
"@nestjs/platform-express": "^11.1.18",
|
||||||
"@nestjs/schedule": "^6.1.1",
|
"@nestjs/schedule": "^6.1.1",
|
||||||
|
"@nestjs/serve-static": "^5.0.5",
|
||||||
"@nestjs/swagger": "^11.2.6",
|
"@nestjs/swagger": "^11.2.6",
|
||||||
"@nestjs/typeorm": "^11.0.1",
|
"@nestjs/typeorm": "^11.0.1",
|
||||||
"@playwright/test": "^1.59.1",
|
"@playwright/test": "^1.59.1",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "liqa",
|
"name": "liqa",
|
||||||
"version": "1.18.0",
|
"version": "1.19.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"workspaces": [
|
"workspaces": [
|
||||||
"server",
|
"server",
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
# ── Build stage ──────────────────────────────────────────────────────────────
|
|
||||||
FROM node:22-slim AS builder
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY package*.json ./
|
|
||||||
COPY server/package.json ./server/
|
|
||||||
RUN npm ci -w server
|
|
||||||
|
|
||||||
COPY server/nest-cli.json ./server/
|
|
||||||
COPY server/tsconfig*.json ./server/
|
|
||||||
COPY server/src ./server/src
|
|
||||||
RUN npm run -w server build
|
|
||||||
|
|
||||||
# ── Runtime stage ─────────────────────────────────────────────────────────────
|
|
||||||
FROM node:22-slim AS runtime
|
|
||||||
|
|
||||||
# Playwright / Chromium system dependencies
|
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
||||||
chromium \
|
|
||||||
fonts-noto \
|
|
||||||
ca-certificates \
|
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
|
||||||
|
|
||||||
# Tell Playwright to use the system Chromium instead of downloading its own
|
|
||||||
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
|
|
||||||
ENV PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium
|
|
||||||
|
|
||||||
WORKDIR /app
|
|
||||||
|
|
||||||
COPY package*.json ./
|
|
||||||
COPY server/package.json ./server/
|
|
||||||
RUN npm ci --omit=dev -w server
|
|
||||||
|
|
||||||
COPY --from=builder /app/server/dist ./dist
|
|
||||||
|
|
||||||
# Runtime directories (keys and SQLite DB mounted via volumes)
|
|
||||||
RUN mkdir -p data
|
|
||||||
|
|
||||||
EXPOSE 3000
|
|
||||||
|
|
||||||
CMD ["node", "dist/main"]
|
|
||||||
@@ -25,6 +25,7 @@
|
|||||||
"@nestjs/core": "^11.1.18",
|
"@nestjs/core": "^11.1.18",
|
||||||
"@nestjs/platform-express": "^11.1.18",
|
"@nestjs/platform-express": "^11.1.18",
|
||||||
"@nestjs/schedule": "^6.1.1",
|
"@nestjs/schedule": "^6.1.1",
|
||||||
|
"@nestjs/serve-static": "^5.0.5",
|
||||||
"@nestjs/swagger": "^11.2.6",
|
"@nestjs/swagger": "^11.2.6",
|
||||||
"@nestjs/typeorm": "^11.0.1",
|
"@nestjs/typeorm": "^11.0.1",
|
||||||
"@playwright/test": "^1.59.1",
|
"@playwright/test": "^1.59.1",
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
import { Module } from "@nestjs/common";
|
import { Module } from "@nestjs/common";
|
||||||
import { ConfigModule, ConfigService } from "@nestjs/config";
|
import { ConfigModule, ConfigService } from "@nestjs/config";
|
||||||
import { ScheduleModule } from "@nestjs/schedule";
|
import { ScheduleModule } from "@nestjs/schedule";
|
||||||
|
import { ServeStaticModule } from "@nestjs/serve-static";
|
||||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||||
import { BrowserModule } from "./browser/browser.module";
|
import { BrowserModule } from "./browser/browser.module";
|
||||||
|
import { clientStaticOptions } from "./common/client-static";
|
||||||
import { AppConfig, validateAppConfig } from "./config/app.config";
|
import { AppConfig, validateAppConfig } from "./config/app.config";
|
||||||
import { CredentialEntity } from "./credential/credential.entity";
|
import { CredentialEntity } from "./credential/credential.entity";
|
||||||
import { CredentialModule } from "./credential/credential.module";
|
import { CredentialModule } from "./credential/credential.module";
|
||||||
@@ -36,6 +38,11 @@ import { SnippetModule } from "./snippet/snippet.module";
|
|||||||
validate: validateAppConfig,
|
validate: validateAppConfig,
|
||||||
}),
|
}),
|
||||||
ScheduleModule.forRoot(),
|
ScheduleModule.forRoot(),
|
||||||
|
ServeStaticModule.forRootAsync({
|
||||||
|
inject: [ConfigService],
|
||||||
|
useFactory: (config: ConfigService<AppConfig, true>) =>
|
||||||
|
clientStaticOptions(config.get("CLIENT_DIR")),
|
||||||
|
}),
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
inject: [ConfigService],
|
inject: [ConfigService],
|
||||||
useFactory: (config: ConfigService<AppConfig, true>) => ({
|
useFactory: (config: ConfigService<AppConfig, true>) => ({
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import type { ServeStaticModuleOptions } from "@nestjs/serve-static";
|
||||||
|
import type { ServerResponse } from "node:http";
|
||||||
|
import * as path from "node:path";
|
||||||
|
|
||||||
|
/** Server routes that must never fall back to the client's index.html. */
|
||||||
|
const SERVER_ROUTES = [
|
||||||
|
"/api/{*path}",
|
||||||
|
"/mcp{*path}",
|
||||||
|
"/docs{*path}",
|
||||||
|
"/health",
|
||||||
|
"/healthz",
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serves the built client (SPA) from `clientDir`: static files as they are,
|
||||||
|
* any other GET outside the server routes gets index.html so client-side
|
||||||
|
* routes survive a page refresh. Returns no options (nothing served) when
|
||||||
|
* `clientDir` is empty, e.g. in tests and local development.
|
||||||
|
*/
|
||||||
|
export function clientStaticOptions(
|
||||||
|
clientDir: string,
|
||||||
|
): ServeStaticModuleOptions[] {
|
||||||
|
if (!clientDir) return [];
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
rootPath: path.resolve(clientDir),
|
||||||
|
exclude: SERVER_ROUTES,
|
||||||
|
serveStaticOptions: {
|
||||||
|
setHeaders: (res: ServerResponse, filePath: string) => {
|
||||||
|
// Vite emits content-hashed files under assets/; index.html must
|
||||||
|
// always be revalidated so a new deploy is picked up immediately
|
||||||
|
const cacheControl = filePath.includes(`${path.sep}assets${path.sep}`)
|
||||||
|
? "public, max-age=31536000, immutable"
|
||||||
|
: "no-cache";
|
||||||
|
res.setHeader("Cache-Control", cacheControl);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -55,6 +55,10 @@ export class AppConfig {
|
|||||||
@IsString()
|
@IsString()
|
||||||
FILE_CLEANUP_CRON: string = "0 3 * * *";
|
FILE_CLEANUP_CRON: string = "0 3 * * *";
|
||||||
|
|
||||||
|
/** Directory with the built client to serve at `/`; empty to not serve it. */
|
||||||
|
@IsString()
|
||||||
|
CLIENT_DIR: string = "";
|
||||||
|
|
||||||
@IsEnum(TraceMode)
|
@IsEnum(TraceMode)
|
||||||
// Off by default: traces with screenshots and snapshots easily reach ~10 MB per run
|
// Off by default: traces with screenshots and snapshots easily reach ~10 MB per run
|
||||||
PLAYWRIGHT_TRACE_MODE: TraceMode = TraceMode.Off;
|
PLAYWRIGHT_TRACE_MODE: TraceMode = TraceMode.Off;
|
||||||
|
|||||||
@@ -0,0 +1,141 @@
|
|||||||
|
/**
|
||||||
|
* Tests serving the built client alongside the server routes: static files,
|
||||||
|
* the index.html fallback for client-side routes, and that server routes are
|
||||||
|
* never answered with index.html.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
Controller,
|
||||||
|
Get,
|
||||||
|
INestApplication,
|
||||||
|
Module,
|
||||||
|
Post,
|
||||||
|
RequestMethod,
|
||||||
|
} from "@nestjs/common";
|
||||||
|
import { ServeStaticModule } from "@nestjs/serve-static";
|
||||||
|
import { NestFactory } from "@nestjs/core";
|
||||||
|
import * as fs from "node:fs";
|
||||||
|
import * as os from "node:os";
|
||||||
|
import * as path from "node:path";
|
||||||
|
import request from "supertest";
|
||||||
|
import { clientStaticOptions } from "../src/common/client-static";
|
||||||
|
|
||||||
|
@Controller("things")
|
||||||
|
class ThingsController {
|
||||||
|
@Get()
|
||||||
|
list() {
|
||||||
|
return [{ id: 1 }];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Controller()
|
||||||
|
class RootRoutesController {
|
||||||
|
@Get("health")
|
||||||
|
health() {
|
||||||
|
return { status: "ok" };
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post("mcp")
|
||||||
|
mcp() {
|
||||||
|
return { jsonrpc: "2.0" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function buildApp(clientDir: string): Promise<INestApplication> {
|
||||||
|
// NestFactory (as in main.ts) rather than Test.createTestingModule: the
|
||||||
|
// serve-static module picks its HTTP loader while the module compiles,
|
||||||
|
// which needs the HTTP adapter to exist already.
|
||||||
|
@Module({
|
||||||
|
imports: [ServeStaticModule.forRoot(...clientStaticOptions(clientDir))],
|
||||||
|
controllers: [ThingsController, RootRoutesController],
|
||||||
|
})
|
||||||
|
class ClientStaticTestModule {}
|
||||||
|
|
||||||
|
const app = await NestFactory.create(ClientStaticTestModule, {
|
||||||
|
logger: false,
|
||||||
|
});
|
||||||
|
// Same prefix setup as main.ts
|
||||||
|
app.setGlobalPrefix("api/v1", {
|
||||||
|
exclude: [
|
||||||
|
{ path: "health", method: RequestMethod.ALL },
|
||||||
|
{ path: "mcp", method: RequestMethod.ALL },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
await app.init();
|
||||||
|
return app;
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("Client static serving", () => {
|
||||||
|
let clientDir: string;
|
||||||
|
let app: INestApplication;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
clientDir = fs.mkdtempSync(path.join(os.tmpdir(), "liqa-client-"));
|
||||||
|
fs.mkdirSync(path.join(clientDir, "assets"));
|
||||||
|
fs.writeFileSync(path.join(clientDir, "index.html"), "<html>liqa</html>");
|
||||||
|
fs.writeFileSync(
|
||||||
|
path.join(clientDir, "assets", "app-abc123.js"),
|
||||||
|
"console.log(1);",
|
||||||
|
);
|
||||||
|
app = await buildApp(clientDir);
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await app.close();
|
||||||
|
fs.rmSync(clientDir, { recursive: true, force: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("serves index.html at / without caching", async () => {
|
||||||
|
const res = await request(app.getHttpServer()).get("/").expect(200);
|
||||||
|
expect(res.text).toBe("<html>liqa</html>");
|
||||||
|
expect(res.headers["cache-control"]).toBe("no-cache");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("serves index.html for client-side routes", async () => {
|
||||||
|
const res = await request(app.getHttpServer())
|
||||||
|
.get("/scenarios/123/runs/456")
|
||||||
|
.expect(200);
|
||||||
|
expect(res.text).toBe("<html>liqa</html>");
|
||||||
|
expect(res.headers["cache-control"]).toBe("no-cache");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("serves hashed assets as immutable", async () => {
|
||||||
|
const res = await request(app.getHttpServer())
|
||||||
|
.get("/assets/app-abc123.js")
|
||||||
|
.expect(200);
|
||||||
|
expect(res.text).toBe("console.log(1);");
|
||||||
|
expect(res.headers["cache-control"]).toContain("immutable");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves API routes to the server", async () => {
|
||||||
|
const res = await request(app.getHttpServer())
|
||||||
|
.get("/api/v1/things")
|
||||||
|
.expect(200);
|
||||||
|
expect(res.body).toEqual([{ id: 1 }]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns the server's 404 for unknown API routes, not index.html", async () => {
|
||||||
|
const res = await request(app.getHttpServer())
|
||||||
|
.get("/api/v1/nope")
|
||||||
|
.expect(404);
|
||||||
|
expect(res.text).not.toContain("liqa</html>");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("leaves /health and /mcp to the server", async () => {
|
||||||
|
await request(app.getHttpServer())
|
||||||
|
.get("/health")
|
||||||
|
.expect(200, { status: "ok" });
|
||||||
|
await request(app.getHttpServer())
|
||||||
|
.post("/mcp")
|
||||||
|
.expect(201, { jsonrpc: "2.0" });
|
||||||
|
const res = await request(app.getHttpServer()).get("/mcp");
|
||||||
|
expect(res.text).not.toContain("liqa</html>");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("serves nothing when no client directory is configured", async () => {
|
||||||
|
const bare = await buildApp("");
|
||||||
|
await request(bare.getHttpServer()).get("/").expect(404);
|
||||||
|
await request(bare.getHttpServer()).get("/api/v1/things").expect(200);
|
||||||
|
await bare.close();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user