Skip to content

Commit

Permalink
Refactor codebase and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejkang committed Jul 28, 2024
1 parent 04acfe5 commit 378c20a
Show file tree
Hide file tree
Showing 48 changed files with 3,828 additions and 9,878 deletions.
6 changes: 6 additions & 0 deletions .docker.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SSH_HOST=${SSH_HOST}
SSH_PORT=${SSH_PORT}
SSH_USERNAME=${SSH_USERNAME}
SSH_PASSWORD=${SSH_PASSWORD}
DATABASE_HOST=${DATABASE_HOST}
DATABASE_PORT=${DATABASE_PORT}
10 changes: 3 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# MySQL
MYSQL_HOST=${MYSQL_HOST}
MYSQL_PORT=${MYSQL_PORT}
MYSQL_USER=${MYSQL_USER}
MYSQL_PASSWORD=${MYSQL_PASSWORD}
MYSQL_DATABASE=${MYSQL_DATABASE}

# JWT
JWT_SECRET=${JWT_SECRET}

# Secret
AES_SECRET=${AES_SECRET}
12 changes: 7 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
'.eslintrc.js',
"src/**/*.spec.ts",
"tests/**/*.ts",
"src/**/*Controller.ts",
],
rules: {
'init-declarations': 'off',
Expand All @@ -33,9 +34,9 @@ module.exports = {
'quotes': 'off',
'brace-style': 'off',
'@typescript-eslint/dot-notation': 'error',
'@typescript-eslint/explicit-function-return-type': 'warn',
"@typescript-eslint/init-declarations": "error",
"@typescript-eslint/no-empty-interface": "warn",
'@typescript-eslint/explicit-function-return-type': 'error',
"@typescript-eslint/init-declarations": "warn",
"@typescript-eslint/no-empty-interface": "off",
'@typescript-eslint/member-delimiter-style': [
'error',
{
Expand Down Expand Up @@ -123,17 +124,18 @@ module.exports = {
'allowGenerics': 'always',
}
],
'@typescript-eslint/no-unnecessary-condition': 'error',
'@typescript-eslint/no-unnecessary-condition': 'warn',
'@typescript-eslint/no-unnecessary-type-arguments': 'error',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-useless-empty-export': 'error',
'@typescript-eslint/no-useless-constructor': 'off',
'@typescript-eslint/non-nullable-type-assertion-style': 'error',
'@typescript-eslint/prefer-for-of': 'error',
'@typescript-eslint/prefer-function-type': 'off',
'@typescript-eslint/prefer-includes': 'error',
'@typescript-eslint/prefer-literal-enum-member': 'error',
'@typescript-eslint/prefer-nullish-coalescing': 'off',
'@typescript-eslint/prefer-optional-chain': 'error',
'@typescript-eslint/prefer-optional-chain': 'warn',
'@typescript-eslint/prefer-reduce-type-parameter': 'error',
'@typescript-eslint/prefer-return-this-type': 'error',
'@typescript-eslint/prefer-string-starts-ends-with': 'off',
Expand Down
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
* linguist-vendored
src/* linguist-vendored=false
scheme/* linguist-vendored=false
.github/* linguist-vendored=false
76 changes: 76 additions & 0 deletions .github/workflows/deployment-development.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
name: Development Deployment

on:
push:
branches:
- deploy/dev

jobs:
deployment:
name: Deployment
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Set tag
id: vars
run: echo "rev_sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
mask-password: 'false'

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: api-ecr
IMAGE_TAG: ${{ steps.vars.outputs.rev_sha }}
NODE_ENV: development
JWT_SECRET: ${{ secrets.JWT_SECRET }}
AES_SECRET: ${{ secrets.AES_SECRET }}

run: |
cat .env.example | envsubst > .env
cat .env
ls -al
printenv
echo $ECR_REGISTRY
echo $ECR_REPOSITORY
echo $IMAGE_TAG
docker build --build-arg ENV=development -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile.development .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> "$GITHUB_OUTPUT"
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition 'api-service' --query taskDefinition > task-definition.json
echo $(jq '.cpu = $CPU_VALUE' --arg CPU_VALUE 1024 task-definition.json) > task-definition.json
echo $(jq '.memory = $MEMORY_VALUE' --arg MEMORY_VALUE 2048 task-definition.json) > task-definition.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: api-application
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: api-service
cluster: api-cluster
wait-for-service-stability: true
76 changes: 76 additions & 0 deletions .github/workflows/deployment-production.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
name: Production Deployment

on:
push:
branches:
- deploy/prod

jobs:
deployment:
name: Deployment
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Set tag
id: vars
run: echo "rev_sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
with:
mask-password: 'false'

- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: api-ecr
IMAGE_TAG: ${{ steps.vars.outputs.rev_sha }}
NODE_ENV: production
JWT_SECRET: ${{ secrets.JWT_SECRET }}
AES_SECRET: ${{ secrets.AES_SECRET }}

run: |
cat .env.example | envsubst > .env
cat .env
ls -al
printenv
echo $ECR_REGISTRY
echo $ECR_REPOSITORY
echo $IMAGE_TAG
docker build --build-arg ENV=production -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -f Dockerfile .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> "$GITHUB_OUTPUT"
- name: Download task definition
run: |
aws ecs describe-task-definition --task-definition 'api-service' --query taskDefinition > task-definition.json
echo $(jq '.cpu = $CPU_VALUE' --arg CPU_VALUE 1024 task-definition.json) > task-definition.json
echo $(jq '.memory = $MEMORY_VALUE' --arg MEMORY_VALUE 2048 task-definition.json) > task-definition.json
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: api-application
image: ${{ steps.build-image.outputs.image }}

- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: api-service
cluster: api-cluster
wait-for-service-stability: true
43 changes: 43 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Integration

on:
workflow_dispatch:
pull_request:
push:
branches:
- main
- dev

jobs:
integration:
name: Integration
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v4
with:
node-version: 20.9.0

- name: Install packages
run: |
npm install
- name: Run lint
run: |
npm run lint:check
- name: Run unit tests with coverage
run: |
npm run test:cov
- name: Run build
run: |
npm run build
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ data
# Environment variables
.env*
!.env.example
.docker.env*
!.docker.env.example
12 changes: 12 additions & 0 deletions .run/Local.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Local" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
<deployment type="docker-compose.yml">
<settings>
<option name="envFilePath" value="" />
<option name="sourceFilePath" value="docker-compose.local.yaml" />
<option name="upDetach" value="false" />
</settings>
</deployment>
<method v="2" />
</configuration>
</component>
2 changes: 1 addition & 1 deletion .run/Test All.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test All" type="JavaScriptTestRunnerJest">
<node-interpreter value="$USER_HOME$/.nvm/versions/node/v18.17.1/bin/node" />
<node-interpreter value="project" />
<jest-package value="$PROJECT_DIR$/node_modules/jest" />
<working-dir value="$PROJECT_DIR$" />
<envs />
Expand Down
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.env ./
RUN apk update && \
apk add --no-cache chromium fontconfig font-noto-cjk && \
fc-cache -f -v
ENV CHROMIUM_LINUX_PATH /usr/bin/chromium-browser
ENV NODE_ENV="production"
CMD ["node", "dist/main"]

Expand Down
26 changes: 26 additions & 0 deletions Dockerfile.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
FROM node:20-alpine AS deps
WORKDIR /app
COPY ["package.json", "package-lock.json", "./"]
RUN ["npm", "install"]

FROM node:20-alpine AS builder
WORKDIR /app
COPY ["tsconfig.build.json", "tsconfig.json", "./"]
COPY ["src/", "./src/"]
COPY [".env", "./"]
COPY --from=deps /app/node_modules ./node_modules
RUN ["npx", "nest", "build"]

FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.env ./
RUN apk update && \
apk add --no-cache chromium fontconfig font-noto-cjk && \
fc-cache -f -v
ENV CHROMIUM_LINUX_PATH /usr/bin/chromium-browser
ENV NODE_ENV="development"
CMD ["node", "dist/main"]

EXPOSE 80
36 changes: 36 additions & 0 deletions docker-compose.development.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
services:
server:
build:
dockerfile: Dockerfile.local
context: ./
container_name: api
restart: unless-stopped
depends_on:
- sshtunnel
ports:
- '80:80'
volumes:
- ./src:/app/src
command:
- "npm"
- "run"
- "start:local:dev"

sshtunnel:
container_name: sshtunnel
image: jossec101/sshtunneller
expose:
- 25001 # database
environment:
- "ssh_host=${SSH_HOST}"
- "ssh_port=${SSH_PORT}"
- "ssh_username=${SSH_USERNAME}"
- "ssh_password=${SSH_PASSWORD}"
- "remote_bind_addresses=[('${DATABASE_HOST}', ${DATABASE_PORT})]"
- "local_bind_addresses=[('0.0.0.0', 25001)]"
logging:
driver: "json-file"
options:
max-size: "20m"
max-file: "3"
restart: unless-stopped
6 changes: 1 addition & 5 deletions docker-compose.local.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: '3.8'

services:
server:
build:
Expand All @@ -13,12 +11,10 @@ services:
- '80:80'
volumes:
- ./src:/app/src
- ./.env:/app/.env
- ./.env.development:/app/.env.development
command:
- "npm"
- "run"
- "start:local:dev"
- "start:local"

mysql:
container_name: api_mysql
Expand Down
Loading

0 comments on commit 378c20a

Please sign in to comment.