Skip to content

Commit

Permalink
Merge branch 'develop' into feature/#147
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeon-z committed Oct 7, 2022
2 parents 58e17b5 + dab74dd commit ec76e20
Show file tree
Hide file tree
Showing 49 changed files with 2,516 additions and 1,175 deletions.
642 changes: 638 additions & 4 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon",
"build": "tsc && node dist",
"local": "cross-env NODE_ENV=local nodemon",
"dev": "cross-env NODE_ENV=development nodemon",
"prod": "cross-env NODE_ENV=production nodemon",
"build-dev": "tsc && cross-env NODE_ENV=development node dist",
"build-prod": "tsc && cross-env NODE_ENV=production node dist",
"start-dev": "cross-env NODE_ENV=development pm2 start dist",
"start-prod": "cross-env NODE_ENV=production pm2 start dist",
"lint": "eslint ."
},
"author": "",
Expand All @@ -15,6 +20,7 @@
"@types/express": "^4.17.13",
"@types/jsonwebtoken": "^8.5.8",
"@types/mongoose": "^5.11.97",
"@types/morgan": "^1.9.3",
"@types/node": "^17.0.25",
"@types/pg": "^8.6.5",
"@typescript-eslint/eslint-plugin": "^5.32.0",
Expand All @@ -27,15 +33,23 @@
"typescript": "^4.7.4"
},
"dependencies": {
"app-root-path": "^3.1.0",
"axios": "^0.27.2",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"dayjs": "^1.11.5",
"dotenv": "^16.0.0",
"express": "^4.17.3",
"express-validator": "^6.14.2",
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.21",
"moment": "^2.29.4",
"moment-timezone": "^0.5.37",
"morgan": "^1.10.0",
"nanoid": "3.3.4",
"pg": "^8.7.3"
"pg": "^8.7.3",
"process": "^0.11.10",
"winston": "^3.8.2",
"winston-daily-rotate-file": "^4.7.1"
}
}
10 changes: 5 additions & 5 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ export default {
*/
port: parseInt(process.env.PORT as string, 10) as number,

user: process.env.PROD_DB_USER,
host: process.env.PROD_DB_HOST,
database: process.env.PROD_DB_DB,
password: process.env.PROD_DB_PASSWORD,

/**
* baseUrl
*/
baseUrl: process.env.BASE_URL as string,

/**
* baseUrl_dev
*/
baseUrlDev: process.env.BASE_URL_DEV as string,

/**
* jwt Secret
*/
Expand Down
93 changes: 93 additions & 0 deletions src/config/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { createLogger, transports, format } from 'winston';
import winstonDaily from 'winston-daily-rotate-file'; // 로그 파일 일자별로 ㄴ생성
import appRoot from 'app-root-path'; // app root 경로를 가져오는 lib
import process from 'process';
import moment from 'moment';
import 'moment-timezone';

const logDir = `${appRoot}/logs`; // logs 디렉토리 하위에 로그파일 저장

const { combine, printf, colorize, simple } = format;

const opts = {
info: new winstonDaily({
level: 'info',
datePattern: 'YYYY-MM-DD',
dirname: logDir,
filename: `%DATE%.log`,
maxFiles: 30, // 30일치 로그 파일 저장
zippedArchive: true,
}),
error: new winstonDaily({
level: 'error',
datePattern: 'YYYY-MM-DD',
dirname: logDir,
filename: `%DATE%.error.log`,
maxFiles: 30,
zippedArchive: true,
}),
exception: new winstonDaily({
level: 'error',
datePattern: 'YYYY-MM-DD',
dirname: logDir,
filename: `%DATE%.exception.log`,
maxFiles: 30,
zippedArchive: true,
}),
http: new winstonDaily({
level: 'http',
datePattern: 'YYYY-MM-DD',
dirname: logDir,
filename: `%DATE%.http.log`,
maxFiles: 30,
zippedArchive: true,
}),
console: new transports.Console({
format: combine(
colorize(), // 색깔 넣어서 출력
simple(), // `${info.level}: ${info.message} JSON.stringify({...rest})` 포맷으로 출력
),
}),
};

moment.tz.setDefault('Asia/Seoul');

const timestamp = () => moment().format('YYYY-MM-DD HH:mm:ss');

const logFormat = printf(({ level, message }) => {
return `${timestamp()}, ${level}:, ${message}`; // log 출력 포맷 정의
});

/**
* Log Level
* error: 0, warn: 1, info: 2, http: 3, verbose: 4, debug: 5, silly: 6
*/
const logger = createLogger({
format: combine(
logFormat, // log 출력 포맷
),
transports: [
// info 레벨 로그를 저장할 파일 설정
opts.info,
// error 레벨 로그를 저장할 파일 설정
opts.error,
// http 레벨 로그를 저장할 파일 설정
opts.http,
],

exceptionHandlers: [opts.exception],
});

// Production 환경이 아닌 경우(dev 등)
if (process.env.NODE_ENV !== 'production') {
logger.add(opts.console);
}

export class LoggerStream {
write(message: string) {
logger.http(message.substring(0, message.lastIndexOf('\n')));
}
}
export default {
logger,
};
22 changes: 14 additions & 8 deletions src/controllers/AloneListCategoryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { validationResult } from 'express-validator';
import { CategoryCreateDto, CategoryUpdateDto, CategoryDeleteDto } from '../interfaces/ICategory';
import { AloneListCategoryService } from '../services';
import db from '../loaders/db';
import logger from '../config/logger';

/**
* @route POST /list/alone/category
Expand All @@ -21,10 +22,10 @@ const createCategory = async (req: Request, res: Response) => {
}
let client;
const categoryCreateDto: CategoryCreateDto = req.body;

const userId = req.body.user.id;
try {
client = await db.connect(req);
const data = await AloneListCategoryService.createCategory(client, categoryCreateDto);
const data = await AloneListCategoryService.createCategory(client, userId, categoryCreateDto);

if (data === 'exceed_len') {
res
Expand All @@ -42,7 +43,7 @@ const createCategory = async (req: Request, res: Response) => {
.send(util.success(statusCode.OK, message.CREATE_ALONE_CATEGORY_SUCCESS, data));
}
} catch (error) {
console.log(error);
logger.logger.error(`POST, /list/alone/category, 혼자 패킹리스트 카테고리 생성, 500, ${error}`);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand All @@ -65,10 +66,10 @@ const updateCategory = async (req: Request, res: Response) => {
}
let client;
const categoryUpdateDto: CategoryUpdateDto = req.body;

const userId = req.body.user.id;
try {
client = await db.connect(req);
const data = await AloneListCategoryService.updateCategory(client, categoryUpdateDto);
const data = await AloneListCategoryService.updateCategory(client, userId, categoryUpdateDto);

if (data === 'no_list') {
res.status(statusCode.NOT_FOUND).send(util.fail(statusCode.NOT_FOUND, message.NO_LIST));
Expand All @@ -92,7 +93,9 @@ const updateCategory = async (req: Request, res: Response) => {
.send(util.success(statusCode.OK, message.UPDATE_ALONE_CATEGORY_SUCCESS, data));
}
} catch (error) {
console.log(error);
logger.logger.error(
`PATCH, /list/alone/category, 혼자 패킹리스트 카테고리 수정, 500, ${error}`,
);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand All @@ -114,9 +117,10 @@ const deleteCategory = async (req: Request, res: Response) => {
listId: listId,
categoryId: categoryId,
};
const userId = req.body.user.id;
try {
client = await db.connect(req);
const data = await AloneListCategoryService.deleteCategory(client, categoryDeleteDto);
const data = await AloneListCategoryService.deleteCategory(client, userId, categoryDeleteDto);
if (data === 'no_list') {
res.status(statusCode.NOT_FOUND).send(util.fail(statusCode.NOT_FOUND, message.NO_LIST));
} else if (data === 'no_category') {
Expand All @@ -131,7 +135,9 @@ const deleteCategory = async (req: Request, res: Response) => {
.send(util.success(statusCode.OK, message.DELETE_ALONE_CATEGORY_SUCCESS, data));
}
} catch (error) {
console.log(error);
logger.logger.error(
`DELETE, /list/alone/category, 혼자 패킹리스트 카테고리 삭제, 500, ${error}`,
);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand Down
16 changes: 10 additions & 6 deletions src/controllers/AloneListController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { validationResult } from 'express-validator';
import { ListCreateDto } from '../interfaces/IList';
import { AloneListService } from '../services';
import db from '../loaders/db';
import logger from '../config/logger';

/**
* @route POST /list/alone
Expand Down Expand Up @@ -40,7 +41,7 @@ const createAloneList = async (req: Request, res: Response) => {
.status(statusCode.OK)
.send(util.success(statusCode.OK, message.CREATE_ALONE_LIST_SUCCESS, data));
} catch (error) {
console.log(error);
logger.logger.error(`POST, /list/alone, 혼자 패킹리스트 생성, 500, ${error}`);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand Down Expand Up @@ -70,7 +71,7 @@ const getAloneList = async (req: Request, res: Response) => {
.status(statusCode.OK)
.send(util.success(statusCode.OK, message.READ_ALONE_LIST_SUCCESS, data));
} catch (error) {
console.log(error);
logger.logger.error(`GET, /list/alone/:listId, 혼자 패킹리스트 조회, 500, ${error}`);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand All @@ -81,18 +82,19 @@ const getAloneList = async (req: Request, res: Response) => {

/**
* @route DELETE /list/alone/:folderId/:listId
* @desc delete alone packinglist
* @desc delete alone list
* @access private
**/
const deleteAloneList = async (req: Request, res: Response) => {
let client;
const userId: number = req.body.user.id;
const { folderId } = req.params;
const { listId } = req.params;

try {
client = await db.connect(req);

const data = await AloneListService.deleteAloneList(client, folderId, listId);
const data = await AloneListService.deleteAloneList(client, userId, folderId, listId);

if (data === 'no_folder')
res.status(statusCode.NOT_FOUND).send(util.success(statusCode.NOT_FOUND, message.NO_FOLDER));
Expand All @@ -107,7 +109,9 @@ const deleteAloneList = async (req: Request, res: Response) => {
.status(statusCode.OK)
.send(util.success(statusCode.OK, message.DELETE_ALONE_LIST_SUCCESS, data));
} catch (error) {
console.log(error);
logger.logger.error(
`DELETE, /list/alone/:folderId/:listId, 혼자 패킹리스트 삭제, 500, ${error}`,
);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand Down Expand Up @@ -139,7 +143,7 @@ const getInviteAloneList = async (req: Request, res: Response) => {
.status(statusCode.OK)
.send(util.success(statusCode.OK, message.GET_INVITE_ALONE_LIST_SUCCESS, data));
} catch (error) {
console.log(error);
logger.logger.error(`GET, /list/alone/invite/:inviteCode, 혼자 패킹리스트 초대, 500, ${error}`);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand Down
21 changes: 15 additions & 6 deletions src/controllers/AloneListPackController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { validationResult } from 'express-validator';
import { PackCreateDto, PackUpdateDto, PackDeleteDto } from '../interfaces/IPack';
import { AloneListPackService } from '../services';
import db from '../loaders/db';
import logger from '../config/logger';

/**
* @route POST /list/alone/pack
Expand All @@ -21,12 +22,14 @@ const createPack = async (req: Request, res: Response) => {
}

let client;

const packCreateDto: PackCreateDto = req.body;
const userId = req.body.user.id;

try {
client = await db.connect(req);

const data = await AloneListPackService.createPack(client, packCreateDto);
const data = await AloneListPackService.createPack(client, userId, packCreateDto);

if (data === 'exceed_len')
res
Expand All @@ -46,7 +49,7 @@ const createPack = async (req: Request, res: Response) => {
.send(util.success(statusCode.OK, message.CREATE_ALONE_PACK_SUCCESS, data));
}
} catch (error) {
console.log(error);
logger.logger.error(`POST, /list/alone/pack, 혼자 패킹리스트 짐 생성, 500, ${error}`);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand All @@ -69,11 +72,14 @@ const updatePack = async (req: Request, res: Response) => {
}

let client;

const packUpdateDto: PackUpdateDto = req.body;
const userId = req.body.user.id;

try {
client = await db.connect(req);

const data = await AloneListPackService.updatePack(client, packUpdateDto);
const data = await AloneListPackService.updatePack(client, userId, packUpdateDto);

if (data === 'exceed_len')
res
Expand All @@ -99,7 +105,7 @@ const updatePack = async (req: Request, res: Response) => {
.send(util.success(statusCode.OK, message.UPDATE_ALONE_PACK_SUCCESS, data));
}
} catch (error) {
console.log(error);
logger.logger.error(`PATCH, /list/alone/pack, 혼자 패킹리스트 짐 수정, 500, ${error}`);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand All @@ -123,10 +129,11 @@ const deletePack = async (req: Request, res: Response) => {
categoryId: categoryId,
packId: packId,
};
const userId = req.body.user.id;

try {
client = await db.connect(req);
const data = await AloneListPackService.deletePack(client, packDeleteDto);
const data = await AloneListPackService.deletePack(client, userId, packDeleteDto);

if (data === 'no_list')
res.status(statusCode.NOT_FOUND).send(util.fail(statusCode.NOT_FOUND, message.NO_ALONE_LIST));
Expand All @@ -148,7 +155,9 @@ const deletePack = async (req: Request, res: Response) => {
.send(util.success(statusCode.OK, message.DELETE_ALONE_PACK_SUCCESS, data));
}
} catch (error) {
console.log(error);
logger.logger.error(
`DELETE, /list/alone/pack/:listId/:categoryId/:packId, 혼자 패킹리스트 짐 삭제, 500, ${error}`,
);
res
.status(statusCode.INTERNAL_SERVER_ERROR)
.send(util.fail(statusCode.INTERNAL_SERVER_ERROR, message.INTERNAL_SERVER_ERROR));
Expand Down
Loading

0 comments on commit ec76e20

Please sign in to comment.