Skip to content

๐ŸŽ ์ƒ์ธ์„ ๋ฌผ ํŽ€๋”ฉ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ with nest.js

Notifications You must be signed in to change notification settings

gkqkehs7/egusajo-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ”‘ย About

b1e7733e-50ab-465b-bd52-b89334e9c62f.mp4

์ƒ์ผ ํŽ€๋”ฉํ•ด์ฃผ๋Š” ์•ฑ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž…๋‹ˆ๋‹ค.


๐Ÿ“Ž Souce Code

https://github.com/inha-commit/egusajo-nest


โš’๏ธย Project Architecture


๐Ÿ” Nest.js(v9.0.0) Request Pipeline


Joi๋ฅผ ์ด์šฉํ•œ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

Joi ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์œ ํšจ์„ฑ๊ณผ ํƒ€์ž…์„ ๊ฒ€์‚ฌํ•˜์—ฌ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋ฅผ ๋ฐฉ์ง€ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


Guard๋ฅผ ์ด์šฉํ•œ ์œ ์ € ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ

Guard๋ฅผ ์ด์šฉํ•˜์—ฌ request์˜ header๋ฅผ ์ฒดํฌํ•˜์—ฌ ์œ ์ € ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.


Winston.js๋ฅผ ํ†ตํ•œ ๋กœ๊ทธ ๊ธฐ๋ก

nest.js์˜ ๋‚ด์žฅ๋กœ๊ฑฐ๋ฅผ Winston.js ๋กœ ๋Œ€์ฒดํ•˜์—ฌ info warn error level๋กœ ๋‚˜๋ˆ„๊ณ  ์„œ๋ฒ„์— ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ, ์„œ๋ฒ„์—์„œ ๋ฐœ์ƒํ•˜๋Š” ๋ชจ๋“  ์—๋Ÿฌ์— ๋Œ€ํ•ด ํŒŒ์ผ๋กœ ๊ธฐ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค.


Pipe๋ฅผ ์ด์šฉํ•œ ์š”์ฒญ ๋ณ€์ˆ˜ ๊ฒ€์‚ฌ

Validation-pipe๋ฅผ ์ด์šฉํ•˜์—ฌ client์˜ ์š”์ฒญ์„ class-validator๋กœ ๊ฒ€์‚ฌํ•˜๊ณ , class-transformor๋กœ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜์ด ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ์— ๋Œ€ํ•ด ์š”์ฒญ์„ ๋ณ€ํ™˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


ModelConvertor๋กœ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์„œ๋น„์Šคํ•จ์ˆ˜์— ์ „๋‹ฌ

Database์—์„œ ๋ฐ›์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ Convertor๋กœ mappingํ•˜์—ฌ service๋กœ ์ „๋‹ฌํ•˜์˜€์Šต๋‹ˆ๋‹ค.


ResponseDto๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ Re-mapping

Service์—์„œ returnํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค์‹œ ResponseDto ๋กœ ๋‹ค์‹œ mappingํ•˜์—ฌ client์—๊ฒŒ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์ „์†กํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


Interceptor๋ฅผ ์ด์šฉํ•œ http ์‘๋‹ต์‹œ๊ฐ„ ๊ณ„์‚ฐ

Interceptor๋ฅผ ์ด์šฉํ•˜์—ฌ http ์š”์ฒญ์— ๋Œ€ํ•œ ์‘๋‹ต์‹œ๊ฐ„์„ ๊ณ„์‚ฐํ•˜๊ณ  ๊ธฐ๋กํ•˜์˜€์Šต๋‹ˆ๋‹ค.


ExceptionFilter๋ฅผ ํ™•์žฅํ•œ Error Filter Cutsomizing

nest.js์˜ ExceptionFilter ๋ฅผ ํ™•์žฅํ•˜์—ฌ ์„œ๋ฒ„์—์„œ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ๋ฅผ BadRequestError , UnauthorizedError , ForbiddenError , InternalServerError , NotFoundError ๋กœ ๋‚˜๋ˆ„์–ด classํ™” ํ•˜๊ณ  Front-end๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์—๋Ÿฌ๋“ค์— ๋Œ€ํ•ด์„  ErrorCode๋“ค์„ ๋ฏธ๋ฆฌ enum type์œผ๋กœ ์ •์˜ํ•ด ๋‘” ๋‹ค์Œ, ํ•ด๋‹น ์—๋Ÿฌ์— ๋งž๋Š” class๋กœ mappingํ•˜์—ฌ client์—๊ฒŒ ์ „์†กํ•˜์˜€์Šต๋‹ˆ๋‹ค.


