Files
task-stream/apps/api/src/app/health.controller.ts
Josep Piguillem 341241a52f feat(api): REST API with OpenAPI for other apps, behind Pireon OAuth
A REST adapter next to GraphQL (presenters/http) that calls the same
application services, so validation, permissions and domain errors are the
same: projects (members, board, stats, activity), issues (search, workflow,
assignee, release, links, comments, history), comments, releases and users.
Projects and issues can be addressed by id or key (WEB, WEB-12).

- Swagger UI at /api/docs, document at /api/docs-json and apps/api/openapi.json
  (rewritten by `nx serve api`, like schema.gql) for client generation.
- Every route but health/auth/webhook needs a Pireon token: the document
  declares Pireon OAuth2 (authorization code + PKCE, password) through the
  /api/auth/token proxy, and a bearer scheme for apps with their own client.
- configureApp() shares main.ts setup with the e2e, which now also covers REST.
- SWAGGER_ENABLED / OPENAPI_FILE config; docs 03/05 and library READMEs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 15:26:47 +02:00

54 lines
1.5 KiB
TypeScript

import {
Controller,
Get,
Inject,
ServiceUnavailableException,
} from '@nestjs/common';
import { InjectConnection } from '@nestjs/mongoose';
import {
ApiOkResponse,
ApiOperation,
ApiServiceUnavailableResponse,
ApiTags,
} from '@nestjs/swagger';
import { Public } from '@task-stream/api/identity/presenters';
import { REDIS_CLIENT } from '@task-stream/api/shared/infrastructure';
import Redis from 'ioredis';
import { Connection } from 'mongoose';
import { DataSource } from 'typeorm';
@ApiTags('Health')
@Controller('health')
export class HealthController {
constructor(
private readonly postgres: DataSource,
@InjectConnection() private readonly mongo: Connection,
@Inject(REDIS_CLIENT) private readonly redis: Redis,
) {}
@Public()
@Get()
@ApiOperation({ summary: 'API and databases status' })
@ApiOkResponse({
schema: {
example: { status: 'ok', postgres: 'up', mongo: 'up', redis: 'up' },
},
})
@ApiServiceUnavailableResponse({ description: 'A database is down' })
async check() {
const checks = await Promise.allSettled([
this.postgres.query('SELECT 1'),
this.mongo.db?.admin().ping(),
this.redis.ping(),
]);
const [postgres, mongo, redis] = checks.map((c) =>
c.status === 'fulfilled' ? 'up' : 'down',
);
const result = { status: 'ok', postgres, mongo, redis };
if (checks.some((c) => c.status === 'rejected')) {
throw new ServiceUnavailableException({ ...result, status: 'degraded' });
}
return result;
}
}