mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-18 22:21:23 +02:00
Implement custom NestJS API Gateway for mana-search, mana-stt, and mana-tts: - API Key management with CRUD operations and key regeneration - Redis-based sliding window rate limiting - Credit-based billing with tier support (free, pro, enterprise) - Usage tracking with daily aggregates - Proxy services to backend microservices - Prometheus metrics endpoint - JWT auth for management API, API key auth for public API Database schema uses separate `api_gateway` schema in shared manacore DB.
33 lines
710 B
TypeScript
33 lines
710 B
TypeScript
import { drizzle } from 'drizzle-orm/postgres-js';
|
|
import postgres from 'postgres';
|
|
import * as schema from './schema';
|
|
|
|
let connection: ReturnType<typeof postgres> | null = null;
|
|
let db: ReturnType<typeof drizzle> | null = null;
|
|
|
|
export function getConnection(databaseUrl: string) {
|
|
if (!connection) {
|
|
connection = postgres(databaseUrl, {
|
|
max: 10,
|
|
idle_timeout: 20,
|
|
connect_timeout: 10,
|
|
});
|
|
}
|
|
return connection;
|
|
}
|
|
|
|
export function getDb(databaseUrl: string) {
|
|
if (!db) {
|
|
const conn = getConnection(databaseUrl);
|
|
db = drizzle(conn, { schema });
|
|
}
|
|
return db;
|
|
}
|
|
|
|
export async function closeConnection() {
|
|
if (connection) {
|
|
await connection.end();
|
|
connection = null;
|
|
db = null;
|
|
}
|
|
}
|