diff --git a/fems-api/src/app.js b/fems-api/src/app.js index 89231ad..7b0ef6b 100644 --- a/fems-api/src/app.js +++ b/fems-api/src/app.js @@ -10,34 +10,48 @@ const registerRoutes = require("./routes"); const app = express(); -// Middleware +// Middleware 설정 app.use(cors(config.cors)); app.use(express.json()); app.use(express.urlencoded({ extended: true })); app.use(requestLogger); -// Register all routes +// 라우트 등록 registerRoutes(app); -// Error handling +// 에러 핸들링 app.use(errorHandler); -// Database initialization and server start +const waitForDatabase = async (retries = 5, interval = 2000) => { + for (let i = 0; i < retries; i++) { + try { + await sequelize.authenticate(); + logger.info("Database connection established successfully."); + return true; + } catch (error) { + logger.warn(`Database connection attempt ${i + 1} failed:`, error); + if (i < retries - 1) { + logger.info(`Retrying in ${interval / 1000} seconds...`); + await new Promise((resolve) => setTimeout(resolve, interval)); + } + } + } + throw new Error("Could not connect to database after multiple attempts"); +}; + const initializeServer = async () => { try { - await sequelize.authenticate(); - logger.info("Database connection established successfully."); + // 데이터베이스 연결 대기 + await waitForDatabase(); - // Sync database (in development only) + // 개발 환경에서만 데이터베이스 동기화 if (process.env.NODE_ENV !== "production") { - // force: true 옵션을 사용하여 테이블을 재생성 await sequelize.sync({ force: true }); logger.info("Database synchronized."); - - // 초기 데이터 생성 await require("./utils/createInitialAdmin")(); } + // 서버 시작 const port = config.port; app.listen(port, () => { logger.info(`Server is running on port ${port}`); diff --git a/fems-api/src/config/config.js b/fems-api/src/config/config.js index 1e2a49e..358a6d9 100644 --- a/fems-api/src/config/config.js +++ b/fems-api/src/config/config.js @@ -14,6 +14,16 @@ module.exports = { database: process.env.POSTGRES_DB, dialect: "postgres", logging: true, + pool: { + max: 5, + min: 0, + acquire: 30000, + idle: 10000, + }, + retry: { + max: 5, + timeout: 3000, + }, }, jwt: { secret: process.env.JWT_SECRET, @@ -32,12 +42,22 @@ module.exports = { port: process.env.PORT || 3001, database: { host: process.env.POSTGRES_HOST, - port: parseInt(process.env.POSTGRES_PORT), + port: parseInt(process.env.POSTGRES_PORT) || 5432, username: process.env.POSTGRES_USER, password: process.env.POSTGRES_PASSWORD, database: process.env.POSTGRES_DB, dialect: "postgres", - logging: false, + logging: true, + pool: { + max: 5, + min: 0, + acquire: 30000, + idle: 10000, + }, + retry: { + max: 5, + timeout: 3000, + }, }, jwt: { secret: process.env.JWT_SECRET, diff --git a/fems-postgres/pg_hba.conf b/fems-postgres/pg_hba.conf index 2e92ce7..c43ce83 100644 --- a/fems-postgres/pg_hba.conf +++ b/fems-postgres/pg_hba.conf @@ -1,10 +1,10 @@ # fems-postgres/pg_hba.conf # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only +# TYPE DATABASE USER ADDRESS METHOD local all all scram-sha-256 -# IPv4 local connections: host all all 127.0.0.1/32 scram-sha-256 -# IPv6 local connections: host all all ::1/128 scram-sha-256 -# Allow connections from Docker network -host all all 0.0.0.0/0 scram-sha-256 \ No newline at end of file +host all all 0.0.0.0/0 scram-sha-256 +host all all 172.16.0.0/12 scram-sha-256 +host all all 192.168.0.0/16 scram-sha-256 \ No newline at end of file