Skip to content

Commit

Permalink
Merge pull request #103 from torusresearch/feat/optimize-backend
Browse files Browse the repository at this point in the history
Feat/optimize backend
  • Loading branch information
tanguyenvn authored Aug 19, 2024
2 parents 8709c40 + 2baf793 commit 7612076
Show file tree
Hide file tree
Showing 10 changed files with 543 additions and 631 deletions.
2 changes: 1 addition & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PORT=8060
REDIS_PORT=6379
REDIS_HOSTNAME=redis

LOG_LEVEL=INFO
LOG_LEVEL=debug
# Sentry
SENTRY_DSN=
SENTRY_TRACES_SAMPLE_RATE=0.01
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/push_to_ecr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push image to Amazon ECR
working-directory: .
working-directory: ./packages/fnd-server
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: fnd-server
Expand All @@ -55,13 +55,13 @@ jobs:
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:$GITHUB_SHA .
echo "Pushing image to ECR..."
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$GITHUB_SHA
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$GITHUB_SHA"
- name: Deploy Production env
timeout-minutes: 10
run: |
aws ecs update-service --cluster $ECS_CLUSTER_FND --service $ECS_SERVICE_FND --force-new-deployment
aws ecs wait services-stable --cluster $ECS_CLUSTER_FND --service $ECS_SERVICE_FND
env:
ECS_CLUSTER_FND: prod-microservices
ECS_SERVICE_FND: fnd-server-service
Expand All @@ -86,7 +86,7 @@ jobs:
uses: aws-actions/amazon-ecr-login@v2

- name: Build, tag, and push image to Amazon ECR
working-directory: .
working-directory: ./packages/fnd-server
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: fnd-server
Expand All @@ -95,13 +95,13 @@ jobs:
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:$GITHUB_SHA .
echo "Pushing image to ECR..."
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$GITHUB_SHA
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$GITHUB_SHA"
- name: Deploy Production env
timeout-minutes: 10
run: |
aws ecs update-service --cluster $ECS_CLUSTER_FND --service $ECS_SERVICE_FND --force-new-deployment
aws ecs wait services-stable --cluster $ECS_CLUSTER_FND --service $ECS_SERVICE_FND
env:
ECS_CLUSTER_FND: prod-microservices
ECS_SERVICE_FND: fnd-server-service
18 changes: 0 additions & 18 deletions Dockerfile

This file was deleted.

1,073 changes: 485 additions & 588 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"just:publish:lerna": "lerna publish",
"wait": "wait-port 8060",
"test": "cross-env FORCE_COLOR=1 lerna run test --parallel",
"server:dev": "docker-compose -f docker-compose.dev.yml up --build",
"server:detach": "docker-compose -f docker-compose.dev.yml up --build --detach",
"server:dev": "docker compose -f docker-compose.dev.yml up --build",
"server:detach": "docker compose -f docker-compose.dev.yml up --build --detach",
"test:ci": "npm run server:detach && wait-port 8060 && sleep 20 && npm run test",
"build": "cross-env FORCE_COLOR=1 lerna run --stream build",
"pack:lerna": "lerna exec -- rimraf toruslabs-*.tgz && lerna exec -- yarn pack",
Expand All @@ -55,4 +55,4 @@
"author": "",
"license": "MIT",
"description": ""
}
}
34 changes: 34 additions & 0 deletions packages/fnd-server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# for build
FROM node:20-alpine AS build

ENV NODE_OPTIONS --max-old-space-size=4096

WORKDIR /app

RUN apk add --no-cache --virtual .gyp \
python3 \
make \
g++

COPY package.json ./

RUN npm i

COPY . .

RUN npm run build

# for production
FROM node:20-alpine

ENV NODE_OPTIONS --max-old-space-size=4096

WORKDIR /app

COPY --from=build /app/package*.json ./

RUN npm ci --omit=dev --ignore-scripts

COPY --from=build /app/dist ./dist

CMD npm run prod
18 changes: 10 additions & 8 deletions packages/fnd-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,40 @@
"main": "src/index.ts",
"scripts": {
"serve": "nodemon --config nodemon.json",
"prod": "cross-env NODE_ENV=production && node dist/index.js",
"prod": "node dist/index.js",
"clean": "rimraf ./dist",
"build": "npm run clean && npm run build:server",
"build:server": "tsc",
"lint": "eslint --fix 'src/**/*.ts'",
"pre-commit": "lint-staged --cwd .",
"dev": "docker-compose -f ../../docker-compose.dev.yml up --build",
"down": "docker-compose -f ../../docker-compose.dev.yml down"
"dev": "docker compose -f ../../docker-compose.dev.yml up --build",
"down": "docker compose -f ../../docker-compose.dev.yml down"
},
"dependencies": {
"@sentry/node": "^8.9.2",
"@sentry/node": "^8.26.0",
"@toruslabs/config": "^2.2.0",
"@toruslabs/constants": "^14.0.0",
"@toruslabs/fnd-base": "^14.0.0",
"@toruslabs/loglevel-sentry": "^7.0.0",
"@toruslabs/loglevel-sentry": "^7.0.1",
"celebrate": "^15.0.3",
"compression": "^1.7.4",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"helmet": "^7.1.0",
"loglevel": "^1.9.1",
"morgan": "^1.10.0"
},
"devDependencies": {
"@toruslabs/eslint-config-node": "^3.3.1",
"@toruslabs/eslint-config-node": "^3.3.3",
"@types/compression": "^1.7.5",
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/morgan": "^1.9.9",
"assert": "^2.1.0",
"cross-env": "^7.0.3",
"nodemon": "^3.1.3",
"rimraf": "^5.0.7",
"nodemon": "^3.1.4",
"rimraf": "^6.0.1",
"wait-port": "^1.1.0"
},
"lint-staged": {
Expand Down
4 changes: 2 additions & 2 deletions packages/fnd-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import dotenv from "dotenv";
import express from "express";
import helmet from "helmet";
import { createServer } from "http";
import log from "loglevel";
import log, { levels, LogLevelDesc } from "loglevel";
import morgan from "morgan";

// Setup environment
dotenv.config({
path: process.env.NODE_ENV === "development" ? ".env.development" : ".env",
});
log.setLevel((process.env.LOG_LEVEL as log.LogLevelDesc) || "DEBUG");
log.setLevel((process.env.LOG_LEVEL as LogLevelDesc) || levels.INFO);

import router from "./router";
import { traceContextMiddleware } from "./utils/traceContext";
Expand Down
5 changes: 1 addition & 4 deletions packages/fnd-server/src/utils/instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@ if (sentryDsn) {
dsn: sentryDsn,
environment: process.env.NODE_ENV,
integrations: [
// enable HTTP calls
Sentry.httpIntegration({ breadcrumbs: true }),

// application not responding detection.
Sentry.anrIntegration({ captureStackTrace: true }),
],
tracesSampleRate: process.env.SENTRY_TRACES_SAMPLE_RATE ? Number(process.env.SENTRY_TRACES_SAMPLE_RATE) : 0.01,
sampleRate: process.env.SENTRY_SAMPLE_RATE ? Number(process.env.SENTRY_SAMPLE_RATE) : 0.1,
sampleRate: process.env.SENTRY_SAMPLE_RATE ? Number(process.env.SENTRY_SAMPLE_RATE) : 1,
beforeSend(event) {
return redact(event);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/fnd-server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"extends": "@toruslabs/config/tsconfig.default.json",
"include": ["src"],
"compilerOptions": {
"moduleResolution": "Node16",
Expand Down

0 comments on commit 7612076

Please sign in to comment.