typescript-express-starter 8.1.3 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.kr.md +23 -27
- package/README.md +23 -25
- package/lib/default/Makefile +26 -3
- package/lib/default/package.json +9 -12
- package/lib/graphql/.env.development.local +2 -2
- package/lib/graphql/.env.production.local +2 -2
- package/lib/graphql/.env.test.local +2 -2
- package/lib/graphql/Makefile +26 -3
- package/lib/graphql/package.json +8 -11
- package/lib/graphql/src/app.ts +6 -3
- package/lib/graphql/src/config/index.ts +2 -1
- package/lib/graphql/src/entities/users.entity.ts +1 -1
- package/lib/graphql/src/repositories/auth.repository.ts +1 -1
- package/lib/graphql/src/repositories/users.repository.ts +1 -1
- package/lib/knex/Makefile +26 -3
- package/lib/knex/package.json +23 -26
- package/lib/mikro-orm/.dockerignore +18 -0
- package/lib/mikro-orm/.editorconfig +9 -0
- package/lib/mikro-orm/.env.development.local +18 -0
- package/lib/mikro-orm/.env.production.local +18 -0
- package/lib/mikro-orm/.env.test.local +18 -0
- package/lib/mikro-orm/.eslintignore +1 -0
- package/lib/mikro-orm/.eslintrc +18 -0
- package/lib/mikro-orm/.huskyrc +5 -0
- package/lib/mikro-orm/.lintstagedrc.json +5 -0
- package/lib/mikro-orm/.prettierrc +8 -0
- package/lib/mikro-orm/.swcrc +41 -0
- package/lib/mikro-orm/.vscode/launch.json +35 -0
- package/lib/mikro-orm/.vscode/settings.json +6 -0
- package/lib/mikro-orm/Dockerfile +24 -0
- package/lib/mikro-orm/Makefile +29 -0
- package/lib/mikro-orm/docker-compose.yml +46 -0
- package/lib/mikro-orm/ecosystem.config.js +57 -0
- package/lib/mikro-orm/jest.config.js +12 -0
- package/lib/mikro-orm/nginx.conf +40 -0
- package/lib/mikro-orm/nodemon.json +12 -0
- package/lib/mikro-orm/package.json +77 -0
- package/lib/mikro-orm/src/app.ts +98 -0
- package/lib/mikro-orm/src/config/index.ts +5 -0
- package/lib/mikro-orm/src/controllers/auth.controller.ts +46 -0
- package/lib/mikro-orm/src/controllers/index.controller.ts +13 -0
- package/lib/mikro-orm/src/controllers/users.controller.ts +65 -0
- package/lib/mikro-orm/src/databases/index.ts +19 -0
- package/lib/mikro-orm/src/dtos/users.dto.ts +9 -0
- package/lib/mikro-orm/src/entities/base.entity.ts +16 -0
- package/lib/mikro-orm/src/entities/users.entity.ts +17 -0
- package/lib/mikro-orm/src/exceptions/HttpException.ts +10 -0
- package/lib/mikro-orm/src/http/auth.http +32 -0
- package/lib/mikro-orm/src/http/users.http +34 -0
- package/lib/mikro-orm/src/interfaces/auth.interface.ts +15 -0
- package/lib/mikro-orm/src/interfaces/routes.interface.ts +6 -0
- package/lib/mikro-orm/src/interfaces/users.interface.ts +5 -0
- package/lib/mikro-orm/src/middlewares/auth.middleware.ts +32 -0
- package/lib/mikro-orm/src/middlewares/error.middleware.ts +17 -0
- package/lib/mikro-orm/src/middlewares/validation.middleware.ts +25 -0
- package/lib/mikro-orm/src/routes/auth.route.ts +24 -0
- package/lib/mikro-orm/src/routes/index.route.ts +19 -0
- package/lib/mikro-orm/src/routes/users.route.ts +25 -0
- package/lib/mikro-orm/src/server.ts +11 -0
- package/lib/mikro-orm/src/services/auth.service.ts +63 -0
- package/lib/mikro-orm/src/services/users.service.ts +68 -0
- package/lib/mikro-orm/src/tests/auth.test.ts +66 -0
- package/lib/mikro-orm/src/tests/index.test.ts +18 -0
- package/lib/mikro-orm/src/tests/users.test.ts +70 -0
- package/lib/mikro-orm/src/utils/logger.ts +65 -0
- package/lib/mikro-orm/src/utils/util.ts +19 -0
- package/lib/mikro-orm/src/utils/validateEnv.ts +10 -0
- package/lib/mikro-orm/swagger.yaml +122 -0
- package/lib/mikro-orm/tsconfig.json +41 -0
- package/lib/mongoose/Makefile +26 -3
- package/lib/mongoose/package.json +7 -10
- package/lib/prisma/Makefile +26 -3
- package/lib/prisma/package.json +7 -10
- package/lib/routing-controllers/Makefile +26 -3
- package/lib/routing-controllers/package.json +8 -10
- package/lib/sequelize/Makefile +26 -3
- package/lib/sequelize/package.json +7 -10
- package/lib/starter.js +30 -8
- package/lib/typegoose/Makefile +26 -3
- package/lib/typegoose/package.json +6 -9
- package/lib/typeorm/Makefile +26 -3
- package/lib/typeorm/package.json +6 -9
- package/package.json +6 -15
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# PORT
|
|
2
|
+
PORT = 3000
|
|
3
|
+
|
|
4
|
+
# DATABASE
|
|
5
|
+
DB_HOST = localhost
|
|
6
|
+
DB_PORT = 27017
|
|
7
|
+
DB_DATABASE = prod
|
|
8
|
+
|
|
9
|
+
# TOKEN
|
|
10
|
+
SECRET_KEY = secretKey
|
|
11
|
+
|
|
12
|
+
# LOG
|
|
13
|
+
LOG_FORMAT = combined
|
|
14
|
+
LOG_DIR = ../logs
|
|
15
|
+
|
|
16
|
+
# CORS
|
|
17
|
+
ORIGIN = your.domain.com
|
|
18
|
+
CREDENTIALS = true
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
/dist
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"parser": "@typescript-eslint/parser",
|
|
3
|
+
"extends": ["prettier", "plugin:@typescript-eslint/recommended", "plugin:prettier/recommended"],
|
|
4
|
+
"parserOptions": {
|
|
5
|
+
"ecmaVersion": 2018,
|
|
6
|
+
"sourceType": "module"
|
|
7
|
+
},
|
|
8
|
+
"rules": {
|
|
9
|
+
"@typescript-eslint/explicit-member-accessibility": 0,
|
|
10
|
+
"@typescript-eslint/explicit-function-return-type": 0,
|
|
11
|
+
"@typescript-eslint/no-parameter-properties": 0,
|
|
12
|
+
"@typescript-eslint/interface-name-prefix": 0,
|
|
13
|
+
"@typescript-eslint/explicit-module-boundary-types": 0,
|
|
14
|
+
"@typescript-eslint/no-explicit-any": "off",
|
|
15
|
+
"@typescript-eslint/ban-types": "off",
|
|
16
|
+
"@typescript-eslint/no-var-requires": "off"
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"jsc": {
|
|
3
|
+
"parser": {
|
|
4
|
+
"syntax": "typescript",
|
|
5
|
+
"tsx": false,
|
|
6
|
+
"dynamicImport": true,
|
|
7
|
+
"decorators": true
|
|
8
|
+
},
|
|
9
|
+
"transform": {
|
|
10
|
+
"legacyDecorator": true,
|
|
11
|
+
"decoratorMetadata": true
|
|
12
|
+
},
|
|
13
|
+
"target": "es2017",
|
|
14
|
+
"externalHelpers": false,
|
|
15
|
+
"keepClassNames": true,
|
|
16
|
+
"loose": false,
|
|
17
|
+
"minify": {
|
|
18
|
+
"compress": false,
|
|
19
|
+
"mangle": false
|
|
20
|
+
},
|
|
21
|
+
"baseUrl": "src",
|
|
22
|
+
"paths": {
|
|
23
|
+
"@/*": ["*"],
|
|
24
|
+
"@config": ["config"],
|
|
25
|
+
"@controllers/*": ["controllers/*"],
|
|
26
|
+
"@databases": ["databases"],
|
|
27
|
+
"@dtos/*": ["dtos/*"],
|
|
28
|
+
"@entities/*": ["entities/*"],
|
|
29
|
+
"@exceptions/*": ["exceptions/*"],
|
|
30
|
+
"@interfaces/*": ["interfaces/*"],
|
|
31
|
+
"@middlewares/*": ["middlewares/*"],
|
|
32
|
+
"@models/*": ["models/*"],
|
|
33
|
+
"@routes/*": ["routes/*"],
|
|
34
|
+
"@services/*": ["services/*"],
|
|
35
|
+
"@utils/*": ["utils/*"]
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"module": {
|
|
39
|
+
"type": "commonjs"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"configurations": [
|
|
4
|
+
{
|
|
5
|
+
"type": "node-terminal",
|
|
6
|
+
"request": "launch",
|
|
7
|
+
"name": "Dev typescript-express-starter",
|
|
8
|
+
"command": "npm run dev"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"type": "node-terminal",
|
|
12
|
+
"request": "launch",
|
|
13
|
+
"name": "Start typescript-express-starter",
|
|
14
|
+
"command": "npm run start"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"type": "node-terminal",
|
|
18
|
+
"request": "launch",
|
|
19
|
+
"name": "Test typescript-express-starter",
|
|
20
|
+
"command": "npm run test"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"type": "node-terminal",
|
|
24
|
+
"request": "launch",
|
|
25
|
+
"name": "Lint typescript-express-starter",
|
|
26
|
+
"command": "npm run lint"
|
|
27
|
+
},
|
|
28
|
+
{
|
|
29
|
+
"type": "node-terminal",
|
|
30
|
+
"request": "launch",
|
|
31
|
+
"name": "Lint:Fix typescript-express-starter",
|
|
32
|
+
"command": "npm run lint:fix"
|
|
33
|
+
}
|
|
34
|
+
]
|
|
35
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Common build stage
|
|
2
|
+
FROM node:14.14.0-alpine3.12 as common-build-stage
|
|
3
|
+
|
|
4
|
+
COPY . ./app
|
|
5
|
+
|
|
6
|
+
WORKDIR /app
|
|
7
|
+
|
|
8
|
+
RUN npm install
|
|
9
|
+
|
|
10
|
+
EXPOSE 3000
|
|
11
|
+
|
|
12
|
+
# Development build stage
|
|
13
|
+
FROM common-build-stage as development-build-stage
|
|
14
|
+
|
|
15
|
+
ENV NODE_ENV development
|
|
16
|
+
|
|
17
|
+
CMD ["npm", "run", "dev"]
|
|
18
|
+
|
|
19
|
+
# Production build stage
|
|
20
|
+
FROM common-build-stage as production-build-stage
|
|
21
|
+
|
|
22
|
+
ENV NODE_ENV production
|
|
23
|
+
|
|
24
|
+
CMD ["npm", "run", "start"]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# app name should be overridden.
|
|
2
|
+
# ex) production-stage: make build APP_NAME=<APP_NAME>
|
|
3
|
+
# ex) development-stage: make build-dev APP_NAME=<APP_NAME>
|
|
4
|
+
|
|
5
|
+
APP_NAME = typescript-express
|
|
6
|
+
APP_NAME := $(APP_NAME)
|
|
7
|
+
|
|
8
|
+
.PHONY: build
|
|
9
|
+
# Build the container image - Dvelopment
|
|
10
|
+
build-dev:
|
|
11
|
+
docker build -t ${APP_NAME}\
|
|
12
|
+
--target development-build-stage\
|
|
13
|
+
-f Dockerfile .
|
|
14
|
+
|
|
15
|
+
# Build the container image - Production
|
|
16
|
+
build:
|
|
17
|
+
docker build -t ${APP_NAME}\
|
|
18
|
+
--target production-build-stage\
|
|
19
|
+
-f Dockerfile .
|
|
20
|
+
|
|
21
|
+
# Clean the container image
|
|
22
|
+
clean:
|
|
23
|
+
docker rmi -f ${APP_NAME}
|
|
24
|
+
|
|
25
|
+
# Run the container image
|
|
26
|
+
run:
|
|
27
|
+
docker run -d -it -p 3000:3000 ${APP_NAME}
|
|
28
|
+
|
|
29
|
+
all: build
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
version: '3.7'
|
|
2
|
+
|
|
3
|
+
services:
|
|
4
|
+
proxy:
|
|
5
|
+
image: nginx:alpine
|
|
6
|
+
container_name: proxy
|
|
7
|
+
ports:
|
|
8
|
+
- '80:80'
|
|
9
|
+
volumes:
|
|
10
|
+
- ./nginx.conf:/etc/nginx/nginx.conf
|
|
11
|
+
restart: 'unless-stopped'
|
|
12
|
+
networks:
|
|
13
|
+
- backend
|
|
14
|
+
server:
|
|
15
|
+
build:
|
|
16
|
+
context: ./
|
|
17
|
+
target: development-build-stage
|
|
18
|
+
dockerfile: Dockerfile
|
|
19
|
+
container_name: server
|
|
20
|
+
ports:
|
|
21
|
+
- '3000:3000'
|
|
22
|
+
volumes:
|
|
23
|
+
- ./:/app
|
|
24
|
+
- /app/node_modules
|
|
25
|
+
restart: 'unless-stopped'
|
|
26
|
+
networks:
|
|
27
|
+
- backend
|
|
28
|
+
links:
|
|
29
|
+
- mongo
|
|
30
|
+
depends_on:
|
|
31
|
+
- mongo
|
|
32
|
+
mongo:
|
|
33
|
+
image: mvertes/alpine-mongo
|
|
34
|
+
container_name: mongo
|
|
35
|
+
ports:
|
|
36
|
+
- '27017:27017'
|
|
37
|
+
networks:
|
|
38
|
+
- backend
|
|
39
|
+
|
|
40
|
+
networks:
|
|
41
|
+
backend:
|
|
42
|
+
driver: bridge
|
|
43
|
+
|
|
44
|
+
volumes:
|
|
45
|
+
data:
|
|
46
|
+
driver: local
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description pm2 configuration file.
|
|
3
|
+
* @example
|
|
4
|
+
* production mode :: pm2 start ecosystem.config.js --only prod
|
|
5
|
+
* development mode :: pm2 start ecosystem.config.js --only dev
|
|
6
|
+
*/
|
|
7
|
+
module.exports = {
|
|
8
|
+
apps: [
|
|
9
|
+
{
|
|
10
|
+
name: 'prod', // pm2 start App name
|
|
11
|
+
script: 'dist/server.js',
|
|
12
|
+
exec_mode: 'cluster', // 'cluster' or 'fork'
|
|
13
|
+
instance_var: 'INSTANCE_ID', // instance variable
|
|
14
|
+
instances: 2, // pm2 instance count
|
|
15
|
+
autorestart: true, // auto restart if process crash
|
|
16
|
+
watch: false, // files change automatic restart
|
|
17
|
+
ignore_watch: ['node_modules', 'logs'], // ignore files change
|
|
18
|
+
max_memory_restart: '1G', // restart if process use more than 1G memory
|
|
19
|
+
merge_logs: true, // if true, stdout and stderr will be merged and sent to pm2 log
|
|
20
|
+
output: './logs/access.log', // pm2 log file
|
|
21
|
+
error: './logs/error.log', // pm2 error log file
|
|
22
|
+
env: { // environment variable
|
|
23
|
+
PORT: 3000,
|
|
24
|
+
NODE_ENV: 'production',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'dev', // pm2 start App name
|
|
29
|
+
script: 'ts-node', // ts-node
|
|
30
|
+
args: '-r tsconfig-paths/register --transpile-only src/server.ts', // ts-node args
|
|
31
|
+
exec_mode: 'cluster', // 'cluster' or 'fork'
|
|
32
|
+
instance_var: 'INSTANCE_ID', // instance variable
|
|
33
|
+
instances: 2, // pm2 instance count
|
|
34
|
+
autorestart: true, // auto restart if process crash
|
|
35
|
+
watch: false, // files change automatic restart
|
|
36
|
+
ignore_watch: ['node_modules', 'logs'], // ignore files change
|
|
37
|
+
max_memory_restart: '1G', // restart if process use more than 1G memory
|
|
38
|
+
merge_logs: true, // if true, stdout and stderr will be merged and sent to pm2 log
|
|
39
|
+
output: './logs/access.log', // pm2 log file
|
|
40
|
+
error: './logs/error.log', // pm2 error log file
|
|
41
|
+
env: { // environment variable
|
|
42
|
+
PORT: 3000,
|
|
43
|
+
NODE_ENV: 'development',
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
deploy: {
|
|
48
|
+
production: {
|
|
49
|
+
user: 'user',
|
|
50
|
+
host: '0.0.0.0',
|
|
51
|
+
ref: 'origin/master',
|
|
52
|
+
repo: 'git@github.com:repo.git',
|
|
53
|
+
path: 'dist/server.js',
|
|
54
|
+
'post-deploy': 'npm install && npm run build && pm2 reload ecosystem.config.js --only prod',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const { pathsToModuleNameMapper } = require('ts-jest');
|
|
2
|
+
const { compilerOptions } = require('./tsconfig.json');
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
preset: 'ts-jest',
|
|
6
|
+
testEnvironment: 'node',
|
|
7
|
+
roots: ['<rootDir>/src'],
|
|
8
|
+
transform: {
|
|
9
|
+
'^.+\\.tsx?$': 'ts-jest',
|
|
10
|
+
},
|
|
11
|
+
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src' }),
|
|
12
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
user nginx;
|
|
2
|
+
worker_processes 1;
|
|
3
|
+
|
|
4
|
+
error_log /var/log/nginx/error.log warn;
|
|
5
|
+
pid /var/run/nginx.pid;
|
|
6
|
+
|
|
7
|
+
events {
|
|
8
|
+
worker_connections 1024;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
http {
|
|
12
|
+
include /etc/nginx/mime.types;
|
|
13
|
+
default_type application/octet-stream;
|
|
14
|
+
|
|
15
|
+
upstream api-server {
|
|
16
|
+
server server:3000;
|
|
17
|
+
keepalive 100;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
server {
|
|
21
|
+
listen 80;
|
|
22
|
+
server_name localhost;
|
|
23
|
+
|
|
24
|
+
location / {
|
|
25
|
+
proxy_http_version 1.1;
|
|
26
|
+
proxy_pass http://api-server;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
|
32
|
+
'$status $body_bytes_sent "$http_referer" '
|
|
33
|
+
'"$http_user_agent" "$http_x_forwarded_for"';
|
|
34
|
+
|
|
35
|
+
access_log /var/log/nginx/access.log main;
|
|
36
|
+
|
|
37
|
+
sendfile on;
|
|
38
|
+
keepalive_timeout 65;
|
|
39
|
+
include /etc/nginx/conf.d/*.conf;
|
|
40
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "typescript-express-starter",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "TypeScript + MikroORM + MongoDB + Express API Server",
|
|
5
|
+
"author": "",
|
|
6
|
+
"license": "ISC",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"start": "npm run build && cross-env NODE_ENV=production node dist/server.js",
|
|
9
|
+
"dev": "cross-env NODE_ENV=development nodemon",
|
|
10
|
+
"build": "swc src -d dist --source-maps --copy-files",
|
|
11
|
+
"build:tsc": "tsc && tsc-alias",
|
|
12
|
+
"test": "jest --forceExit --detectOpenHandles",
|
|
13
|
+
"lint": "eslint --ext .ts src/",
|
|
14
|
+
"lint:fix": "npm run lint -- --fix",
|
|
15
|
+
"deploy:prod": "npm run build && pm2 start ecosystem.config.js --only prod",
|
|
16
|
+
"deploy:dev": "pm2 start ecosystem.config.js --only dev"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@mikro-orm/core": "^5.1.2",
|
|
20
|
+
"@mikro-orm/mongodb": "^5.1.2",
|
|
21
|
+
"bcrypt": "^5.0.1",
|
|
22
|
+
"class-transformer": "^0.5.1",
|
|
23
|
+
"class-validator": "^0.13.1",
|
|
24
|
+
"compression": "^1.7.4",
|
|
25
|
+
"cookie-parser": "^1.4.5",
|
|
26
|
+
"cors": "^2.8.5",
|
|
27
|
+
"dotenv": "^10.0.0",
|
|
28
|
+
"envalid": "^7.1.0",
|
|
29
|
+
"express": "^4.17.1",
|
|
30
|
+
"helmet": "^4.6.0",
|
|
31
|
+
"hpp": "^0.2.3",
|
|
32
|
+
"jsonwebtoken": "^8.5.1",
|
|
33
|
+
"morgan": "^1.10.0",
|
|
34
|
+
"swagger-jsdoc": "^6.0.0",
|
|
35
|
+
"swagger-ui-express": "^4.1.6",
|
|
36
|
+
"winston": "^3.3.3",
|
|
37
|
+
"winston-daily-rotate-file": "^4.5.5"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@mikro-orm/mongo-highlighter": "^1.0.0",
|
|
41
|
+
"@swc/cli": "^0.1.51",
|
|
42
|
+
"@swc/core": "^1.2.108",
|
|
43
|
+
"@types/bcrypt": "^5.0.0",
|
|
44
|
+
"@types/compression": "^1.7.1",
|
|
45
|
+
"@types/cookie-parser": "^1.4.2",
|
|
46
|
+
"@types/cors": "^2.8.11",
|
|
47
|
+
"@types/express": "^4.17.13",
|
|
48
|
+
"@types/hpp": "^0.2.1",
|
|
49
|
+
"@types/jest": "27.4.x",
|
|
50
|
+
"@types/jsonwebtoken": "^8.5.4",
|
|
51
|
+
"@types/morgan": "^1.9.3",
|
|
52
|
+
"@types/node": "^16.0.1",
|
|
53
|
+
"@types/supertest": "^2.0.11",
|
|
54
|
+
"@types/swagger-jsdoc": "^6.0.1",
|
|
55
|
+
"@types/swagger-ui-express": "^4.1.3",
|
|
56
|
+
"@typescript-eslint/eslint-plugin": "^4.28.2",
|
|
57
|
+
"@typescript-eslint/parser": "^4.28.2",
|
|
58
|
+
"cross-env": "^7.0.3",
|
|
59
|
+
"eslint": "^7.30.0",
|
|
60
|
+
"eslint-config-prettier": "^8.3.0",
|
|
61
|
+
"eslint-plugin-prettier": "^3.4.0",
|
|
62
|
+
"husky": "^7.0.1",
|
|
63
|
+
"jest": "27.4.x",
|
|
64
|
+
"lint-staged": "^11.0.0",
|
|
65
|
+
"node-config": "^0.0.2",
|
|
66
|
+
"node-gyp": "^8.1.0",
|
|
67
|
+
"nodemon": "^2.0.9",
|
|
68
|
+
"pm2": "^5.1.0",
|
|
69
|
+
"prettier": "^2.3.2",
|
|
70
|
+
"supertest": "^6.2.2",
|
|
71
|
+
"ts-jest": "^27.1.4",
|
|
72
|
+
"ts-node": "^10.4.0",
|
|
73
|
+
"tsc-alias": "^1.4.1",
|
|
74
|
+
"tsconfig-paths": "^3.10.1",
|
|
75
|
+
"typescript": "^4.5.2"
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import cookieParser from 'cookie-parser';
|
|
3
|
+
import compression from 'compression';
|
|
4
|
+
import cors from 'cors';
|
|
5
|
+
import helmet from 'helmet';
|
|
6
|
+
import hpp from 'hpp';
|
|
7
|
+
import morgan from 'morgan';
|
|
8
|
+
import swaggerJSDoc from 'swagger-jsdoc';
|
|
9
|
+
import swaggerUi from 'swagger-ui-express';
|
|
10
|
+
import { logger, stream } from '@utils/logger';
|
|
11
|
+
import { MikroORM, RequestContext } from '@mikro-orm/core';
|
|
12
|
+
import { NODE_ENV, PORT, LOG_FORMAT, ORIGIN, CREDENTIALS } from '@config';
|
|
13
|
+
import { DI, dbOptions } from '@databases';
|
|
14
|
+
import { UserEntity } from '@entities/users.entity';
|
|
15
|
+
import { Routes } from '@interfaces/routes.interface';
|
|
16
|
+
import errorMiddleware from '@middlewares/error.middleware';
|
|
17
|
+
|
|
18
|
+
class App {
|
|
19
|
+
public app: express.Application;
|
|
20
|
+
public env: string;
|
|
21
|
+
public port: string | number;
|
|
22
|
+
public host: string;
|
|
23
|
+
|
|
24
|
+
constructor(routes: Routes[]) {
|
|
25
|
+
this.app = express();
|
|
26
|
+
this.env = NODE_ENV || 'development';
|
|
27
|
+
this.port = PORT || 3000;
|
|
28
|
+
|
|
29
|
+
this.initializeMiddlewares();
|
|
30
|
+
this.connectToDatabase();
|
|
31
|
+
this.initializeRoutes(routes);
|
|
32
|
+
this.initializeSwagger();
|
|
33
|
+
this.initializeErrorHandling();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public listen() {
|
|
37
|
+
this.app.listen(this.port, () => {
|
|
38
|
+
logger.info(`=================================`);
|
|
39
|
+
logger.info(`======= ENV: ${this.env} =======`);
|
|
40
|
+
logger.info(`🚀 App listening on the port ${this.port}`);
|
|
41
|
+
logger.info(`=================================`);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
public getServer() {
|
|
46
|
+
return this.app;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private async connectToDatabase() {
|
|
50
|
+
try {
|
|
51
|
+
DI.orm = await MikroORM.init(dbOptions);
|
|
52
|
+
DI.em = DI.orm.em.fork();
|
|
53
|
+
DI.userRepository = DI.orm.em.fork().getRepository(UserEntity);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
logger.error(error);
|
|
56
|
+
}
|
|
57
|
+
this.app.use((_1, _2, next) => RequestContext.create(DI.orm.em, next));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private initializeMiddlewares() {
|
|
61
|
+
this.app.use(morgan(LOG_FORMAT, { stream }));
|
|
62
|
+
this.app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS }));
|
|
63
|
+
this.app.use(hpp());
|
|
64
|
+
this.app.use(helmet());
|
|
65
|
+
this.app.use(compression());
|
|
66
|
+
this.app.use(express.json());
|
|
67
|
+
this.app.use(express.urlencoded({ extended: true }));
|
|
68
|
+
this.app.use(cookieParser());
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private initializeRoutes(routes: Routes[]) {
|
|
72
|
+
routes.forEach(route => {
|
|
73
|
+
this.app.use('/', route.router);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private initializeSwagger() {
|
|
78
|
+
const options = {
|
|
79
|
+
swaggerDefinition: {
|
|
80
|
+
info: {
|
|
81
|
+
title: 'REST API',
|
|
82
|
+
version: '1.0.0',
|
|
83
|
+
description: 'Example docs',
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
apis: ['swagger.yaml'],
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const specs = swaggerJSDoc(options);
|
|
90
|
+
this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private initializeErrorHandling() {
|
|
94
|
+
this.app.use(errorMiddleware);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export default App;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
config({ path: `.env.${process.env.NODE_ENV || 'development'}.local` });
|
|
3
|
+
|
|
4
|
+
export const CREDENTIALS = process.env.CREDENTIALS === 'true';
|
|
5
|
+
export const { NODE_ENV, PORT, DB_HOST, DB_PORT, DB_DATABASE, SECRET_KEY, LOG_FORMAT, LOG_DIR, ORIGIN } = process.env;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { CreateUserDto } from '@dtos/users.dto';
|
|
3
|
+
import { RequestWithUser } from '@interfaces/auth.interface';
|
|
4
|
+
import { User } from '@interfaces/users.interface';
|
|
5
|
+
import AuthService from '@services/auth.service';
|
|
6
|
+
|
|
7
|
+
class AuthController {
|
|
8
|
+
public authService = new AuthService();
|
|
9
|
+
|
|
10
|
+
public signUp = async (req: Request, res: Response, next: NextFunction) => {
|
|
11
|
+
try {
|
|
12
|
+
const userData: CreateUserDto = req.body;
|
|
13
|
+
const signUpUserData: User = await this.authService.signup(userData);
|
|
14
|
+
|
|
15
|
+
res.status(201).json({ data: signUpUserData, message: 'signup' });
|
|
16
|
+
} catch (error) {
|
|
17
|
+
next(error);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
public logIn = async (req: Request, res: Response, next: NextFunction) => {
|
|
22
|
+
try {
|
|
23
|
+
const userData: CreateUserDto = req.body;
|
|
24
|
+
const { cookie, findUser } = await this.authService.login(userData);
|
|
25
|
+
|
|
26
|
+
res.setHeader('Set-Cookie', [cookie]);
|
|
27
|
+
res.status(200).json({ data: findUser, message: 'login' });
|
|
28
|
+
} catch (error) {
|
|
29
|
+
next(error);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
public logOut = async (req: RequestWithUser, res: Response, next: NextFunction) => {
|
|
34
|
+
try {
|
|
35
|
+
const userData: User = req.user;
|
|
36
|
+
const logOutUserData: User = await this.authService.logout(userData);
|
|
37
|
+
|
|
38
|
+
res.setHeader('Set-Cookie', ['Authorization=; Max-age=0']);
|
|
39
|
+
res.status(200).json({ data: logOutUserData, message: 'logout' });
|
|
40
|
+
} catch (error) {
|
|
41
|
+
next(error);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default AuthController;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
|
|
3
|
+
class IndexController {
|
|
4
|
+
public index = (req: Request, res: Response, next: NextFunction) => {
|
|
5
|
+
try {
|
|
6
|
+
res.sendStatus(200);
|
|
7
|
+
} catch (error) {
|
|
8
|
+
next(error);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default IndexController;
|