diff --git a/server/src/Board/Board.Controller.ts b/server/src/Board/Board.Controller.ts index 987069f..6004c41 100644 --- a/server/src/Board/Board.Controller.ts +++ b/server/src/Board/Board.Controller.ts @@ -1,5 +1,5 @@ import { Request, Response, NextFunction } from "express"; -import { redisCli } from "../app"; +import redisCli from "../Loader/Redis.Loader"; import BoardService from "./Board.Service"; import { searchKeyList, sortKeyList } from "./Board.Constants"; diff --git a/server/src/Form/Form.Caching.ts b/server/src/Form/Form.Caching.ts index 1258ad0..dcb678b 100644 --- a/server/src/Form/Form.Caching.ts +++ b/server/src/Form/Form.Caching.ts @@ -1,5 +1,5 @@ import { NextFunction, Request, Response } from "express"; -import { redisCli } from "../app"; +import redisCli from "../Loader/Redis.Loader"; const formCaching = async (req: Request, res: Response, next: NextFunction) => { const cachingResult = await redisCli.get(`form:${req.params.formId}`); diff --git a/server/src/Form/Form.Controller.ts b/server/src/Form/Form.Controller.ts index e69625a..6f23373 100644 --- a/server/src/Form/Form.Controller.ts +++ b/server/src/Form/Form.Controller.ts @@ -3,7 +3,7 @@ import InteranServerException from "../Common/Exceptions/InternalServer.Exceptio import BadRequestException from "../Common/Exceptions/BadRequest.Exception"; import FormService from "./Form.Service"; import { FormInDB } from "./Form.Interface"; -import { redisCli } from "../app"; +import redisCli from "../Loader/Redis.Loader"; class FormController { static async createNewForm(req: Request, res: Response, next: NextFunction) { diff --git a/server/src/Loader/Mongo.Loader.ts b/server/src/Loader/Mongo.Loader.ts new file mode 100644 index 0000000..14db425 --- /dev/null +++ b/server/src/Loader/Mongo.Loader.ts @@ -0,0 +1,17 @@ +import * as dotenv from "dotenv"; +import mongoose from "mongoose"; + +dotenv.config(); + +export default function connectMongoDB() { + mongoose.connect( + `mongodb+srv://${process.env.MONGODB_ID}:${process.env.MONGODB_PASSWORD}@cluster0.a7vmgdw.mongodb.net/database0?`, + (err) => { + if (err) { + console.log(err); + } else { + console.log("mongoDB is connected..."); + } + } + ); +} diff --git a/server/src/Loader/MySQL.Loader.ts b/server/src/Loader/MySQL.Loader.ts new file mode 100644 index 0000000..d3c4145 --- /dev/null +++ b/server/src/Loader/MySQL.Loader.ts @@ -0,0 +1,16 @@ +import * as dotenv from "dotenv"; +import "reflect-metadata"; +import { DataSource } from "typeorm"; + +dotenv.config(); +const myDataSource = new DataSource({ + type: "mysql", + host: process.env.TYPEORM_HOST || "", + port: Number(process.env.TYPEORM_PORT), + username: process.env.TYPEORM_USERNAME || "", + password: process.env.TYPEORM_PASSWORD || "", + database: process.env.TYPEORM_DATABASE || "", + entities: [`${__dirname}/../**/*.Model{.ts,.js}`], +}); + +export default myDataSource; diff --git a/server/src/Loader/Redis.Loader.ts b/server/src/Loader/Redis.Loader.ts new file mode 100644 index 0000000..ec26efe --- /dev/null +++ b/server/src/Loader/Redis.Loader.ts @@ -0,0 +1,18 @@ +import * as redis from "redis"; + +const redisClient = redis.createClient({ + url: `redis://@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/0`, + legacyMode: true, +}); + +redisClient.on("connect", () => { + console.info("Redis connected!"); +}); +redisClient.on("error", (err) => { + console.error("Redis Client Error", err); +}); + +redisClient.connect(); +const redisCli = redisClient.v4; + +export default redisCli; diff --git a/server/src/Response/Response.Service.ts b/server/src/Response/Response.Service.ts index 1060de7..46c900c 100644 --- a/server/src/Response/Response.Service.ts +++ b/server/src/Response/Response.Service.ts @@ -1,5 +1,5 @@ import FormResponse from "./Response.Model"; -import { redisCli } from "../app"; +import redisCli from "../Loader/Redis.Loader"; import { AnswerInterface, AnswerFromRequest } from "./Response.Interface"; class ResponseService { diff --git a/server/src/app.ts b/server/src/app.ts index 12606eb..8b1c4c0 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -5,11 +5,8 @@ import path from "path"; import cookieParser from "cookie-parser"; import logger from "morgan"; import * as dotenv from "dotenv"; -import mongoose from "mongoose"; import cors from "cors"; import "reflect-metadata"; -import { DataSource } from "typeorm"; -import * as redis from "redis"; import indexRouter from "./routes/index"; import errorMiddleware from "./Middlewares/Error.Middleware"; @@ -24,54 +21,6 @@ app.use( credentials: true, }) ); - -const myDataSource = new DataSource({ - type: "mysql", - host: process.env.TYPEORM_HOST || "", - port: Number(process.env.TYPEORM_PORT), - username: process.env.TYPEORM_USERNAME || "", - password: process.env.TYPEORM_PASSWORD || "", - database: process.env.TYPEORM_DATABASE || "", - entities: [`${__dirname}/**/*.Model{.ts,.js}`], -}); - -myDataSource.initialize().then(() => { - console.log("Data Source has been initialized!"); -}); - -// MongoDB 연결 -function connectDB() { - mongoose.connect( - `mongodb+srv://${process.env.MONGODB_ID}:${process.env.MONGODB_PASSWORD}@cluster0.a7vmgdw.mongodb.net/database0?`, - (err) => { - if (err) { - console.log(err); - } else { - console.log("mongoDB is connected..."); - } - } - ); -} - -connectDB(); - -// redis 연결 -const redisClient = redis.createClient({ - url: `redis://@${process.env.REDIS_HOST}:${process.env.REDIS_PORT}/0`, - legacyMode: true, -}); - -redisClient.on("connect", () => { - console.info("Redis connected!"); -}); -redisClient.on("error", (err) => { - console.error("Redis Client Error", err); -}); - -redisClient.connect(); - -export const redisCli = redisClient.v4; - // view engine setup app.use(logger("dev")); app.use(express.json()); @@ -88,6 +37,4 @@ app.use((req: Request, res: Response, next: NextFunction) => { // error handler app.use(errorMiddleware); -app.listen(process.env.PORT); - export default app; diff --git a/server/src/server.ts b/server/src/server.ts new file mode 100644 index 0000000..1dc80b6 --- /dev/null +++ b/server/src/server.ts @@ -0,0 +1,11 @@ +import app from "./app"; +import myDataSource from "./Loader/MySQL.Loader"; +import connectMongoDB from "./Loader/Mongo.Loader"; + +myDataSource.initialize().then(() => { + console.log("Data Source has been initialized!"); +}); + +connectMongoDB(); + +app.listen(process.env.PORT);