Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API #31

Merged
merged 20 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.github
cache
dist
docker-compose*
.env*
Dockerfile
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
REVERSE_PROXY_PORT=8000
REVERSE_PROXY_UI_PORT=8080
CFG_VERSION=latest
CGW_VERSION=latest
TXS_VERSION=latest
UI_VERSION=latest
RPC_NODE_URL=http://host.docker.internal:8545
ETHEREUM_NODE_URL=http://host.docker.internal:8545
237 changes: 237 additions & 0 deletions .github/workflows/api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
name: API CI

on:
push:

jobs:
buildAndTest:
name: CI Pipeline
runs-on: ubuntu-latest
strategy:
matrix:
node-version: ['18'] # Add other versions if needed

steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1

- name: Install Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Restore Node.js dependencies
uses: actions/cache@v3
with:
path: ./node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-node-

- name: Install Node.js dependencies
run: yarn install --frozen-lockfile

- name: Cache Node.js dependencies
uses: actions/cache/save@v3
if: always()
with:
path: ./node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}

- name: Start Hardhat RPC
run: |
yarn hardhat > /dev/null &
yarn hardhat-deploy

- name: Start Safe Transaction Service
run: |
docker compose --env-file=.env.example -f docker-compose.safe.yml up -d --remove-orphans
docker compose exec txs-web python manage.py insert_safe_master_copy --address "0xd916a690676e925Ac9Faf2d01869c13Fd9757ef2"
sudo chmod -R a+rwx ./docker/data

- name: Run Tests
env:
NODE_OPTIONS: --max-old-space-size=8192
run: |
docker compose -f docker-compose.yml -f docker-compose.api.yml run \
-e AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY_ID }} \
-e AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} \
-e NODE_OPTIONS='--max-old-space-size=8192' \
-T api \
npx nx run api:test --verbose

# - name: SonarCloud Scan
# uses: SonarSource/sonarcloud-github-action@master
# env:
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
# with:
# projectBaseDir: apps/api

bumpVersion:
name: 'Bump Version on develop'
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop'
needs: buildAndTest
outputs:
newTag: ${{ steps.version-bump.outputs.newTag }}

steps:
- name: 'Checkout source code'
uses: 'actions/checkout@v2'
with:
ref: ${{ github.ref }}

- name: 'Automated Version Bump'
id: version-bump
uses: 'phips28/gh-action-bump-version@master'
with:
tag-prefix: 'v'
tag-suffix: '-api'
commit-message: 'CI: bumps version to {{version}}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PACKAGEJSON_DIR: 'apps/api'

buildAndPushImage:
name: Build and Push docker image
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop'
needs: bumpVersion
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-3

- name: Login to ECR
uses: docker/login-action@v1
with:
registry: 275440070213.dkr.ecr.eu-west-3.amazonaws.com

- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: |
275440070213.dkr.ecr.eu-west-3.amazonaws.com/api
tags: |
type=ref,event=branch
type=sha
type=semver,pattern={{version}},value=${{needs.bumpVersion.outputs.newTag}}
type=semver,pattern={{major}}.{{minor}},value=${{needs.bumpVersion.outputs.newTag}}
type=semver,pattern={{raw}},value=${{needs.bumpVersion.outputs.newTag}}

- name: Set correct version
run: npm version ${{needs.bumpVersion.outputs.newTag}} --allow-same-version=true --git-tag-version=false
working-directory: ./apps/api

- name: Build
uses: docker/build-push-action@v2
with:
context: .
file: apps/api/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

buildAndPushHotfixImage:
name: Build and Push hotfix docker image
runs-on: ubuntu-latest
if: startsWith(github.ref,'refs/heads/hotfix/')
needs: buildAndTest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-3

- name: Login to ECR
uses: docker/login-action@v1
with:
registry: 275440070213.dkr.ecr.eu-west-3.amazonaws.com

- name: Docker meta
id: meta
uses: docker/metadata-action@v3
with:
images: |
275440070213.dkr.ecr.eu-west-3.amazonaws.com/api
tags: |
type=ref,event=branch
type=sha

- name: Build
uses: docker/build-push-action@v2
with:
context: .
file: apps/api/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

autodeploy:
name: Auto deploy develop to dev.api.thx.network
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop'
needs: [buildAndPushImage, bumpVersion]
steps:
- name: Install Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}

- name: Install deploy-scripts
run: npm install -g thxprotocol/deploy-scripts

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: eu-west-3

- name: Deploy-script
run: thx-deploy ApiDev sha-$(echo ${{github.sha}} | cut -c1-7)

