Skip to content

Commit

Permalink
chore: update configs and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
francislagares committed Jul 23, 2024
1 parent 9fbb377 commit 10938ed
Show file tree
Hide file tree
Showing 23 changed files with 2,191 additions and 3,681 deletions.
64 changes: 32 additions & 32 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,35 @@ jobs:
node-version: [22.x]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v3
with:
version: 9
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path | tr -d '\n')" >> $GITHUB_ENV
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install Dependencies
run: pnpm install

- name: Run Tests
run: pnpm test:ci
- name: Checkout
uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v3
with:
version: 9
run_install: false

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path | tr -d '\n')" >> $GITHUB_ENV
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Setup pnpm cache
uses: actions/cache@v3
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install Dependencies
run: pnpm install

- name: Run Tests
run: pnpm test:ci
6 changes: 1 addition & 5 deletions .husky/commit-msg
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
. "$(dirname "$0")/common.sh"

pnpm commitlint --edit $1
pnpm commitlint --edit
8 changes: 0 additions & 8 deletions .husky/common.sh

This file was deleted.

6 changes: 1 addition & 5 deletions .husky/pre-commit
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
. "$(dirname "$0")/common.sh"

pnpm lint-staged
pnpm lint-staged
3 changes: 0 additions & 3 deletions .husky/pre-push
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

pnpm test:ci
4 changes: 0 additions & 4 deletions .husky/prepare-commit-msg
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
. "$(dirname "$0")/common.sh"

exec < /dev/tty && pnpm cz --hook || true
2 changes: 1 addition & 1 deletion api/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RUN addgroup backend && adduser -S -G backend francis

USER francis

WORKDIR /node-api
WORKDIR /api

COPY package.json tsconfig.json ./

