Skip to content

Commit

Permalink
feat(Add a nats client to consume cic-chain-events.): Create a consum…
Browse files Browse the repository at this point in the history
…er to parse events from cic-chain-events and handle porting to Jessamy after processing and tagging.

- Add nats plugin and supporting business logic implementation.
- Make app builder and initialization functions async to accommodate async functions related to nats. (What are teh repercussions of this ? ).
- Update .env.example file.
- Add convenience script for dev-xp to source env files (env.sh).
- Add nats deps.
- Refactor plugins dir for cleaner implementation.
- Add `-r tsconfig-paths/register` to accommodate path aliasing in ts-config.
  • Loading branch information
Mango Habanero committed Jan 25, 2023
1 parent 86c0e98 commit 37a452b
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 31 deletions.
13 changes: 11 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
API_VERSION=v1
AFRICAS_TALKING_VALID_IPS=127.0.0.1,0.0.0.0
AFRICASTALKING_VALID_IPS=127.0.0.1,0.0.0.0
CIC_CUSTODIAL_BALANCE_ENDPOINT=http://localhost:5000/api/balance
CIC_CUSTODAIL_REGISTER_ENDPOINT=http://localhost:5000/api/register
CIC_CUSTODIAL_TRANSFER_ENDPOINT=http://localhost:5000/api/transfer
CIC_GRAPH_GRAPHQQL_ENDPOINT=http://localhost:6080/graphql
CIC_GRAPH_HASURA_ADMIN_SECRET=admin
DATABASE_URL=postgres://postgres:password@localhost:5432/cic_ussd
LOG_LEVEL=debug
NATS_CLIENT_NAME=cic-ussd
NATS_DRAIN_ON_SHUTDOWN=true
NATS_URL=nats://localhost:4222
NATS_SUBJECT=CHAIN.*
NODE_ENV=development
REDIS_DATABASE=0
REDIS_HOST=localhost
REDIS_PASSWORD=xD
REDIS_PORT=6379
SERVER_HOST=127.0.0.1
SERVER_PORT=5000
SERVER_PORT=5000
13 changes: 10 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
API_VERSION=v1
AFRICAS_TALKING_VALID_IPS=127.0.0.1,0.0.0.0
CIC_GRAPH_GRAPHQL_ENDPOINT=http://localhost:6080/v1/graphql
AFRICASTALKING_VALID_IPS=127.0.0.1,0.0.0.0
CIC_CUSTODIAL_BALANCE_ENDPOINT=http://localhost:5000/api/balance
CIC_CUSTODAIL_REGISTER_ENDPOINT=http://localhost:5000/api/register
CIC_CUSTODIAL_TRANSFER_ENDPOINT=http://localhost:5000/api/transfer
CIC_GRAPH_GRAPHQQL_ENDPOINT=http://localhost:6080/graphql
CIC_GRAPH_HASURA_ADMIN_SECRET=admin
DATABASE_DSN=postgres://postgres:postgres@localhost:5432/cic_ussd
DATABASE_URL=postgres://postgres:password@localhost:5432/cic_ussd
LOG_LEVEL=debug
NATS_CLIENT_NAME=cic-ussd
NATS_DRAIN_ON_SHUTDOWN=true
NATS_URL=nats://localhost:4222
NATS_SUBJECT=CHAIN.*
NODE_ENV=development
REDIS_DATABASE=0
REDIS_HOST=localhost
Expand Down
4 changes: 4 additions & 0 deletions dev/env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
PROJECT_ROOT_DIR=$(dirname "$(dirname "$(realpath "$0")")")
echo "PROJECT_ROOT_DIR=$PROJECT_ROOT_DIR"
eval "$(cat "$PROJECT_ROOT_DIR/.env" | sed 's/^/export /')"
109 changes: 101 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/grassrootseconomics/cic-ussd/issues",
"license": "AGPL-3.0-or-later",
"scripts": {
"dev": " ts-node-dev --exit-child src/index.ts ",
"dev": " ts-node-dev --exit-child -r tsconfig-paths/register src/index.ts",
"build": "tsc -p tsconfig.json",
"lint": "eslint src --ext .ts --fix"
},
Expand All @@ -20,12 +20,13 @@
"@prisma/client": "^4.8.0",
"@types/qs": "^6.9.7",
"@xstate/immer": "^0.3.1",
"dotenv": "^16.0.3",
"fastify": "^4.9.2",
"fastify-plugin": "^4.5.0",
"graphql-request": "^5.1.0",
"immer": "^9.0.16",
"ioredis": "^5.2.4",
"libphonenumber-js": "^1.10.14",
"nats": "^2.11.0",
"prisma": "^4.8.0",
"qs": "^6.11.0",
"redis-json": "^6.0.3",
Expand All @@ -38,11 +39,13 @@
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"@xstate/cli": "^0.3.3",
"dotenv": "^16.0.3",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.7.1",
"ts-node-dev": "^2.0.0",
"tsconfig-paths": "^4.1.2",
"typescript": "^4.8.4"
},
"prisma": {
Expand Down
12 changes: 9 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import fastifyCors from '@fastify/cors'
import formBody from '@fastify/formbody'
import fastifySensible from '@fastify/sensible'
import fastify, { FastifyInstance } from 'fastify'
import fastify from 'fastify'
import { config } from './config'
import qs from 'qs';
import * as dotenv from 'dotenv'
import natsService from '@plugins/nats'
import { initChainEventsHandler } from "@lib/events/handler";


const build = (): FastifyInstance => {
const build = async () => {
// TODO: [Philip] - Whereas this shifts from convict to dotenv, is it ideal for externally defined variables like ones stored in vault?
dotenv.config()

// create fastify app.
const app = fastify({
disableRequestLogging: true,
logger: {
Expand All @@ -23,8 +27,10 @@ const build = (): FastifyInstance => {
})
app.register(fastifyCors, { origin: true })
app.register(fastifySensible)
app.register(natsService)


app.setErrorHandler<Error>(function (error, request, reply) {
app.setErrorHandler<Error>(function(error, request, reply) {
app.log.error({ error: error.toString(), request: request })

reply.status(500).send({
Expand Down
20 changes: 18 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
function stringToList (value : string|undefined) : string[] | void {
if (value === undefined) {
return
} else {
return value.split(',');
}
}

export const config = {
API: {
VERSION: process.env.API_VERSION
},
AFRICAS_TALKING: {
VALID_IPS: process.env.AFRICASTALKING_VALID_IPS
AFRICASTALKING: {
VALID_IPS: stringToList(process.env.AFRICASTALKING_VALID_IPS) ?? ['0.0.0.0']
},
CIC_CUSTODIAL:{
BALANCE_ENDPOINT: process.env.CIC_CUSTODIAL_BALANCE_ENDPOINT ?? 'http://localhost:5000/api/balance',
REGISTER_ENDPOINT: process.env.CIC_CUSTODIAL_REGISTER_ENDPOINT ?? 'http://localhost:5000/api/register',
TRANSFER_ENDPOINT: process.env.CIC_CUSTODIAL_TRANSFER_ENDPOINT ?? 'http://localhost:5000/api/transfer',
},
Expand All @@ -20,6 +29,13 @@ export const config = {
LOG: {
LEVEL: process.env.LOG_LEVEL
},
NATS : {
CLIENT_NAME: process.env.NATS_CLIENT_NAME ?? 'cic-ussd',
DRAIN_ON_SHUTDOWN: process.env.NATS_DRAIN_ON_SHUTDOWN ?? true,
SUBJECT: process.env.NATS_SUBJECT ?? 'CHAIN.*',
URL: process.env.NATS_URL ?? 'nats://localhost:4222',
VERBOSE: process.env.NATS_VERBOSE ?? false
},
REDIS: {
DATABASE: parseInt(process.env.REDIS_DATABASE ?? '0'),
HOST: process.env.REDIS_HOST ?? 'localhost',
Expand Down
Loading

0 comments on commit 37a452b

Please sign in to comment.