diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..6a7ef44 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +**/.husky +**/.idea +**/.vscode +**/node_modules +**/.env +.dockerignore +**/.gitignore +**/*.env +**/.git +**/.gitmodules +**/.github +**/Dockerfile +**/docker-compose.yml \ No newline at end of file diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml new file mode 100644 index 0000000..c151626 --- /dev/null +++ b/.github/workflows/build_and_deploy.yml @@ -0,0 +1,48 @@ +#Location: .github/workflows/custom_config.yml + +name: Build and push containers to Github Container Registry and apply rolling update +on: + push: + branches: + - develop + +jobs: + build: + runs-on: ubuntu-latest + permissions: + packages: write + contents: read + steps: + - uses: actions/checkout@v4 + with: + submodules: "recursive" + + - name: Getting image tag + uses: actions/github-script@v6 + id: get_image_tag + with: + result-encoding: string + script: | + return context.payload.ref.replace(/.*\//, ''); + + - name: Create and push docker image + run: | + # login to ghcr.io + docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} + + # build with full metadata + docker build \ + --label "org.opencontainers.image.source=https://github.com/${GITHUB_REPOSITORY}" \ + --label "org.opencontainers.image.description=Deposit payment example dockerized app" \ + --label "org.opencontainers.image.licenses=MIT" \ + -t ghcr.io/golemfactory/deposit_example:${{ steps.get_image_tag.outputs.result }} \ + --build-arg BACKEND_URL=https://deposit.dev.golem.network \ + . + + # push one image with two tags into repository + docker push --all-tags ghcr.io/golemfactory/deposit_example + + - name: Notify Develop Branch + continue-on-error: true + if: ${{ steps.get_image_tag.outputs.result == 'develop' }} + run: curl http://deposit.dev.golem.network:5000/release/pull diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..f01233c --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +name: Build docker files + +on: + push: + +permissions: + packages: write + contents: write + +jobs: + + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + submodules: 'recursive' + + - name: Create and push docker image + run: | + # login to ghcr.io + docker login ghcr.io -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }} + + # build with full metadata + docker build \ + --label "org.opencontainers.image.source=https://github.com/${GITHUB_REPOSITORY}" \ + --label "org.opencontainers.image.description=Deposit payment example dockerized app" \ + --label "org.opencontainers.image.licenses=MIT" \ + -t ghcr.io/golemfactory/deposit_example:latest \ + --build-arg BACKEND_URL=http://deposit.dev.golem.network:5174 \ + . + + # push one image with two tags into repository + docker push --all-tags ghcr.io/golemfactory/deposit_example + + - name: Notify Develop Server + continue-on-error: true + if: ${{ steps.get_image_tag.outputs.result == 'develop' }} + run: curl http://deposit.dev.golem.network.:5000/release/pull diff --git a/.gitignore b/.gitignore index ea8b570..6b12b77 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ node_modules dist backend/.env.local temp +.env \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index 5bfa6b8..a16a3e3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,13 +1,13 @@ [submodule "golem-js"] path = golem-js - url = git@github.com:golemfactory/golem-js.git + url = https://github.com/golemfactory/golem-js.git branch = pociej/deposit [submodule "ya-ts-client"] path = ya-ts-client - url = git@github.com:golemfactory/ya-ts-client.git + url = https://github.com/golemfactory/ya-ts-client.git branch = pociej/deposit [submodule "golem-sdk-task-executor"] path = golem-sdk-task-executor - url = git@github.com:golemfactory/golem-sdk-task-executor.git + url = https://github.com/golemfactory/golem-sdk-task-executor.git branch = pociej/deposit diff --git a/.husky/commit-msg b/.husky/commit-msg deleted file mode 100755 index 1d46fee..0000000 --- a/.husky/commit-msg +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/sh -. "$(dirname "$0")/_/husky.sh" - -print_error() { - echo -e "\033[0;31m$1\033[0m" # \033[0;31m sets text color to red, \033[0m resets color -} - -commit_msg_file=".git/COMMIT_EDITMSG" - -# Check if the commit message file exists -if [ -e "$commit_msg_file" ]; then - commit_message=$(cat "$commit_msg_file") - - # Use $commit_message as needed - echo "Commit message: $commit_message" - - # Your logic here, for example, running check-for-issue-reference - node ./check-for-issue-reference "$commit_message" || { - echo "husky: commit-msg hook failed (add --no-verify to bypass)" && exit 1 - } - - npx commitlint --edit "$commit_msg_file" -else - echo "Error: Commit message file not found: $commit_msg_file" - exit 1 -fi - -npx --no -- commitlint --edit "$1" diff --git a/.husky/pre-commit b/.husky/pre-commit deleted file mode 100644 index 605953f..0000000 --- a/.husky/pre-commit +++ /dev/null @@ -1 +0,0 @@ -. "$(dirname -- "$0")/_/husky.sh" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8dbd734 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +ARG BACKEND_URL=http://127.0.0.1:5174 + +FROM node:20-bullseye + +RUN apt-get update && apt-get install -y vim + +RUN npm install -g pnpm serve pm2 +# Set the working directory +WORKDIR /app +COPY . /app + +ARG BACKEND_URL +ENV VITE_BACKEND_URL=$BACKEND_URL +RUN echo "The backend URL is $BACKEND_URL" +RUN mkdir /app/temp + +RUN pnpm install +RUN pnpm build:all diff --git a/backend/package.json b/backend/package.json index 147a14d..60a3791 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,11 +1,12 @@ { - "name": "backend", + "name": "deposit:backend", "version": "1.0.0", "description": "", "main": "index.ts", "scripts": { "dev": "concurrently -k \"tsc -w\" \"nodemon --env-file=.env.local ./dist/index.js\"", - "build": "tsc" + "build": "tsc", + "start": "pm2 start ./dist/index.js" }, "type": "module", "keywords": [], diff --git a/backend/src/di.ts b/backend/src/di.ts index ca4b808..e76111e 100644 --- a/backend/src/di.ts +++ b/backend/src/di.ts @@ -55,7 +55,9 @@ container.register({ container.register({ db: awilix .asFunction((connectionString) => { - return mongoose.connect(connectionString); + return mongoose.connect(connectionString, { + dbName: process.env.DB_NAME, + }); }) .singleton(), }); diff --git a/backend/src/services/user/service.ts b/backend/src/services/user/service.ts index 82b43ab..eb2e6a0 100644 --- a/backend/src/services/user/service.ts +++ b/backend/src/services/user/service.ts @@ -1,6 +1,7 @@ import { IUserService, IUser } from "./types.js"; import { userModel } from "./model.js"; import { v4 as uuidv4 } from "uuid"; +import mongoose from "mongoose"; const randomNonce = () => { return Math.floor(Math.random() * 10000000); @@ -9,6 +10,11 @@ const randomNonce = () => { export const userService: IUserService = { registerUser: async (walletAddress: string) => { const user = await userModel.findOne({ walletAddress }); + console.log("#########################"); + console.log("registering user"); + console.log("model", mongoose.connection); + console.log("user", user); + console.log("#########################"); if (user !== null) { return user; } @@ -17,6 +23,7 @@ export const userService: IUserService = { walletAddress, nonce: randomNonce(), id: uuidv4(), + deposits: [], }, { _id: true, diff --git a/deploy/docker-compose.yml b/deploy/docker-compose.yml new file mode 100644 index 0000000..277c222 --- /dev/null +++ b/deploy/docker-compose.yml @@ -0,0 +1,84 @@ +services: + # application service + backend: + image: ghcr.io/golemfactory/deposit_example:${DEPLOY_BRANCH:-latest} + command: pnpm run start + working_dir: /app/backend + environment: + - YAGNA_APPKEY=66667777888 + - YAGNA_API_URL=http://yagna:7465 + - JWT_SECRET=dup]a + - JWT_TOKEN_EXPIRATION=1d + - JWT_REFRESH_TOKEN_EXPIRATION=7d + - JWT_ISSUER=golem.network + - MONGO_URI=mongodb://${MONGO_ROOT_USER}:${MONGO_ROOT_PASSWORD}@mongo:27017/ + - HOST=0.0.0.0 + - PORT=5174 + - DB_NAME=depositDB + - DEPOSIT_CONTRACT_ADDRESS=0xb9919c8D8D384d93C195503064A3b303Ea8Fdbaa + ports: + - "5174:5174" + depends_on: + - yagna + - mongo + + frontend: + image: ghcr.io/golemfactory/deposit_example:${DEPLOY_BRANCH:-latest} + command: pnpm run start + working_dir: /app/frontend + environment: + - YAGNA_APPKEY=66667777888 + - YAGNA_API_URL=http://yagna:7465 + - JWT_SECRET=dup]a + - JWT_TOKEN_EXPIRATION=1d + - JWT_REFRESH_TOKEN_EXPIRATION=7d + - JWT_ISSUER=golem.network + - MONGO_URI=mongodb://${MONGO_ROOT_USER}:${MONGO_ROOT_PASSWORD}@mongo:27017/ + - HOST=0.0.0.0 + - PORT=5174 + - DB_NAME=depositDB + - DEPOSIT_CONTRACT_ADDRESS=0xb9919c8D8D384d93C195503064A3b303Ea8Fdbaa + ports: + - "3000:3000" + depends_on: + - backend + # yagna service + yagna: + build: + context: ../yagna + dockerfile: Dockerfile + args: + - YAGNA_VERSION=pre-rel-v0.15.0-deposits-beta1 + command: yagna service run + environment: + - YAGNA_AUTOCONF_APPKEY=66667777888 + - YAGNA_AUTOCONF_ID_SECRET=0000000000000000000000000000000000000000000000000000000222222222 + - YAGNA_API_URL=http://0.0.0.0:7465 + - SUBNET=change_me + - YA_NET_BIND_URL=udp://0.0.0.0:0 + # mongo service + mongo: + image: mongo + environment: + - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER} + - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD} + - MONGO_INITDB_DATABASE=project + ports: + - "27017:27017" + + # mongo express service + mongo-express: + image: mongo-express + environment: + - ME_CONFIG_MONGODB_SERVER=mongo + - ME_CONFIG_MONGODB_PORT=27017 + - ME_CONFIG_MONGODB_ENABLE_ADMIN=false + - ME_CONFIG_MONGODB_AUTH_DATABASE=admin + - ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER} + - ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD} + - ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN} + - ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD} + depends_on: + - mongo + ports: + - "8888:8081" \ No newline at end of file diff --git a/deploy/npm_server.js b/deploy/npm_server.js new file mode 100644 index 0000000..d077eb8 --- /dev/null +++ b/deploy/npm_server.js @@ -0,0 +1,18 @@ +const express = require('express'); +const { spawn } = require('child_process'); + +const app = express(); + +app.get('/release/pull', (req, res) => { + const deployProcess = spawn('/bin/bash', ['-c', './script.sh'], + { + cwd: process.cwd(), + stdio: "inherit" + } + ); + res.send('Hello World'); +}); + +const server = app.listen(5000, '0.0.0.0', () => { + console.log('Server running'); +}); diff --git a/deploy/package.json b/deploy/package.json new file mode 100644 index 0000000..7afc1b9 --- /dev/null +++ b/deploy/package.json @@ -0,0 +1,10 @@ +{ + "name": "deposit-payments-deploy-updater", + "version": "1.0.0", + "dependencies": { + "express": "^4.17.1" + }, + "scripts": { + "start": "node npm_server.js" + } +} \ No newline at end of file diff --git a/deploy/script.sh b/deploy/script.sh new file mode 100755 index 0000000..8569bc9 --- /dev/null +++ b/deploy/script.sh @@ -0,0 +1,8 @@ +for i in {1..10} +do + git pull + docker-compose pull + # docker-compose config + docker-compose up -d + sleep 10 +done diff --git a/docker-compose-dev.yml b/docker-compose-dev.yml new file mode 100644 index 0000000..4345ab8 --- /dev/null +++ b/docker-compose-dev.yml @@ -0,0 +1,61 @@ +services: + # application service + app: + build: + context: . + dockerfile: Dockerfile + args: + - BACKEND_URL=${BACKEND_URL:-http://127.0.0.1:5174} + command: pnpm run start:app + environment: + - YAGNA_APPKEY=66667777888 + - YAGNA_API_URL=http://yagna:7465 + - JWT_SECRET=dup]a + - JWT_TOKEN_EXPIRATION=1d + - JWT_REFRESH_TOKEN_EXPIRATION=7d + - JWT_ISSUER=golem.network + - MONGO_URI=mongodb://${MONGO_ROOT_USER}:${MONGO_ROOT_PASSWORD}@mongo:27017/ + - HOST=0.0.0.0 + - PORT=5174 + - DB_NAME=depositDB + - DEPOSIT_CONTRACT_ADDRESS=0xb9919c8D8D384d93C195503064A3b303Ea8Fdbaa + ports: + - "3000:3000" + - "5174:5174" + # yagna service + yagna: + build: + context: ./yagna + dockerfile: Dockerfile + args: + - YAGNA_VERSION=pre-rel-v0.15.0-deposits-beta1 + command: yagna service run + environment: + - YAGNA_AUTOCONF_APPKEY=66667777888 + - YAGNA_AUTOCONF_ID_SECRET=0000000000000000000000000000000000000000000000000000000222222222 + - YAGNA_API_URL=http://0.0.0.0:7465 + - SUBNET=change_me + - YA_NET_BIND_URL=udp://0.0.0.0:0 + # mongo service + mongo: + image: mongo + environment: + - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER} + - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD} + - MONGO_INITDB_DATABASE=project + # mongo express service + mongo-express: + image: mongo-express + environment: + - ME_CONFIG_MONGODB_SERVER=mongo + - ME_CONFIG_MONGODB_PORT=27017 + - ME_CONFIG_MONGODB_ENABLE_ADMIN=false + - ME_CONFIG_MONGODB_AUTH_DATABASE=admin + - ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER} + - ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD} + - ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN} + - ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD} + depends_on: + - mongo + ports: + - "8888:8081" \ No newline at end of file diff --git a/frontend/package.json b/frontend/package.json index 170a67b..347978c 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -5,6 +5,7 @@ "scripts": { "dev": "vite", "build": "tsc && vite build", + "start": "serve dist -l tcp://0.0.0.0:3000", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview" }, diff --git a/frontend/src/Uploader.tsx b/frontend/src/Uploader.tsx index 0faabda..8def55b 100644 --- a/frontend/src/Uploader.tsx +++ b/frontend/src/Uploader.tsx @@ -51,7 +51,7 @@ export const FileUploader = () => { // }} > - Drop files here or{" "} + Drag & Drop files here or{" "}