Skip to content

Commit

Permalink
mock payment gateway and setup kafka using docker (#9)
Browse files Browse the repository at this point in the history
* add docker compose to setup/run kafka & zookeeper (kafka uses zookeeper to run)
* update readme file for settingup & using kafka.
* add code to check kafka working smoothly or not.
* add checkPayment() which returns sucess 70% of time.
  • Loading branch information
satyajitnayk committed Dec 23, 2023
1 parent 36b6487 commit 2260f80
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@ A queue service to manage order using Fastify framework

## Update .env file

## setup kafka locally using docker
```shell
cd setup-kafka
docker-compose build #do this first time only
docker-compose up
```

## connnect with kafka container (to check if it's runnig)
```shell
# to find containers with name
docker ps --format '{{.Names}}'

docker exec -it setup-kafka-kafka-1 bin/bash #setup-kafka-kafka-1 is my kafka container name

# find kafka ip
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_id_or_name>

# to check if kafka working in machine
node config/check_kafka.js
```

## run test
```shell
npm test
Expand Down
57 changes: 57 additions & 0 deletions config/check_kafka.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { Kafka } = require('kafkajs');

// Define your Kafka broker(s)
const kafkaBrokers = ['localhost:9093']; // Update with your Kafka broker(s) information

// Create a Kafka instance
const kafka = new Kafka({
clientId: 'kafka-test-client',
brokers: kafkaBrokers,
});

// Create a Kafka producer
const producer = kafka.producer();

// Create a Kafka consumer
const consumer = kafka.consumer({ groupId: 'kafka-test-group' });

// Function to produce a test message
async function produceMessage() {
await producer.connect();
await producer.send({
topic: 'test-topic',
messages: [{ value: 'Hello, Kafka!' }],
});
await producer.disconnect();
}

// Function to consume messages
async function consumeMessages() {
await consumer.connect();
await consumer.subscribe({ topic: 'test-topic', fromBeginning: true });

await consumer.run({
eachMessage: async ({ message }) => {
console.log(`Received message: ${message.value}`);
},
});

// Uncomment the following line if you want to stop the consumer after a certain period
// setTimeout(async () => await consumer.disconnect(), 5000);
}

// Run the test
async function runTest() {
try {
await produceMessage();
await consumeMessages();
} catch (error) {
console.error('Error:', error);
} finally {
// Uncomment the following line if you want to stop the producer after the test
// await producer.disconnect();
}
}

// Run the test
runTest();
7 changes: 7 additions & 0 deletions config/payment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* it returns success 70% of time
* @returns {boolean}
*/
export function checkPayment({txnId, orderId}) {
return Math.random() < 0.7;
}
16 changes: 15 additions & 1 deletion package-lock.json

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

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
"url": "git+https://github.com/satyajitnayk/order-management.git"
},
"author": "Satyajit Nayak",
"contributors": [ "jyoti (https://github.com/JyotiRanjanSatapathy)","sagar (https://github.com/sagarmohapatra)"],
"contributors": [
"jyoti (https://github.com/JyotiRanjanSatapathy)",
"sagar (https://github.com/sagarmohapatra)"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/satyajitnayk/order-management/issues"
Expand All @@ -21,7 +24,8 @@
"dependencies": {
"@fastify/postgres": "^5.2.2",
"dotenv": "^16.3.1",
"fastify": "^4.25.1"
"fastify": "^4.25.1",
"kafkajs": "^2.2.4"
},
"devDependencies": {
"jest": "^29.7.0",
Expand Down
32 changes: 32 additions & 0 deletions setup-kafka/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# docker-compose.yml
version: "3.7"
services:
zookeeper:
restart: always
image: docker.io/bitnami/zookeeper:3.8
ports:
- "2181:2181"
volumes:
- "zookeeper-volume:/bitnami"
environment:
- ALLOW_ANONYMOUS_LOGIN=yes
kafka:
restart: always
image: docker.io/bitnami/kafka:3.3
ports:
- "9093:9093"
volumes:
- "kafka-volume:/bitnami"
environment:
- KAFKA_BROKER_ID=1
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CLIENT:PLAINTEXT,EXTERNAL:PLAINTEXT
- KAFKA_CFG_LISTENERS=CLIENT://:9092,EXTERNAL://:9093
- KAFKA_CFG_ADVERTISED_LISTENERS=CLIENT://kafka:9092,EXTERNAL://localhost:9093
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=CLIENT
depends_on:
- zookeeper
volumes:
kafka-volume:
zookeeper-volume:

0 comments on commit 2260f80

Please sign in to comment.