feat(api): add global infrastructure modules

This commit is contained in:
dahyeon committed 2026-04-11 03:17:00 +09:00
1 parent 05565f5a44
commit fb59e7721e
15 files changed
+2004

No files matched your search

@@ -0,0 +1,14 @@
import { resolveMongoDbName } from './connection.service';
describe('resolveMongoDbName', () => {
it('uses production database only for prod', () => {
expect(resolveMongoDbName('prod')).toBe('lolympus');
});
it('uses isolated databases for non-production environments', () => {
expect(resolveMongoDbName('dev')).toBe('lolympus-dev');
expect(resolveMongoDbName('test')).toBe('lolympus-test');
expect(resolveMongoDbName('')).toBe('lolympus-dev');
expect(resolveMongoDbName('local')).toBe('lolympus-dev');
});
});
@@ -0,0 +1,100 @@
import { Injectable, Scope, OnModuleInit } from '@nestjs/common';
import mongoose from 'mongoose';
import { SecretService } from '../secret/secret.service';
import { tournamentModelDefinitions, TournamentModelTypes } from './schema';
export function resolveMongoDbName(nodeEnv = process.env.NODE_ENV): string {
if (nodeEnv === 'prod') return 'lolympus';
if (nodeEnv === 'test') return 'lolympus-test';
return 'lolympus-dev';
}
@Injectable({ scope: Scope.DEFAULT })
export class ConnectionService implements OnModuleInit {
private connection: mongoose.Connection | null = null;
constructor(private readonly secretService: SecretService) {}
private tournamentModels: Map<string, mongoose.Model<any>> | null = null;
public async getConnection(): Promise<mongoose.Connection> {
if (this.connection !== null && this.connection.readyState === 1) {
return this.connection;
}
const connectionString = await this.secretService.get(
'MONGODB_CONNECTION_STRING',
);
const dbName = resolveMongoDbName();
const defaultConnection = await mongoose
.createConnection(connectionString, {
dbName,
minPoolSize: 10,
maxPoolSize: 20,
})
.asPromise();
this.connection = defaultConnection;
console.log(`Default Connection이 새로 생성됨. dbName=${dbName}`);
return this.connection;
}
public async getModel<K extends keyof TournamentModelTypes>(
modelKey: K,
): Promise<mongoose.Model<TournamentModelTypes[K]>> {
if (this.tournamentModels)
return this.tournamentModels.get(modelKey) as mongoose.Model<
TournamentModelTypes[K]
>;
const models = new Map<K, mongoose.Model<TournamentModelTypes[K]>>();
const connection = await this.getConnection();
tournamentModelDefinitions.forEach((modelDefinition) => {
// 이미 생성된 경우 컴파일 스킵.
if (
Object.prototype.hasOwnProperty.call(
connection.models,
modelDefinition.name,
)
) {
models.set(
modelDefinition.name as K,
connection.models[modelDefinition.name],
);
return;
} else {
models.set(
modelDefinition.name as K,
connection.model<TournamentModelTypes[K]>(
modelDefinition.name,
modelDefinition.schema,
modelDefinition.schemaName,
),
);
}
});
this.tournamentModels = models;
return models.get(modelKey) as mongoose.Model<TournamentModelTypes[K]>;
}
public async createSession(
options?: mongoose.ClientSessionOptions,
): Promise<mongoose.ClientSession | undefined> {
try {
const connection = await this.getConnection();
const session = await connection.startSession(options);
return session as unknown as mongoose.ClientSession;
} catch (error) {
console.log(error);
}
}
public async onModuleInit() {
await this.getConnection().catch(console.error);
}
public async onModuleDestroy() {
this.connection?.close();
}
}
@@ -0,0 +1,9 @@
import { DynamicModule, Module } from '@nestjs/common';
import { ConnectionService } from './connection.service';
@Module({
imports: [],
providers: [ConnectionService],
exports: [ConnectionService],
})
export class DatabaseModule {}
File diff suppressed because it is too large. Load diff
+5
View File
@@ -0,0 +1,5 @@
export interface ModelDefinition {
name: string;
schema: mongoose.Schema<any>;
schemaName?: string;
}
@@ -0,0 +1 @@
export const GARNET_CLIENT = Symbol('GARNET_CLIENT');
@@ -0,0 +1,57 @@
import { Logger, Module } from '@nestjs/common';
import { createClient } from 'redis';
import { SecretModule } from '../secret/secret.module';
import { SecretService } from '../secret/secret.service';
import { GARNET_CLIENT } from './garnet.constants';
import { GarnetService, type GarnetRedisClient } from './garnet.service';
function normalizeGarnetUrl(endpoint: string): string {
const trimmed = endpoint.trim();
if (!trimmed) {
throw new Error('GARNET_ENDPOINT cannot be empty');
}
if (/^redis(s)?:\/\//iu.test(trimmed)) {
return trimmed;
}
return `redis://${trimmed}`;
}
@Module({
imports: [SecretModule],
providers: [
{
provide: GARNET_CLIENT,
inject: [SecretService],
useFactory: async (
secretService: SecretService,
): Promise<GarnetRedisClient> => {
await secretService.ready();
const [endpoint, username, password] = await Promise.all([
secretService.get<string>('GARNET_ENDPOINT'),
secretService.get<string>('GARNET_USERID'),
secretService.get<string>('GARNET_PASSWORD'),
]);
const logger = new Logger('GarnetClient');
const client = createClient({
url: normalizeGarnetUrl(endpoint),
username,
password,
});
client.on('error', (error) => {
logger.error(`Garnet client error: ${error?.message ?? error}`);
});
await client.connect();
logger.log('Garnet connected');
return client;
},
},
GarnetService,
],
exports: [GarnetService, GARNET_CLIENT],
})
export class GarnetModule {}
@@ -0,0 +1,213 @@
import {
Inject,
Injectable,
Logger,
OnModuleDestroy,
} from '@nestjs/common';
import type { createClient } from 'redis';
import { GARNET_CLIENT } from './garnet.constants';
export type GarnetRedisClient = ReturnType<typeof createClient>;
export interface StreamEntry {
deliveriesCounter?: number;
id: string;
message: Record<string, string>;
millisElapsedFromDelivery?: number;
}
export interface StreamReadResult {
messages: StreamEntry[];
name: string;
}
interface RawStreamMessage {
deliveriesCounter?: number;
id: string;
message: unknown;
millisElapsedFromDelivery?: number;
}
interface RawStreamReadResult {
messages: RawStreamMessage[];
name: string;
}
function normalizeStreamFields(fields: unknown): Record<string, string> {
if (fields instanceof Map) {
return Object.fromEntries(
[...fields.entries()].map(([key, value]) => [String(key), String(value)]),
);
}
if (typeof fields === 'object' && fields !== null) {
return Object.fromEntries(
Object.entries(fields as Record<string, unknown>).map(([key, value]) => [
key,
String(value),
]),
);
}
return {};
}
@Injectable()
export class GarnetService implements OnModuleDestroy {
private readonly logger = new Logger(GarnetService.name);
constructor(
@Inject(GARNET_CLIENT) private readonly client: GarnetRedisClient,
) {}
async onModuleDestroy() {
try {
await this.client.quit();
} catch {
this.client.destroy();
}
}
public raw(): GarnetRedisClient {
return this.client;
}
public async getJson<T>(key: string): Promise<T | null> {
const raw = await this.client.get(key);
if (!raw) return null;
try {
return JSON.parse(raw) as T;
} catch (err) {
this.logger.warn(
`Failed to JSON.parse cached value for key="${key}": ${
(err as Error)?.message ?? err
}`,
);
return null;
}
}
public async setJson(
key: string,
value: unknown,
ttlSeconds?: number,
): Promise<void> {
const serialized = JSON.stringify(value);
if (ttlSeconds && ttlSeconds > 0) {
await this.client.set(key, serialized, { EX: ttlSeconds });
return;
}
await this.client.set(key, serialized);
}
public async getString(key: string): Promise<string | null> {
return this.client.get(key);
}
public async setString(
key: string,
value: string,
ttlSeconds?: number,
): Promise<void> {
if (ttlSeconds && ttlSeconds > 0) {
await this.client.set(key, value, { EX: ttlSeconds });
return;
}
await this.client.set(key, value);
}
public async setStringIfAbsent(
key: string,
value: string,
ttlSeconds?: number,
): Promise<boolean> {
const options: { EX?: number; NX: true } = { NX: true };
if (ttlSeconds && ttlSeconds > 0) options.EX = ttlSeconds;
const result = await this.client.set(key, value, options);
return result === 'OK';
}
public async delete(key: string): Promise<number> {
return this.client.del(key);
}
public async appendStream(
key: string,
fields: Record<string, string>,
options?: { maxLen?: number },
): Promise<string> {
const trimOptions =
options?.maxLen && options.maxLen > 0
? {
TRIM: {
strategy: 'MAXLEN' as const,
strategyModifier: '~' as const,
threshold: options.maxLen,
},
}
: undefined;
return this.client.xAdd(key, '*', fields, trimOptions);
}
public async createConsumerGroup(
key: string,
group: string,
startId = '$',
): Promise<void> {
await this.client.xGroupCreate(key, group, startId, { MKSTREAM: true });
}
public async readGroup(
key: string,
group: string,
consumer: string,
startId: string,
options?: { blockMs?: number; count?: number },
): Promise<StreamReadResult[] | null> {
const readOptions: { BLOCK?: number; COUNT?: number } = {};
if (options?.blockMs !== undefined) readOptions.BLOCK = options.blockMs;
if (options?.count !== undefined) readOptions.COUNT = options.count;
const reply = await this.client.xReadGroup(
group,
consumer,
{ key, id: startId },
readOptions,
);
if (!reply) return null;
return (reply as RawStreamReadResult[]).map((stream) => ({
name: String(stream.name),
messages: stream.messages.map((message) => ({
deliveriesCounter:
typeof message.deliveriesCounter === 'number'
? message.deliveriesCounter
: undefined,
id: String(message.id),
message: normalizeStreamFields(message.message),
millisElapsedFromDelivery:
typeof message.millisElapsedFromDelivery === 'number'
? message.millisElapsedFromDelivery
: undefined,
})),
}));
}
public async acknowledgeStream(
key: string,
group: string,
ids: string | string[],
): Promise<number> {
return this.client.xAck(key, group, ids);
}
public async streamLength(key: string): Promise<number> {
return this.client.xLen(key);
}
public async publish(channel: string, message: string): Promise<number> {
return this.client.publish(channel, message);
}
}
@@ -0,0 +1,13 @@
import { Module, Global } from '@nestjs/common';
import { SecretModule } from './secret/secret.module';
import { NatsModule } from './nats/nats.module';
import { DatabaseModule } from './database/database.module';
import { GarnetModule } from './garnet/garnet.module';
@Global()
@Module({
imports: [SecretModule, NatsModule, DatabaseModule, GarnetModule],
providers: [],
exports: [SecretModule, NatsModule, DatabaseModule, GarnetModule],
})
export class GlobalModule {}
@@ -0,0 +1 @@
export const NATS_CLIENT = Symbol('NATS_CLIENT');
@@ -0,0 +1,45 @@
import { Module } from '@nestjs/common';
import fs from 'fs/promises';
import { connect } from 'nats';
import type { NatsConnection } from 'nats';
import { SecretModule } from '../secret/secret.module';
import { SecretService } from '../secret/secret.service';
import { NATS_CLIENT } from './nats.constants';
import { NatsService } from './nats.service';
@Module({
imports: [SecretModule],
providers: [
{
provide: NATS_CLIENT,
inject: [SecretService],
useFactory: async (
secretService: SecretService,
): Promise<NatsConnection> => {
await secretService.ready();
const [endpoint, user, password, serviceName] = await Promise.all([
secretService.get<string>('NATS_ENDPOINT'),
secretService.get<string>('NATS_USER'),
secretService.get<string>('NATS_PASS'),
secretService.get<string>('SERVICE_NAME'),
]);
return connect({
name: serviceName,
servers: [endpoint],
maxReconnectAttempts: -1,
reconnectTimeWait: 500,
reconnect: true,
pingInterval: 20000,
timeout: 5000,
user,
pass: password,
});
},
},
NatsService,
],
exports: [NatsService, NATS_CLIENT],
})
export class NatsModule {}
@@ -0,0 +1,117 @@
import {
Inject,
Injectable,
Logger,
OnModuleDestroy,
} from '@nestjs/common';
import { StringCodec } from 'nats';
import type { Msg, NatsConnection, Subscription } from 'nats';
import { NATS_CLIENT } from './nats.constants';
export type NatsMessageHandler<T = unknown> = (
data: T,
msg: Msg,
) => Promise<void> | void;
@Injectable()
export class NatsService implements OnModuleDestroy {
private readonly logger = new Logger(NatsService.name);
private readonly stringCodec = StringCodec();
private readonly subscriptions = new Set<Subscription>();
constructor(
@Inject(NATS_CLIENT) private readonly natsClient: NatsConnection,
) {}
async onModuleDestroy() {
for (const sub of this.subscriptions) {
try {
sub.unsubscribe();
} catch {
// ignore
}
}
this.subscriptions.clear();
try {
const closed = this.natsClient.closed();
await this.natsClient.drain();
const err = await closed;
if (err) this.logger.error(`NATS closed with error: ${err}`);
} catch {
try {
await this.natsClient.close();
} catch {
// ignore
}
}
}
public async publish<T = unknown>(subject: string, data: T): Promise<void> {
this.natsClient.publish(
subject,
this.stringCodec.encode(JSON.stringify(data)),
);
await this.natsClient.flush();
}
public subscribe<T = unknown>(
subject: string,
handler: NatsMessageHandler<T>,
options?: { queue?: string },
): Subscription {
const sub = this.natsClient.subscribe(subject, {
queue: options?.queue,
});
this.subscriptions.add(sub);
void this.consumeSubscription(sub, handler);
return sub;
}
private async consumeSubscription<T>(
sub: Subscription,
handler: NatsMessageHandler<T>,
): Promise<void> {
try {
for await (const msg of sub) {
await this.dispatch(msg, handler);
}
} catch (err) {
this.logger.error(
`NATS subscription on subject="${sub.getSubject()}" terminated: ${
(err as Error)?.message ?? err
}`,
);
} finally {
this.subscriptions.delete(sub);
}
}
private async dispatch<T>(
msg: Msg,
handler: NatsMessageHandler<T>,
): Promise<void> {
let data: T;
try {
const decoded = this.stringCodec.decode(msg.data);
data = JSON.parse(decoded) as T;
} catch (err) {
this.logger.warn(
`Failed to decode NATS message on subject="${msg.subject}": ${
(err as Error)?.message ?? err
}`,
);
return;
}
try {
await handler(data, msg);
} catch (err) {
this.logger.error(
`Handler error on subject="${msg.subject}": ${
(err as Error)?.stack ?? err
}`,
);
}
}
}
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { SecretService } from './secret.service';
@Module({
imports: [],
providers: [SecretService],
exports: [SecretService],
})
export class SecretModule {}
@@ -0,0 +1,152 @@
import { Injectable, Logger } from '@nestjs/common';
import { InfisicalSDK } from '@infisical/sdk';
import { ConfigService } from '@nestjs/config';
type SecretMap = Record<string, string | boolean | undefined>;
@Injectable()
export class SecretService {
private logger = new Logger(SecretService.name);
private secrets: SecretMap | null = null; // null = 아직 로드 전
private loading?: Promise<void>; // 진행 중인 로드(중복 호출 방지)
private infisicalClient: InfisicalSDK;
private readonly siteUrl: string;
private readonly clientSecret: string;
private readonly clientId: string;
private readonly projectId: string;
private readonly PROCESS_ENVS = [
'COSMOSDB_ENDPOINT',
'COSMOSDB_KEY',
'NATS_ENDPOINT',
'NATS_TOKEN',
'NATS_URL',
'DAKAO_INBOUND_SUBJECT',
'DAKAO_NATS_COMMAND_CHAT_SEND_MESSAGE_SUBJECT',
'DAKAO_REPLY_SUBJECT_PREFIX',
'DAKAO_COMMAND_SOURCE',
'DAKAO_BOT_USER_ID',
'DAKAO_INSTANCE_ID',
'JWT_CLAIM_AUDIENCE',
'MICROSOFT_ENTRAID_TENANT_ID',
'MICROSOFT_ENTRAID_ALLOWED_ALGS',
'AUTH_DEV_HEADER_ENABLED',
// 필요시 추가
] as const;
constructor(private readonly config: ConfigService) {
this.siteUrl =
this.config.get<string>('INFISICAL_SITE_URL') ??
'https://secret-manager.dahyeon.us/';
this.clientSecret = this.config.get<string>(
'INFISICAL_CLIENT_SECRET',
) as string;
this.clientId = this.config.get<string>('INFISICAL_CLIENT_ID') as string;
this.projectId = this.config.get<string>('INFISICAL_PROJECT_ID') as string;
// console.log(this.siteUrl, this.clientSecret, this.clientId, this.projectId)
const missing = [
!this.siteUrl && 'INFISICAL_SITE_URL',
!this.clientId && 'INFISICAL_CLIENT_ID',
!this.clientSecret && 'INFISICAL_CLIENT_SECRET',
!this.projectId && 'INFISICAL_PROJECT_ID',
].filter(Boolean);
if (missing.length > 0) {
throw new Error(
`Missing required Infisical configuration: ${missing.join(', ')}`,
);
}
this.infisicalClient = new InfisicalSDK({ siteUrl: this.siteUrl });
}
/** 한 번만 로그인/페치. 동시에 여러 번 호출돼도 같은 Promise를 공유 */
private async fetchSecrets(): Promise<void> {
if (this.secrets) return; // 이미 로드됨
if (this.loading) return this.loading; // 로드 중이면 그거 기다리기
this.loading = (async () => {
await this.infisicalClient.auth().universalAuth.login({
clientId: this.clientId,
clientSecret: this.clientSecret,
});
// console.log(this.infisicalClient)
const res = await this.infisicalClient.secrets().listSecrets({
environment:
process.env.NODE_ENV === 'prod'
? 'prod'
: process.env.NODE_ENV === 'test'
? 'staging'
: 'dev',
projectId: this.projectId,
});
// 임시 객체에 채운 뒤 한 번에 교체(일관성 보장)
const map: SecretMap = {};
for (const s of res.secrets) {
map[s.secretKey] = s.secretValue;
}
// 필요한 값은 process.env에 반영
for (const k of this.PROCESS_ENVS) {
const v = map[k as string];
if (typeof v === 'string' && v.length > 0) {
process.env[k as string] = v;
}
}
this.secrets = map;
this.logger.log(
`Fetched ${Object.keys(map).length} secrets from Infisical`,
);
})();
try {
await this.loading;
} finally {
this.loading = undefined; // 실패/성공 모두 끝나면 해제(실패 시 재시도 가능)
}
}
/** lazy get: 필요할 때 처음 한번만 로드 */
public async get<T = string>(key: string, fallback?: T): Promise<T> {
if (!this.secrets) {
await this.fetchSecrets();
}
// console.log('Secrets loaded:', this.secrets);
const val = this.secrets![key];
if (val === undefined) {
const cfg = this.config.get<T>(key);
if (cfg !== undefined) return cfg;
if (fallback !== undefined) return fallback;
throw new Error(`Secret not found for key="${key}"`);
}
return val as T;
}
/** 필요 시: 동기 접근(초기화 안됐으면 에러). constructor 등에서 강제 사용 금지 */
public getSync<T = string>(key: string, fallback?: T): T {
if (!this.secrets) {
const cfg = this.config.get<T>(key);
if (cfg !== undefined) return cfg;
if (fallback !== undefined) return fallback;
throw new Error(`SecretService not initialized yet (key="${key}")`);
}
const val = this.secrets[key];
if (val === undefined) {
const cfg = this.config.get<T>(key);
if (cfg !== undefined) return cfg;
if (fallback !== undefined) return fallback;
throw new Error(`Secret not found for key="${key}"`);
}
return val as T;
}
/** 선택: 사전에 강제로 로드하고 싶을 때 호출 */
public async ready(): Promise<void> {
await this.fetchSecrets();
}
}