feat(M01-A): 建立后端API基础骨架
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import type { FastifyInstance } from 'fastify';
|
||||
import type { AppConfig } from '../config.js';
|
||||
|
||||
interface HealthPayload {
|
||||
ok: true;
|
||||
service: 'qipai-api';
|
||||
version: string;
|
||||
traceId: string;
|
||||
}
|
||||
|
||||
interface ReadyPayload extends HealthPayload {
|
||||
checks: {
|
||||
mysqlConfigured: boolean;
|
||||
mqttConfigured: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export async function registerHealthRoutes(app: FastifyInstance, config: AppConfig): Promise<void> {
|
||||
const health = async (request: { traceId: string }): Promise<HealthPayload> => ({
|
||||
ok: true,
|
||||
service: 'qipai-api',
|
||||
version: config.version,
|
||||
traceId: request.traceId
|
||||
});
|
||||
|
||||
const ready = async (request: { traceId: string }): Promise<ReadyPayload> => ({
|
||||
...(await health(request)),
|
||||
checks: {
|
||||
mysqlConfigured: config.mysql.passwordConfigured,
|
||||
mqttConfigured: config.mqtt.usernameConfigured && config.mqtt.passwordConfigured
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/app-api/health', health);
|
||||
app.get('/admin-api/health', health);
|
||||
app.get('/app-api/ready', ready);
|
||||
app.get('/admin-api/ready', ready);
|
||||
app.get('/app-api/version', health);
|
||||
app.get('/admin-api/version', health);
|
||||
}
|
||||
Reference in New Issue
Block a user