From 519d44ae176d0c6c2b1f1390b8df5fbc05dc8b0c Mon Sep 17 00:00:00 2001 From: Michael Neale Date: Fri, 10 Nov 2023 09:21:02 +1100 Subject: [PATCH] add CI (#3) * trying out some CI * adding postgres * already have postgres running * updating postgres setup for mock * install dbmate * trying defaults life * add loading of did of server for test * wait for server to amp up * Update src/main.ts Co-authored-by: Moe Jangda * move server-did to config * typo * tidy up env * cleaning up a bit more --------- Co-authored-by: Moe Jangda --- .github/workflows/ci.yml | 57 ++++++++++++++++++++++++++++++++++++++++ .gitignore | 3 ++- src/client-test.ts | 5 +++- src/config.ts | 3 +++ src/main.ts | 3 +-- 5 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..92d64c5 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,57 @@ +name: Node.js CI with PostgreSQL + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build_and_test: + runs-on: ubuntu-latest + services: + postgres: + image: postgres:latest + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: tbd + POSTGRES_DB: mockpfi + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + strategy: + matrix: + node-version: ['20.4.0'] + + steps: + - uses: actions/checkout@v3 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v3 + with: + node-version: ${{ matrix.node-version }} + - name: Install dependencies + run: npm install + - name: Install dbmate + run: | + curl -fsSL https://github.com/amacneil/dbmate/releases/download/v1.12.1/dbmate-linux-amd64 -o dbmate + chmod +x dbmate + sudo mv dbmate /usr/local/bin + - name: Run migration scripts + run: | + ./db/scripts/migrate + - name: Seed offerings + run: npm run seed-offerings + - name: Run server and tests + run: | + npm run server & + SERVER_PID=$! + sleep 5 + npm run test + kill $SERVER_PID diff --git a/.gitignore b/.gitignore index 871f375..74912f5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ node_modules/ .env .DS_STORE alice.json -issuer.json \ No newline at end of file +issuer.json +server-did.txt \ No newline at end of file diff --git a/src/client-test.ts b/src/client-test.ts index db07f98..ea3be4a 100644 --- a/src/client-test.ts +++ b/src/client-test.ts @@ -6,7 +6,10 @@ import fs from 'fs/promises' // Replace this with the DID of the PFI you want to connect to. // Get this from the console of the server once you launch it. // -const PFI_DID = 'did:ion:EiDgs_F6pK672qmdJP2n8-LY4MjhPSehDl1I6y1nfrSW3g:eyJkZWx0YSI6eyJwYXRjaGVzIjpbeyJhY3Rpb24iOiJyZXBsYWNlIiwiZG9jdW1lbnQiOnsicHVibGljS2V5cyI6W3siaWQiOiJkd24tc2lnIiwicHVibGljS2V5SndrIjp7ImNydiI6IkVkMjU1MTkiLCJrdHkiOiJPS1AiLCJ4IjoiNnpYZ3VBZW1XZHBDYVVkYjE2Rjl4czFmU3JBUEZ2MmZlcFE0SGJoM2QxNCJ9LCJwdXJwb3NlcyI6WyJhdXRoZW50aWNhdGlvbiJdLCJ0eXBlIjoiSnNvbldlYktleTIwMjAifV0sInNlcnZpY2VzIjpbeyJpZCI6InBmaSIsInNlcnZpY2VFbmRwb2ludCI6Imh0dHA6Ly9sb2NhbGhvc3Q6OTAwMCIsInR5cGUiOiJQRkkifV19fV0sInVwZGF0ZUNvbW1pdG1lbnQiOiJFaUJOV21uNlV1c3hrczBTalhPTzBSUUZVbnR3dGxzczFjLVlBTFpJQ01BQUNBIn0sInN1ZmZpeERhdGEiOnsiZGVsdGFIYXNoIjoiRWlEOU50cWVXbzNMOXNJdHBRRHQzazlIaEVpcDhZMk9UZk00QWJCTFNXeWpmZyIsInJlY292ZXJ5Q29tbWl0bWVudCI6IkVpQUxqei1rNWtaZlBmQTJEWUcwY2V1Rk9tbGhUN0VDM1F0ZUVxR2tOeXM0cFEifX0' + +// load server-did (this will be created when you run server did, or you can copy/paste one): +let PFI_DID = await fs.readFile('server-did.txt', 'utf-8') + // // diff --git a/src/config.ts b/src/config.ts index 4fa822a..27d4d71 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,7 @@ import type { PoolConfig } from 'pg' import type { LogLevelDesc } from 'loglevel' +import fs from 'node:fs' import 'dotenv/config' @@ -51,7 +52,9 @@ if (!config.did.id) { services: [{ id: 'pfi', type: 'PFI', serviceEndpoint: config.host }] }) + config.did.id = DidIon.did + fs.writeFileSync('server-did.txt', config.did.id) config.did.privateKey = DidIon.keySet.verificationMethodKeys[0].privateKeyJwk config.did.kid = `${config.did.id}#${config.did.privateKey.kid}` } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 9889283..a6b359a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -9,6 +9,7 @@ import { Postgres, ExchangeRespository, OfferingRepository } from './db/index.js import { HttpServerShutdownHandler } from './http-shutdown-handler.js' import { TbdexHttpServer } from '@tbdex/http-server' + process.on('unhandledRejection', (reason: any, promise) => { log.error(`Unhandled promise rejection. Reason: ${reason}. Promise: ${JSON.stringify(promise)}. Stack: ${reason.stack}`) }) @@ -31,8 +32,6 @@ process.on('SIGTERM', async () => { gracefulShutdown() }) -// await Postgres.ping() - const httpApi = new TbdexHttpServer({ exchangesApi: ExchangeRespository, offeringsApi: OfferingRepository }) httpApi.submit('rfq', async (ctx, rfq) => {