From b3f3b25c3ad3debdcb2ddb1e98c6f0f21a54d3a7 Mon Sep 17 00:00:00 2001 From: yyyyaaa Date: Thu, 26 Nov 2026 17:10:44 +0700 Subject: [PATCH] feat(server): GET /version answers the server package and its commit The server answers GET /version with { name, version, commit } and Cache-Control: no-store. It is mounted next to the health check, before API routing, so it answers on every host without a credential or the database. name and version come from the server's package.json; commit is null when it is not set. The commit comes from SERVER_COMMIT: @pgpmjs/env reads it into server.commit, a new optional field of ServerOptions in @pgpmjs/types. The Dockerfile takes it as a build argument, declared last so a new commit rebuilds no other layer, and the image workflow passes github.sha. The pgpm skill's environment reference lists the variable. --- .../references/environment-configuration.md | 1 + .github/workflows/docker-constructive.yaml | 2 + Dockerfile | 6 ++ graphql/server/package.json | 1 + .../src/middleware/__tests__/version.test.ts | 63 +++++++++++++++++++ graphql/server/src/middleware/version.ts | 17 +++++ graphql/server/src/server.ts | 2 + pgpm/env/src/env.ts | 2 + pgpm/types/src/pgpm.ts | 2 + pnpm-lock.yaml | 3 + 10 files changed, 99 insertions(+) create mode 100644 graphql/server/src/middleware/__tests__/version.test.ts create mode 100644 graphql/server/src/middleware/version.ts diff --git a/.agents/skills/pgpm/references/environment-configuration.md b/.agents/skills/pgpm/references/environment-configuration.md index 4f34232863..e1a8fdc877 100644 --- a/.agents/skills/pgpm/references/environment-configuration.md +++ b/.agents/skills/pgpm/references/environment-configuration.md @@ -122,6 +122,7 @@ const deployOptions = getDeploymentEnvOptions(); | `SERVER_HOST` | Server host | | `SERVER_TRUST_PROXY` | Trust proxy headers | | `SERVER_ORIGIN` | Server origin URL | +| `SERVER_COMMIT` | Source commit of the running build, answered by `GET /version` (the image sets it from the build argument) | | `SERVER_STRICT_AUTH` | Strict authentication mode | ### Storage diff --git a/.github/workflows/docker-constructive.yaml b/.github/workflows/docker-constructive.yaml index a0cc531f43..99e039312d 100644 --- a/.github/workflows/docker-constructive.yaml +++ b/.github/workflows/docker-constructive.yaml @@ -92,6 +92,8 @@ jobs: file: ./Dockerfile platforms: ${{ matrix.platform }} labels: ${{ steps.meta.outputs.labels }} + build-args: | + SERVER_COMMIT=${{ github.sha }} outputs: type=image,name=${{ env.REPO }}/${{ env.IMAGE_NAME }},push-by-digest=true,push=true cache-from: type=gha cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile index 3f92ae38db..bf671475e2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -54,5 +54,11 @@ RUN set -eux; \ printf '#!/usr/bin/env bash\nnode /app/pgpm/pgpm/dist/index.js "$@"\n' > /usr/local/bin/pgpm; \ chmod +x /usr/local/bin/cnc /usr/local/bin/constructive /usr/local/bin/pgpm +# The commit the image is built from, answered by the server's GET /version. +# Declared last so that a new commit rebuilds no other layer; without the +# argument it is empty and the route answers null. +ARG SERVER_COMMIT= +ENV SERVER_COMMIT=${SERVER_COMMIT} + ENTRYPOINT ["/usr/local/bin/constructive"] CMD ["--help"] diff --git a/graphql/server/package.json b/graphql/server/package.json index 32035525c5..6a6d833fc4 100644 --- a/graphql/server/package.json +++ b/graphql/server/package.json @@ -62,6 +62,7 @@ "cors": "^2.8.6", "deepmerge": "^4.3.1", "express": "^5.2.1", + "find-and-require-package-json": "^0.9.1", "gql-ast": "workspace:^", "grafast": "1.1.2", "grafserv": "1.0.1", diff --git a/graphql/server/src/middleware/__tests__/version.test.ts b/graphql/server/src/middleware/__tests__/version.test.ts new file mode 100644 index 0000000000..26c27ac60e --- /dev/null +++ b/graphql/server/src/middleware/__tests__/version.test.ts @@ -0,0 +1,63 @@ +import { svcCache } from '@pgpmjs/server-utils'; +import type { Express } from 'express'; +import { readFileSync } from 'fs'; +import { graphileCache } from 'graphile-cache'; +import { join } from 'path'; +import supertest from 'supertest'; + +import { Server } from '../../server'; + +const pkg = JSON.parse(readFileSync(join(__dirname, '../../../package.json'), 'utf8')); + +const servers: Server[] = []; + +// Port 1 refuses every connection, so a request that reaches the database or +// the routing of a host fails instead of answering. +const appWith = (commit: string | undefined): Express => { + const previous = process.env.SERVER_COMMIT; + if (commit === undefined) delete process.env.SERVER_COMMIT; + else process.env.SERVER_COMMIT = commit; + try { + const server = new Server({ pg: { host: '127.0.0.1', port: 1, database: 'version_route_test' } }); + servers.push(server); + return (server as unknown as { app: Express }).app; + } finally { + if (previous === undefined) delete process.env.SERVER_COMMIT; + else process.env.SERVER_COMMIT = previous; + } +}; + +describe('GET /version', () => { + afterEach(async () => { + for (const server of servers.splice(0)) await server.close(); + svcCache.clear(); + graphileCache.clear(); + }); + + it('answers the package and the commit of SERVER_COMMIT, uncached', async () => { + const res = await supertest(appWith('0123456789abcdef0123456789abcdef01234567')) + .get('/version').set('Host', 'api.localhost'); + + expect(res.status).toBe(200); + expect(res.headers['cache-control']).toBe('no-store'); + expect(res.body).toEqual({ name: pkg.name, version: pkg.version, commit: '0123456789abcdef0123456789abcdef01234567' }); + }); + + it.each([ + ['unset', undefined], + ['empty, as an image built without the argument has it', ''] + ])('answers a null commit when SERVER_COMMIT is %s', async (_what, commit) => { + const res = await supertest(appWith(commit)).get('/version').set('Host', 'api.localhost'); + + expect(res.status).toBe(200); + expect(res.body).toEqual({ name: pkg.name, version: pkg.version, commit: null }); + }); + + it('answers on a host the server does not route, with a bad credential, without the database', async () => { + const res = await supertest(appWith('abc1234')) + .get('/version').set('Host', 'nowhere.invalid').set('Authorization', 'Bearer not-a-token'); + + expect(res.status).toBe(200); + expect(res.body).toEqual({ name: pkg.name, version: pkg.version, commit: 'abc1234' }); + }); +}); diff --git a/graphql/server/src/middleware/version.ts b/graphql/server/src/middleware/version.ts new file mode 100644 index 0000000000..b06ec264a5 --- /dev/null +++ b/graphql/server/src/middleware/version.ts @@ -0,0 +1,17 @@ +import type { RequestHandler } from 'express'; +import { findAndRequirePackageJson } from 'find-and-require-package-json'; + +const pkg = findAndRequirePackageJson(__dirname); + +/** + * GET /version: the server package and the commit this process runs, so a run + * report can say what it tested. The server mounts it before routing, so it + * answers on every host without a credential or the database. + */ +export const version = (commit: string | undefined): RequestHandler => { + const body = { name: pkg.name, version: pkg.version, commit: commit || null }; + return (_req, res) => { + res.setHeader('Cache-Control', 'no-store'); + res.json(body); + }; +}; diff --git a/graphql/server/src/server.ts b/graphql/server/src/server.ts index 64ef41524d..542c5c4d3b 100644 --- a/graphql/server/src/server.ts +++ b/graphql/server/src/server.ts @@ -43,6 +43,7 @@ import { localObservabilityOnly } from './middleware/observability/guard'; import { createRequestLogger } from './middleware/observability/request-logger'; import { createRequestProtectionMiddleware } from './middleware/request-protection'; import { createStandingMiddleware } from './middleware/standing'; +import { version } from './middleware/version'; import { isWebSocketUpgrade, routeWebSocketUpgrade } from './middleware/websocket'; import { createPlatformRefusalRecorder, installRefusalRecorder } from './refusals/recorder'; @@ -137,6 +138,7 @@ class Server { } healthz(app); + app.get('/version', version(effectiveOpts.server?.commit)); if (observabilityEnabled) { app.get('/debug/memory', localObservabilityOnly, debugMemory); app.get('/debug/db', localObservabilityOnly, createDebugDatabaseMiddleware(effectiveOpts)); diff --git a/pgpm/env/src/env.ts b/pgpm/env/src/env.ts index 668080a439..a54e9957bd 100644 --- a/pgpm/env/src/env.ts +++ b/pgpm/env/src/env.ts @@ -45,6 +45,7 @@ export const getEnvVars = (env: NodeJS.ProcessEnv = process.env): PgpmOptions => SERVER_TRUST_PROXY, SERVER_ORIGIN, SERVER_STRICT_AUTH, + SERVER_COMMIT, PGHOST, PGPORT, @@ -127,6 +128,7 @@ export const getEnvVars = (env: NodeJS.ProcessEnv = process.env): PgpmOptions => ...(SERVER_TRUST_PROXY && { trustProxy: parseEnvBoolean(SERVER_TRUST_PROXY) }), ...(SERVER_ORIGIN && { origin: SERVER_ORIGIN }), ...(SERVER_STRICT_AUTH && { strictAuth: parseEnvBoolean(SERVER_STRICT_AUTH) }), + ...(SERVER_COMMIT && { commit: SERVER_COMMIT }), }, pg: { ...(PGHOST && { host: PGHOST }), diff --git a/pgpm/types/src/pgpm.ts b/pgpm/types/src/pgpm.ts index 3a2fcc0a23..5aa2dabffd 100644 --- a/pgpm/types/src/pgpm.ts +++ b/pgpm/types/src/pgpm.ts @@ -120,6 +120,8 @@ export interface ServerOptions { origin?: string; /** Whether to enforce strict authentication */ strictAuth?: boolean; + /** The source commit the running build was made from; unset when unknown */ + commit?: string; } /** diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 119eb60698..4550fb6ba3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2219,6 +2219,9 @@ importers: express: specifier: ^5.2.1 version: 5.2.1 + find-and-require-package-json: + specifier: ^0.9.1 + version: 0.9.1 gql-ast: specifier: workspace:^ version: link:../gql-ast/dist