Skip to content

Commit

Permalink
refactor: app.ts 코드 분리
Browse files Browse the repository at this point in the history
DB 연결 Loader 파일로 분리
port listen 역할 server.ts로 분리 - 테스트 코드에서 app import 해야하는데 이때 서버가 실행되지 않도록 분리
  • Loading branch information
ekek54 committed Dec 12, 2022
1 parent 1e26012 commit 4b9ee2d
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 57 deletions.
2 changes: 1 addition & 1 deletion server/src/Board/Board.Controller.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
2 changes: 1 addition & 1 deletion server/src/Form/Form.Caching.ts
Original file line number Diff line number Diff line change
@@ -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}`);
Expand Down
2 changes: 1 addition & 1 deletion server/src/Form/Form.Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions server/src/Loader/Mongo.Loader.ts
Original file line number Diff line number Diff line change
@@ -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...");
}
}
);
}
16 changes: 16 additions & 0 deletions server/src/Loader/MySQL.Loader.ts
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 18 additions & 0 deletions server/src/Loader/Redis.Loader.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 1 addition & 1 deletion server/src/Response/Response.Service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
53 changes: 0 additions & 53 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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());
Expand All @@ -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;
11 changes: 11 additions & 0 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 4b9ee2d

Please sign in to comment.