feat(api): complete phase 2.4 control plane
This commit is contained in:
@@ -5,6 +5,7 @@ import Fastify, {
|
||||
type FastifyServerOptions,
|
||||
type FastifyRequest,
|
||||
} from 'fastify';
|
||||
import { UpstreamError } from './infrastructure/transport/upstream-error.js';
|
||||
|
||||
export const API_DEFAULT_HOST = '127.0.0.1' as const;
|
||||
export const API_DEFAULT_PORT = 8790 as const;
|
||||
@@ -144,6 +145,7 @@ export function createListenOptions(environment: ListenEnvironment): ListenOptio
|
||||
throw new Error('API port must be an integer between 1 and 65535');
|
||||
}
|
||||
if (port === LEGACY_PORT) throw new Error('API port 8788 is reserved for the legacy service');
|
||||
if (port !== API_DEFAULT_PORT) throw new Error('API port is fixed at 8790');
|
||||
return { host, port };
|
||||
}
|
||||
|
||||
@@ -161,6 +163,12 @@ export function buildApp(options: BuildAppOptions = {}): FastifyInstance {
|
||||
const fastifyOptions: FastifyServerOptions = {
|
||||
genReqId: () => randomUUID(),
|
||||
logger,
|
||||
ajv: {
|
||||
customOptions: {
|
||||
removeAdditional: false,
|
||||
coerceTypes: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
const app = Fastify(fastifyOptions);
|
||||
const readiness = options.readiness ?? (() => ({ ready: true, checks: { bootstrap: 'ready' } }));
|
||||
@@ -216,6 +224,26 @@ export function buildApp(options: BuildAppOptions = {}): FastifyInstance {
|
||||
),
|
||||
);
|
||||
}
|
||||
if (error instanceof UpstreamError && error.code !== 'UPSTREAM_REQUEST_INVALID') {
|
||||
const status =
|
||||
error.code === 'UPSTREAM_INSECURE_AUTH'
|
||||
? 400
|
||||
: error.code === 'UPSTREAM_TIMEOUT'
|
||||
? 504
|
||||
: 502;
|
||||
const title =
|
||||
status === 400 ? 'Bad Request' : status === 504 ? 'Gateway Timeout' : 'Bad Gateway';
|
||||
const detail =
|
||||
status === 400
|
||||
? 'Authentication requires an HTTPS instance origin.'
|
||||
: status === 504
|
||||
? 'The upstream instance did not respond in time.'
|
||||
: 'The upstream instance could not complete the request.';
|
||||
return reply
|
||||
.code(status)
|
||||
.type('application/problem+json')
|
||||
.send(sendProblem(request, status, title, error.code, detail));
|
||||
}
|
||||
const status = clientStatus(error);
|
||||
if (status !== undefined) {
|
||||
const title = CLIENT_ERROR_TITLES[status] ?? 'Bad Request';
|
||||
|
||||
Reference in New Issue
Block a user