From 070fc7d4443f9676cfa96ef00dcca093d63d7e06 Mon Sep 17 00:00:00 2001 From: dohyeons Date: Mon, 25 Aug 2025 17:07:29 +0900 Subject: [PATCH] =?UTF-8?q?=EB=B0=B1=EC=97=94=EB=93=9C=20config=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=EB=93=A4=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 - backend-node/src/config/database.ts | 45 ++++++++++ backend-node/src/config/environment.ts | 117 +++++++++++++++++++++++++ 3 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 backend-node/src/config/database.ts create mode 100644 backend-node/src/config/environment.ts diff --git a/.gitignore b/.gitignore index 380a5719..26fdcbad 100644 --- a/.gitignore +++ b/.gitignore @@ -190,7 +190,6 @@ docker-compose.prod.yml .env.docker # 설정 파일들 -config/ configs/ settings/ *.config.js diff --git a/backend-node/src/config/database.ts b/backend-node/src/config/database.ts new file mode 100644 index 00000000..2d13d368 --- /dev/null +++ b/backend-node/src/config/database.ts @@ -0,0 +1,45 @@ +import { PrismaClient } from "@prisma/client"; +import config from "./environment"; + +// Prisma 클라이언트 인스턴스 생성 +const prisma = new PrismaClient({ + datasources: { + db: { + url: config.databaseUrl, + }, + }, + log: config.debug ? ["query", "info", "warn", "error"] : ["error"], +}); + +// 데이터베이스 연결 테스트 +async function testConnection() { + try { + await prisma.$connect(); + console.log("✅ 데이터베이스 연결 성공"); + } catch (error) { + console.error("❌ 데이터베이스 연결 실패:", error); + process.exit(1); + } +} + +// 애플리케이션 종료 시 연결 해제 +process.on("beforeExit", async () => { + await prisma.$disconnect(); +}); + +process.on("SIGINT", async () => { + await prisma.$disconnect(); + process.exit(0); +}); + +process.on("SIGTERM", async () => { + await prisma.$disconnect(); + process.exit(0); +}); + +// 초기 연결 테스트 (개발 환경에서만) +if (config.nodeEnv === "development") { + testConnection(); +} + +export default prisma; diff --git a/backend-node/src/config/environment.ts b/backend-node/src/config/environment.ts new file mode 100644 index 00000000..be936f76 --- /dev/null +++ b/backend-node/src/config/environment.ts @@ -0,0 +1,117 @@ +import dotenv from "dotenv"; +import path from "path"; + +// .env 파일 로드 +dotenv.config({ path: path.resolve(process.cwd(), ".env") }); + +interface Config { + // 서버 설정 + port: number; + host: string; + nodeEnv: string; + + // 데이터베이스 설정 + databaseUrl: string; + + // JWT 설정 + jwt: { + secret: string; + expiresIn: string; + refreshExpiresIn: string; + }; + + // 보안 설정 + bcryptRounds: number; + sessionSecret: string; + + // CORS 설정 + cors: { + origin: string; + credentials: boolean; + }; + + // 로깅 설정 + logging: { + level: string; + file: string; + }; + + // API 설정 + apiPrefix: string; + apiVersion: string; + + // 파일 업로드 설정 + maxFileSize: number; + uploadDir: string; + + // 이메일 설정 + smtpHost: string; + smtpPort: number; + smtpUser: string; + smtpPass: string; + + // Redis 설정 + redisUrl: string; + + // 개발 환경 설정 + debug: boolean; + showErrorDetails: boolean; +} + +const config: Config = { + // 서버 설정 + port: parseInt(process.env.PORT || "3000", 10), + host: process.env.HOST || "0.0.0.0", + nodeEnv: process.env.NODE_ENV || "development", + + // 데이터베이스 설정 + databaseUrl: + process.env.DATABASE_URL || + "postgresql://postgres:postgres@localhost:5432/ilshin", + + // JWT 설정 + jwt: { + secret: process.env.JWT_SECRET || "ilshin-plm-super-secret-jwt-key-2024", + expiresIn: process.env.JWT_EXPIRES_IN || "24h", + refreshExpiresIn: process.env.JWT_REFRESH_EXPIRES_IN || "7d", + }, + + // 보안 설정 + bcryptRounds: parseInt(process.env.BCRYPT_ROUNDS || "12", 10), + sessionSecret: process.env.SESSION_SECRET || "ilshin-plm-session-secret-2024", + + // CORS 설정 + cors: { + origin: process.env.CORS_ORIGIN || "http://localhost:9771", + credentials: process.env.CORS_CREDENTIALS === "true", + }, + + // 로깅 설정 + logging: { + level: process.env.LOG_LEVEL || "info", + file: process.env.LOG_FILE || "logs/app.log", + }, + + // API 설정 + apiPrefix: process.env.API_PREFIX || "/api", + apiVersion: process.env.API_VERSION || "v1", + + // 파일 업로드 설정 + maxFileSize: parseInt(process.env.MAX_FILE_SIZE || "10485760", 10), + uploadDir: process.env.UPLOAD_DIR || "uploads", + + // 이메일 설정 + smtpHost: process.env.SMTP_HOST || "smtp.gmail.com", + smtpPort: parseInt(process.env.SMTP_PORT || "587", 10), + smtpUser: process.env.SMTP_USER || "", + smtpPass: process.env.SMTP_PASS || "", + + // Redis 설정 + redisUrl: process.env.REDIS_URL || "redis://localhost:6379", + + // 개발 환경 설정 + debug: process.env.DEBUG === "true", + showErrorDetails: process.env.SHOW_ERROR_DETAILS === "true", +}; + +export default config;