๐Ÿ”ย TEST

Jest๋ฅผ ์ด์šฉํ•œ ์œ ๋‹› ํ…Œ์ŠคํŠธ

Jest ๋ฅผ ์ด์šฉํ•ด์„œ controller์™€ service ํ•จ์ˆ˜์— ๋Œ€ํ•ด ๊ฐ๊ฐ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ Jest๋ฅผ ์ด์šฉํ•œ unit test


Typeorm-extension์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ seeding

typeorm-extension์„ ์ด์šฉํ•˜์—ฌ faker.js๋กœ ๋ฐ์ดํ„ฐ๋ฅผ seeding ํ•˜๊ณ  ์œ ์ €๋ณ„๋กœ API๋ฅผ ํ…Œ์ŠคํŠธํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ typeorm-extension์œผ๋กœ ๋ฐ์ดํ„ฐ seedingํ•˜๊ธฐ


Artillery๋ฅผ ์ด์šฉํ•œ ๋ถ€ํ•˜ ํ…Œ์ŠคํŠธ

Artillery ๋ฅผ ์ด์šฉํ•˜์—ฌ 60์ดˆ ๋™์•ˆ ์ดˆ๋‹น ์š”์ฒญํšŸ์ˆ˜๋ฅผ ๋Š˜๋ ค๊ฐ€๋ฉด์„œ development production ๋ ˆ๋ฒจ์—์„œ ๊ฐ๊ฐ€ ๋ถ€ํ•˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ Artillery๋กœ ์„œ๋ฒ„์— ๋ถ€ํ•˜ํ…Œ์ŠคํŠธ ํ•˜๊ธฐ


Artillery ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ๋กœ ์ปจํ…Œ์ด๋„ˆ ๊ฐœ์ˆ˜ ์กฐ์ ˆ

Load Balancing ์ด ์ ์šฉ๋˜์–ด ์žˆ๋Š” ์‹ค์ œ ์„œ๋ฒ„์— Artillery ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜์—ฌ ์š”์ฒญ์„ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋Š” ์ ๋‹นํ•œ ์ปจํ…Œ์ด๋„ˆ ๊ฐœ์ˆ˜๋ฅผ ์„ ์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ Artillery ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•œ ์‹ค์ œ ์„œ๋ฒ„ ์ปจํ…Œ์ด๋„ˆ ๊ฐœ์ˆ˜ ์กฐ์ ˆ


Artillery ํ…Œ์ŠคํŠธ๋กœ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ๋ฐœ์ƒ ๋ฌธ์ œ ์ฝ”๋“œ ์ˆ˜์ •

Artillery ๋กœ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ฉฐ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ํ™•์ธํ•˜์˜€๊ณ , ๋””๋ฒ„๊น…ํ•˜์—ฌ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ์ฝ”๋“œ๋ฅผ ๋ฐœ๊ฒฌํ•˜๊ณ  ์ˆ˜์ •ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ Artillery ํ…Œ์ŠคํŠธ๋ฅผ ํ†ตํ•œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ๋ฐœ์ƒ ๋ฐ ์ฝ”๋“œ ์ˆ˜์ •


๐Ÿ” Github action

CI

docker/build-push-action@v4 ๋ฅผ ์ด์šฉํ•˜์—ฌ DockerFile์— ์ž‘์„ฑ๋œ ๋Œ€๋กœ ์šฐ๋ฆฌ์˜ ์„œ๋น„์Šค๋ฅผ ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ค์–ด Github packages์— ์ €์žฅ๋˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


CD

Github packages์— ์ €์žฅ๋œ ์ด๋ฏธ์ง€๋ฅผ appleboy/ssh-action@master ๋ฅผ ์ด์šฉํ•˜์—ฌ ์šฐ๋ฆฌ์˜ ํด๋ผ์šฐ๋“œ ์„œ๋ฒ„์— ์ ‘๊ทผํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ ์ปจํ…Œ์ด๋„ˆํ™” ํ•˜์—ฌ ์„œ๋น„์Šค๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ Github-action์„ ํ†ตํ•œ CI/CD


๐Ÿ” Docker

Docker-network๋ฅผ ์ด์šฉํ•œ ์ปจํ…Œ์ด๋„ˆ ๊ทธ๋ฃนํ™”

Docker-network ๋ฅผ ์ด์šฉํ•˜์—ฌ Server MySQL Redis๋ฅผ ํ•œ network๋กœ ๋ฌถ์–ด ์™ธ๋ถ€์—์„œ์˜ ์•ก์„ธ์Šค๋ฅผ ์ œ์–ดํ•˜๊ณ  ๋„คํŠธ์›Œํฌ ์ˆ˜์ค€์—์„œ ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


