refactor: improve environment configuration resolution and centralize Prisma instance management

This commit is contained in:
이정수 2026-04-21 11:35:18 +09:00
parent f8c95945e7
commit 706dda5def
6 changed files with 49 additions and 4 deletions

View File

@ -0,0 +1,10 @@
const path = require('path');
console.log('CWD:', process.cwd());
console.log('__dirname:', __dirname);
console.log('.env path:', path.resolve(process.cwd(), '.env'));
const fs = require('fs');
console.log('.env exists in CWD?', fs.existsSync(path.resolve(process.cwd(), '.env')));
console.log('.env exists in root?', fs.existsSync(path.resolve(process.cwd(), '../../.env')));
require('dotenv').config({ path: path.resolve(process.cwd(), '../../.env') });
console.log('DATABASE_URL from ../../.env:', process.env.DATABASE_URL);

View File

@ -1,9 +1,21 @@
import { config } from 'dotenv'; import { config } from 'dotenv';
import { existsSync } from 'fs';
import { hostname } from 'os'; import { hostname } from 'os';
import { resolve } from 'path'; import { resolve } from 'path';
// Prefer systemd/cron-set DOTENV_CONFIG_PATH; otherwise cwd .env (default dotenv behavior). const getEnvPath = () => {
config({ path: process.env.DOTENV_CONFIG_PATH || resolve(process.cwd(), '.env') }); if (process.env.DOTENV_CONFIG_PATH) return process.env.DOTENV_CONFIG_PATH;
const localEnv = resolve(process.cwd(), '.env');
if (existsSync(localEnv)) return localEnv;
const rootEnv = resolve(process.cwd(), '../../.env');
if (existsSync(rootEnv)) return rootEnv;
return localEnv;
};
config({ path: getEnvPath() });
const generateInstanceId = () => { const generateInstanceId = () => {
return process.env.INSTANCE_ID || hostname() || `kord-${Math.random().toString(36).substring(2, 7)}`; return process.env.INSTANCE_ID || hostname() || `kord-${Math.random().toString(36).substring(2, 7)}`;

View File

@ -38,6 +38,11 @@ export const prisma = new PrismaClient({
}); });
export const connectDB = async () => { export const connectDB = async () => {
if (!env.DATABASE_URL) {
logger.error('DATABASE_URL is not set. Please check your .env file.');
process.exit(1);
}
try { try {
// Adapter-based client connects when first used, // Adapter-based client connects when first used,
// but we can test the pool connection here. // but we can test the pool connection here.
@ -46,6 +51,9 @@ export const connectDB = async () => {
logger.info('Connected to PostgreSQL successfully via Driver Adapter.'); logger.info('Connected to PostgreSQL successfully via Driver Adapter.');
} catch (error) { } catch (error) {
logger.error('Failed to connect to PostgreSQL:', error); logger.error('Failed to connect to PostgreSQL:', error);
if (error instanceof Error && error.message.includes('password')) {
logger.error('Database authentication failed. Please check your DATABASE_URL password.');
}
process.exit(1); process.exit(1);
} }
}; };

View File

@ -8,7 +8,8 @@ import { auditLogService } from '../services/AuditLogService';
import { env } from '../config/env'; import { env } from '../config/env';
import { PrismaShardStatusRepository, prisma } from '@kord/db'; import { PrismaShardStatusRepository } from '@kord/db';
import { prisma } from '../database';
export default { export default {
name: Events.ClientReady, name: Events.ClientReady,
once: true, once: true,

View File

@ -787,6 +787,18 @@ export class FishingService {
} }
private static resolveResourcePath(relativePath: string) { private static resolveResourcePath(relativePath: string) {
// Current file: apps/bot/src/services/FishingService.ts
// 1:src/services, 2:src, 3:bot, 4:apps, 5:root
// After re-evaluating:
// apps/bot/src/services -> .. -> src -> .. -> bot -> .. -> apps -> .. -> root (Total 4 levels)
const rootPath = path.resolve(__dirname, '..', '..', '..', '..');
const candidatePath = path.resolve(rootPath, relativePath);
if (fs.existsSync(candidatePath)) {
return candidatePath;
}
// Fallback to local if root doesn't have it (unlikely in this monorepo)
return path.resolve(__dirname, '..', '..', relativePath); return path.resolve(__dirname, '..', '..', relativePath);
} }

View File

@ -1,6 +1,8 @@
import { PrismaClient, ShardStatus } from '@prisma/client'; import { PrismaClient, ShardStatus } from '@prisma/client';
export const prisma = new PrismaClient(); // We no longer export a default prisma instance here to avoid initialization conflicts.
// Consumers should provide their own configured PrismaClient instance.
// export const prisma = new PrismaClient();
export * from '@prisma/client'; export * from '@prisma/client';