feat(M06-A): 建立MQTT连接与Broker基础
This commit is contained in:
@@ -12,10 +12,24 @@ interface ReadyPayload extends HealthPayload {
|
||||
checks: {
|
||||
mysqlConfigured: boolean;
|
||||
mqttConfigured: boolean;
|
||||
mqttConnected: boolean;
|
||||
mqttSubscriptionsReady: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export async function registerHealthRoutes(app: FastifyInstance, config: AppConfig): Promise<void> {
|
||||
export interface MqttHealthProvider {
|
||||
health(): {
|
||||
configured: boolean;
|
||||
connected: boolean;
|
||||
subscriptionsReady: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
export async function registerHealthRoutes(
|
||||
app: FastifyInstance,
|
||||
config: AppConfig,
|
||||
mqtt?: MqttHealthProvider
|
||||
): Promise<void> {
|
||||
const health = async (request: { traceId: string }): Promise<HealthPayload> => ({
|
||||
ok: true,
|
||||
service: 'qipai-api',
|
||||
@@ -23,13 +37,19 @@ export async function registerHealthRoutes(app: FastifyInstance, config: AppConf
|
||||
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
|
||||
}
|
||||
});
|
||||
const ready = async (request: { traceId: string }): Promise<ReadyPayload> => {
|
||||
const mqttHealth = mqtt?.health();
|
||||
return {
|
||||
...(await health(request)),
|
||||
checks: {
|
||||
mysqlConfigured: config.mysql.passwordConfigured,
|
||||
mqttConfigured: mqttHealth?.configured
|
||||
?? (config.mqtt.usernameConfigured && config.mqtt.passwordConfigured),
|
||||
mqttConnected: mqttHealth?.connected ?? false,
|
||||
mqttSubscriptionsReady: mqttHealth?.subscriptionsReady ?? false
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
app.get('/app-api/health', health);
|
||||
app.get('/admin-api/health', health);
|
||||
|
||||
Reference in New Issue
Block a user