discord:
name: Update Discord
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/develop'
needs: [autodeploy, bumpVersion]
steps:
- name: Send message
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_AWS_WEBHOOK }}
uses: Ilshidur/action-discord@master
with:
args: "${{ needs.autodeploy.result == 'success' && '✅' || '⛔' }} Released APIDev `${{ needs.bumpVersion.outputs.newTag }}`"
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ Thumbs.db
!.env.example
certs/*

.nx/cache
.nx/cache

# Docker
docker/data/**/*
41 changes: 41 additions & 0 deletions apps/api/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
MONGODB_URI="mongodb://root:root@localhost:27017/api?authSource=admin&ssl=false"
MONGODB_URI_TEST_OVERRIDE="mongodb://root:root@localhost:27017/api_test?authSource=admin&ssl=false"
MONGODB_NAME="api"
MONGODB_USER="root"
MONGODB_PASSWORD="root"
AUTH_URL="https://local.auth.thx.network"
API_URL="https://localhost:3001"
DASHBOARD_URL="https://localhost:8082"
WALLET_URL="https://localhost:8083"
WIDGET_URL="https://localhost:8080"
HARDHAT_RPC="https://localhost:8547"
HARDHAT_RPC_TEST_OVERRIDE="http://127.0.0.1:8545"
POLYGON_RPC=""
ETHEREUM_RPC=""
PRIVATE_KEY="0x873c254263b17925b686f971d7724267710895f1585bb0533db8e693a2af32ff"
INITIAL_ACCESS_TOKEN="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
PORT="3000"
AUTH_CLIENT_ID="ISsh6Xw6wDISihJS2xs2_"
AUTH_CLIENT_SECRET="BnQrAHSb4UDnpR-9klfFAQbZvq_RjVZgLTKwRD3qfOjawX21jrnbBxvSOU84EwqAy1J-fNKvxD2qZen5Gm8jXg"
INFURA_PROJECT_ID=""
INFURA_IPFS_PROJECT_ID=""
INFURA_IPFS_PROJECT_SECRET=""
RATE_LIMIT_REWARD_GIVE="100"
RATE_LIMIT_REWARD_GIVE_WINDOW="900"
MAX_FEE_PER_GAS="400"
POLYGON_NAME="maticdev"
POLYGON_MUMBAI_NAME="mumbaidev"
SAFE_TXS_SERVICE="http://localhost:8000/txs"
HARDHAT_NAME="hardhat"
AWS_ACCESS_KEY_ID=""
AWS_SECRET_ACCESS_KEY=""
AWS_S3_PUBLIC_BUCKET_NAME="local-thx-storage-bucket"
AWS_S3_PUBLIC_BUCKET_REGION="eu-west-3"
AWS_S3_PRIVATE_BUCKET_NAME="local-thx-private-storage-bucket"
AWS_S3_PRIVATE_BUCKET_REGION="eu-west-3"
POLYGON_RELAYER=""
POLYGON_RELAYER_API_KEY=""
POLYGON_RELAYER_API_SECRET=""
LOCAL_CERT="../../certs/localhost.crt"
LOCAL_CERT_KEY="../../certs/localhost.key"
CWD="/usr/src/app/apps/api/src/"
18 changes: 18 additions & 0 deletions apps/api/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
60 changes: 60 additions & 0 deletions apps/api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#####################################################################################################
## Develop stage
#####################################################################################################
FROM node:18-slim AS develop

WORKDIR /usr/src/app

ENV NODE_OPTIONS="--max_old_space_size=8192"

RUN apt-get update \
&& apt-get install -y g++ make python3-pip build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

COPY package.json yarn.lock ./
RUN yarn
COPY . .

CMD [ "npx", "nx", "serve", "api" ]

#####################################################################################################
## Build stage
#####################################################################################################
FROM node:18-slim AS build

ENV NODE_ENV=production

WORKDIR /usr/src/app
COPY --from=develop ./usr/src/app/ ./
RUN npx nx build api --prod
COPY ./newrelic.js ./yarn.lock ./dist/apps/api/
COPY ./libs/contracts/exports ./dist/apps/api/libs/contracts/exports

#####################################################################################################
## Production stage
#####################################################################################################
FROM node:18-slim AS production

ENV NODE_ENV=production

WORKDIR /usr/src/app
COPY --from=build ./usr/src/app/dist/apps/api/package.json ./usr/src/app/dist/apps/api/yarn.lock ./

# Install dependencies and packages
RUN apt-get update \
&& apt-get install -y g++ make python3-pip build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

# Install your application dependencies (assuming it uses Node.js)
RUN yarn

# Clean up unnecessary packages and files
RUN apt-get purge -y --auto-remove build-essential && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

COPY --from=build ./usr/src/app/dist/apps/api ./

CMD [ "main.js" ]
Loading
Loading