chore: 服务骨架(Hono+TS+vitest+eslint,health 路由与探活注册点)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
import { Hono } from 'hono';
|
||||
import { healthRoute } from './health';
|
||||
|
||||
export function buildApp(): Hono {
|
||||
const app = new Hono();
|
||||
app.route('/health', healthRoute());
|
||||
return app;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Hono } from 'hono';
|
||||
import { config } from '../shared/config';
|
||||
|
||||
export type HealthCheck = () => Promise<'ok' | 'fail'>;
|
||||
const checks = new Map<string, HealthCheck>();
|
||||
|
||||
/** 后续任务注册 db / nats 探活 */
|
||||
export function registerHealthCheck(name: string, fn: HealthCheck): void {
|
||||
checks.set(name, fn);
|
||||
}
|
||||
|
||||
export function healthRoute(): Hono {
|
||||
const r = new Hono();
|
||||
r.get('/', async (c) => {
|
||||
const entries = await Promise.all(
|
||||
[...checks.entries()].map(async ([name, fn]) => [name, await fn()] as const),
|
||||
);
|
||||
const result = Object.fromEntries(entries);
|
||||
const ok = entries.every(([, v]) => v === 'ok');
|
||||
return c.json({ ok, service: config.serviceName, checks: result }, ok ? 200 : 503);
|
||||
});
|
||||
return r;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import { serve } from '@hono/node-server';
|
||||
import { buildApp } from './app';
|
||||
import { config } from '../shared/config';
|
||||
|
||||
serve({ fetch: buildApp().fetch, port: config.port }, (info) => {
|
||||
console.log(`${config.serviceName} listening on :${info.port}`);
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
export const config = {
|
||||
port: Number(process.env.PORT ?? 8800),
|
||||
natsUrl: process.env.NATS_URL ?? 'nats://172.16.86.151:4222',
|
||||
keycloak: {
|
||||
url: process.env.KEYCLOAK_URL ?? 'https://keycloak.akhr-ai.com',
|
||||
realm: process.env.KEYCLOAK_REALM ?? 'corp',
|
||||
clientId: process.env.KEYCLOAK_CLIENT_ID ?? 'api-gateway',
|
||||
clientSecret: process.env.KEYCLOAK_CLIENT_SECRET, // 只从环境读,无缺省;data-service 恒无
|
||||
},
|
||||
agentServiceUrl: process.env.AGENT_SERVICE_URL ?? 'http://127.0.0.1:8901',
|
||||
taskServiceUrl: process.env.TASK_SERVICE_URL ?? 'http://127.0.0.1:8801',
|
||||
dataServiceUrl: process.env.DATA_SERVICE_URL ?? 'http://127.0.0.1:8802',
|
||||
serviceName: 'bpos-service-gateway',
|
||||
} as const;
|
||||
Reference in New Issue
Block a user