Docker-compose๋ฅผ ์ด์šฉํ•œ ์ปจํ…Œ์ด๋„ˆ ๊ด€๋ฆฌ

docker-compose.yml ํŒŒ์ผ์— ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‹คํ–‰ํ•˜๋Š”๋ฐ ํ•„์š”ํ•œ ๋„คํŠธ์›Œํฌ, ๋ณผ๋ฅจ, ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋“ฑ์„ ์ •์˜ํ•˜์—ฌ. ๊ฐ€๋…์„ฑ์ด ์ข‹๊ฒŒ ํ•˜์˜€๊ณ , ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ตฌ์„ฑ์„ ์ดํ•ดํ•˜๊ณ  ๊ณต์œ ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.


๐Ÿ” Nginx

Reverse proxy๋ฅผ ์ด์šฉํ•œ Https์ ์šฉ

nginx ๋ฅผ ์ด์šฉํ•ด http๋กœ 80๋ฒˆ port์— ๋“ค์–ด์˜จ ์š”์ฒญ๋“ค์— ๋Œ€ํ•ด์„œ ๋Œ€๋ฆฌ๋กœ ๋ฐ›์€ ๋‹ค์Œ, 443๋ฒˆ port๋กœ redirect์‹œ์ผœ์„œ ๋ณด๋‚ด์ฃผ๊ณ . ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๋Š”(reverse-proxy) ๋ฅผ ์ด์šฉํ•ด 4000๋ฒˆ์—์„œ ์ž‘๋™ํ•˜๊ณ  ์žˆ๋Š” node.js์„œ๋ฒ„๋กœ ์š”์ฒญ์ด ๊ฐ€๊ฒŒ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒย ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ๋Š”(reverse-proxy)๋ฅผ ํ†ตํ•ด ์„œ๋ฒ„๋ฅผ ๊ฐ์ถ”๊ณ , SSL/TLS ์•”ํ˜ธํ™”๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ „์†กํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ Http์™€ Https ๊ทธ๋ฆฌ๊ณ  SSL์€ ๊ฐ๊ฐ ๋ฌด์—‡์ผ๊นŒ? | โœ๏ธ Nginx๋ž€ ๋ฌด์—‡์ด๊ณ  ์™œ ์‚ฌ์šฉํ• ๊นŒ? | โœ๏ธ Nginx ์„ค์น˜ ๋ฐ Https ์ ์šฉํ•˜๊ธฐ


ํ•˜๋‚˜์˜ ์„œ๋ฒ„๋กœ ๋‘๊ฐœ์˜ port์— ๋Œ€ํ•ด ์„œ๋กœ๋‹ค๋ฅธ ๋„๋ฉ”์ธ ์ ์šฉํ•˜๊ธฐ

๋ฆฌ๋ˆ…์Šค ์„œ๋ฒ„์— ์ด๋ฏธ ๋„๋ฉ”์ธ์ด ์ ์šฉ๋˜์–ด ์žˆ์–ด์„œ Nginx์˜ reverse-proxy๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•˜๋‚˜์˜ ์„œ๋ฒ„์— ๋‘๊ฐœ์˜ ๋„๋ฉ”์ธ์ด ์ ์šฉ๋˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ ๋‘๊ฐœ์˜ port์— ๋Œ€ํ•ด ์„œ๋กœ๋‹ค๋ฅธ ๋„๋ฉ”์ธ ์ ์šฉํ•˜๊ธฐ


Docker + Nginx๋ฅผ ์ด์šฉํ•œ Load Balancing ์ ์šฉ

docker-compose ๋ฅผ ์ด์šฉํ•ด ์ปจํ…Œ์ด๋„ˆ ๊ฐœ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๊ณ  nginx ๋ฅผ ์ด์šฉํ•ด least_conn ๋ฐฉ์‹์„ ์ด์šฉํ•ด ํ˜„์žฌ ์—ฐ๊ฒฐ ์ƒํƒœ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ํŠธ๋ž˜ํ”ฝ์„ ๋ถ„์‚ฐ์‹œ์ผœ ์„œ๋ฒ„ ๊ฐ„์˜ ๋ถ€ํ•˜๋ฅผ ๊ท ๋“ฑํ•˜๊ฒŒ ๋ถ„๋ฐฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

โœ๏ธ Nginx + Docker๋กœ Load Balancingํ•˜๊ธฐ


About

๐ŸŽ ์ƒ์ธ์„ ๋ฌผ ํŽ€๋”ฉ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ with nest.js

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published