Skip to content

Commit

Permalink
Add Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
dennypradipta committed Jul 26, 2024
1 parent 69c7211 commit 355468d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 29 deletions.
4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

5 changes: 0 additions & 5 deletions .husky/pre-push

This file was deleted.

38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM node:20-alpine

WORKDIR /app

# Install dependencies
COPY package.json .
COPY package-lock.json .
RUN npm ci

# Copy source files
COPY . .

# Build TypeScript
RUN npm run build

FROM node:20-alpine

WORKDIR /app

# Copy built files
COPY --from=0 /app/dist ./dist

# Set environment variables
ENV NODE_ENV=production

# Install dependencies
COPY package.json .
COPY package-lock.json .
RUN npm ci

# Install PM2
RUN npm install -g pm2

# Expose port according to the environment
EXPOSE 8000

# Start the app
CMD ["pm2-runtime", "dist/index.js"]
28 changes: 12 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"format:check": "prettier --config .prettierrc 'src/**/*.ts' --check",
"lint": "eslint . --ext .ts",
"start": "npm run build && node dist/index.js",
"test": "echo \"Error: no test specified\" && exit 1",
"prepare": "husky install"
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
Expand All @@ -35,6 +34,7 @@
"dependencies": {
"@fastify/swagger": "^8.8.0",
"@fastify/swagger-ui": "^1.9.3",
"dotenv": "^16.4.5",
"fastify": "^4.21.0"
},
"devDependencies": {
Expand All @@ -44,7 +44,6 @@
"eslint": "^8.46.0",
"eslint-config-prettier": "^8.9.0",
"eslint-plugin-prettier": "^5.0.0",
"husky": "^8.0.0",
"prettier": "^3.0.0",
"typescript": "^5.1.6"
}
Expand Down
12 changes: 12 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import dotenv from 'dotenv';

// Get environment variables
dotenv.config();

const config = {
port: parseInt(process.env.PORT as string, 10) || 8000,
host: process.env.HOST || '0.0.0.0',
environment: process.env.NODE_ENV || 'development'
};

export default config;
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Fastify from 'fastify';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUi from '@fastify/swagger-ui';
import config from './config';

import { getHealthCheck } from './handlers/health-check';
import { getStatusSimulator } from './handlers/status-simulator';
Expand All @@ -15,6 +16,9 @@ import * as BodySimulatorSchema from './schemas/body-simulator.json';
import * as LoginSimulatorSchema from './schemas/login-simulator.json';
import * as IdentitySimulatorSchema from './schemas/identity-simulator.json';

// Get environment variables
const { port, host } = config;

// Initiate Fastify
const fastify = Fastify({
logger: true
Expand Down Expand Up @@ -65,7 +69,7 @@ fastify.register(
try {
fastify.log.info('Monika Alert Simulator is running...');
await fastify.ready();
await fastify.listen({ port: 8000 });
await fastify.listen({ port, host });
fastify.swagger();
} catch (err) {
fastify.log.error(err);
Expand Down

0 comments on commit 355468d

Please sign in to comment.