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
@@ -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/platform-express": "^11.1.18",
|
||||
"@nestjs/schedule": "^6.1.1",
|
||||
"@nestjs/serve-static": "^5.0.5",
|
||||
"@nestjs/swagger": "^11.2.6",
|
||||
"@nestjs/typeorm": "^11.0.1",
|
||||
"@playwright/test": "^1.59.1",
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { ConfigModule, ConfigService } from "@nestjs/config";
|
||||
import { ScheduleModule } from "@nestjs/schedule";
|
||||
import { ServeStaticModule } from "@nestjs/serve-static";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { BrowserModule } from "./browser/browser.module";
|
||||
import { clientStaticOptions } from "./common/client-static";
|
||||
import { AppConfig, validateAppConfig } from "./config/app.config";
|
||||
import { CredentialEntity } from "./credential/credential.entity";
|
||||
import { CredentialModule } from "./credential/credential.module";
|
||||
@@ -36,6 +38,11 @@ import { SnippetModule } from "./snippet/snippet.module";
|
||||
validate: validateAppConfig,
|
||||
}),
|
||||
ScheduleModule.forRoot(),
|
||||
ServeStaticModule.forRootAsync({
|
||||
inject: [ConfigService],
|
||||
useFactory: (config: ConfigService<AppConfig, true>) =>
|
||||
clientStaticOptions(config.get("CLIENT_DIR")),
|
||||
}),
|
||||
TypeOrmModule.forRootAsync({
|
||||
inject: [ConfigService],
|
||||
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()
|
||||
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)
|
||||
// Off by default: traces with screenshots and snapshots easily reach ~10 MB per run
|
||||
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