mirror of
https://github.com/Memo-2023/mana-monorepo.git
synced 2026-05-14 19:41:09 +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.
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
import { Module } from '@nestjs/common';
|
|
import { MulterModule } from '@nestjs/platform-express';
|
|
import { memoryStorage } from 'multer';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import Redis from 'ioredis';
|
|
import { ProxyController } from './proxy.controller';
|
|
import { SearchProxyService, SttProxyService, TtsProxyService } from './services';
|
|
import { ApiKeysModule } from '../api-keys/api-keys.module';
|
|
import { UsageModule } from '../usage/usage.module';
|
|
import { CreditsModule } from '../credits/credits.module';
|
|
import { ApiKeyGuard } from '../guards/api-key.guard';
|
|
import { RateLimitGuard, REDIS_CLIENT } from '../guards/rate-limit.guard';
|
|
import { CreditsGuard } from '../guards/credits.guard';
|
|
import { UsageTrackingInterceptor } from '../common/interceptors/usage-tracking.interceptor';
|
|
|
|
@Module({
|
|
imports: [
|
|
MulterModule.register({
|
|
storage: memoryStorage(),
|
|
limits: {
|
|
fileSize: 100 * 1024 * 1024, // 100MB max file size
|
|
},
|
|
}),
|
|
ApiKeysModule,
|
|
UsageModule,
|
|
CreditsModule,
|
|
],
|
|
controllers: [ProxyController],
|
|
providers: [
|
|
SearchProxyService,
|
|
SttProxyService,
|
|
TtsProxyService,
|
|
ApiKeyGuard,
|
|
RateLimitGuard,
|
|
CreditsGuard,
|
|
UsageTrackingInterceptor,
|
|
{
|
|
provide: REDIS_CLIENT,
|
|
useFactory: (configService: ConfigService) => {
|
|
const host = configService.get<string>('redis.host') || 'localhost';
|
|
const port = configService.get<number>('redis.port') || 6379;
|
|
const password = configService.get<string>('redis.password');
|
|
|
|
return new Redis({
|
|
host,
|
|
port,
|
|
password: password || undefined,
|
|
keyPrefix: configService.get<string>('redis.keyPrefix') || 'api-gateway:',
|
|
});
|
|
},
|
|
inject: [ConfigService],
|
|
},
|
|
],
|
|
exports: [REDIS_CLIENT],
|
|
})
|
|
export class ProxyModule {}
|