Expand Down
4 changes: 1 addition & 3 deletions api/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ services:
env_file: .env.development.local
depends_on:
- mongodb
- redis
networks:
- mern
ports:
- 4000:4000
environment:
- NODE_ENV=development
- BASE_URL=http://localhost:4000
volumes:
- .:/node-api
- node_modules:/node-api/node_modules
Expand Down
7 changes: 4 additions & 3 deletions api/ecosystem.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
apps: [
{
name: 'prod', // pm2 start App name
script: '../dist/api/src/server.js',
script: './dist/src/server.js',
exec_mode: 'cluster', // 'cluster' or 'fork'
instance_var: 'INSTANCE_ID', // instance variable
instances: 2, // pm2 instance count
Expand All @@ -27,8 +27,9 @@ module.exports = {
},
{
name: 'dev', // pm2 start App name
script: './node_modules/.bin/ts-node', // ts-node
args: '-r tsconfig-paths/register --transpile-only src/server.ts', // ts-node args
script: './src/server.ts',
interpreter: 'node',
interpreter_args: '--import tsx',
exec_mode: 'cluster', // 'cluster' or 'fork'
instance_var: 'INSTANCE_ID', // instance variable
instances: 1, // pm2 instance count
Expand Down
20 changes: 10 additions & 10 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"author": "Francis Lagares",
"license": "MIT",
"scripts": {
"start:dev": "pnpm prisma:generate && tsx watch src/server.ts",
"start": "pnpm build && NODE_ENV=production && node dist/server.js",
"start:dev": "pnpm prisma:generate && tsx watch src/app/index.ts",
"start": "pnpm build && NODE_ENV=production && node dist/src/index.js",
"build": "tsc && tsc-alias",
"deploy:clean": "pm2 stop all && pm2 delete all",
"deploy:dev": "pm2 start ecosystem.config.js --only dev && pm2 logs all",
Expand All @@ -24,7 +24,7 @@
"seed": "ts-node --transpile-only src/prisma/seed.ts"
},
"dependencies": {
"@prisma/client": "^5.16.1",
"@prisma/client": "^5.17.0",
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
Expand All @@ -39,8 +39,8 @@
"http-status-codes": "^2.2.0",
"ioredis": "^5.3.2",
"jsonwebtoken": "^9.0.0",
"prisma": "^5.16.1",
"winston": "^3.9.0"
"prisma": "^5.17.0",
"winston": "^3.13.1"
},
"devDependencies": {
"@types/bcrypt": "^5.0.0",
Expand All @@ -51,13 +51,13 @@
"@types/express": "^4.17.17",
"@types/hpp": "^0.2.2",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^20.2.5",
"@types/node": "^20.14.11",
"@types/supertest": "^6.0.2",
"@types/swagger-jsdoc": "^6.0.1",
"@types/swagger-ui-express": "^4.1.3",
"dotenv-cli": "^7.2.1",
"nodemon": "^3.1.4",
"pm2": "^5.3.0",
"pm2": "^5.4.2",
"supertest": "^7.0.0",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
Expand All @@ -74,8 +74,8 @@
}
},
"engines": {
"node": ">=22.0.0",
"pnpm": ">=9.4.0"
"node": ">=22.5.0",
"pnpm": ">=9.6.0"
},
"packageManager": "pnpm@9.4.0+sha512.f549b8a52c9d2b8536762f99c0722205efc5af913e77835dbccc3b0b0b2ca9e7dc8022b78062c17291c48e88749c70ce88eb5a74f1fa8c4bf5e18bb46c8bd83a"
"packageManager": "pnpm@9.6.0+sha512.38dc6fba8dba35b39340b9700112c2fe1e12f10b17134715a4aa98ccf7bb035e76fd981cf0bb384dfa98f8d6af5481c2bef2f4266a24bfa20c34eb7147ce0b5e"
}
129 changes: 38 additions & 91 deletions api/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,55 @@
import compression from 'compression';
import cookieParser from 'cookie-parser';
import cors from 'cors';
import { Application, json, urlencoded } from 'express';
import helmet from 'helmet';
import hpp from 'hpp';
import swaggerJSDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';

import { ErrorMiddleware } from '@/libs/shared/middlewares/error.middleware';
import express from 'express';

import logger from '@/utils/logger';

import { MongoDBInstance as dbConnection } from '@/config/database';
import {
BASE_URL,
CLIENT_URL,
CREDENTIALS,
HOST,
NODE_ENV,
ORIGIN,
PORT,
} from '@/config/environment';

import applicationRoutes from '@/routes/index';
import { Server } from '@/server';

export class App {
private app: Application;
public init() {
const httpServer = new Server(express());

constructor(app: Application) {
this.app = app;
httpServer.start();
}

public start(): void {
this.connectDatabase();
this.securityMiddleware(this.app);
this.routesMiddleware(this.app);
this.globalErrorHandler(this.app);
this.startServer(this.app);
this.initializeSwagger();
}

public getServer() {
return this.app;
}

private securityMiddleware(app: Application): void {
app.use(cors({ origin: ORIGIN, credentials: CREDENTIALS }));
app.use(hpp());
app.use(helmet());
app.use(compression());
app.use(json());
app.use(urlencoded({ extended: true, limit: '50mb' }));
app.use(cookieParser());
app.use(
cors({
origin: CLIENT_URL,
credentials: true,
optionsSuccessStatus: 200,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
}),
);
}
private static handleExit(): void {
process.on('uncaughtException', (error: Error) => {
logger.error(`There was an uncaught error: ${error}`);
App.shutDownProperly(1);
});

private routesMiddleware(app: Application): void {
applicationRoutes(app);
}
process.on('unhandledRejection', (reason: Error) => {
logger.error(`Unhandled rejection at promise: ${reason}`);
App.shutDownProperly(2);
});

private globalErrorHandler(app: Application): void {
app.use(ErrorMiddleware);
}
process.on('SIGTERM', () => {
logger.error('Caught SIGTERM');
App.shutDownProperly(2);
});

private connectDatabase(): void {
dbConnection.getInstance();
}
process.on('SIGINT', () => {
logger.error('Caught SIGINT');
App.shutDownProperly(2);
});

private startServer(app: Application): void {
logger.info(`------ NODE_ENV: ${NODE_ENV} ------`);
logger.info(`Server has started with process ${process.pid}`);
app.listen(PORT, () => {
logger.info(`Server listening on port ${PORT}`);
process.on('exit', () => {
logger.error('Exiting');
});
}

private initializeSwagger() {
const options = {
swaggerDefinition: {
openapi: '3.0.3',
info: {
title: 'API Documentation',
version: '1.0.0',
description: 'REST API docs',
},
servers: [
{
url: `${HOST}:${PORT}${BASE_URL}`,
},
],
},
apis: ['./swagger.yaml'],
};

const swaggerSpecs = swaggerJSDoc(options);
this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpecs));

logger.info(`Docs are available at ${HOST}:${PORT}/api-docs`);
private static shutDownProperly(exitCode: number): void {
Promise.resolve()
.then(() => {
logger.info('Shutdown complete');
process.exit(exitCode);
})
.catch(error => {
logger.error(`Error during shutdown: ${error}`);
process.exit(1);
});
}
}

const app = new App();

app.init();
Loading

0 comments on commit 10938ed

Please sign in to comment.