-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (48 loc) · 1.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const { setupDatabase } = require('./config/db.js');
const { userRoutes } = require('./routes/users.js');
const { kafka } = require('./config');
const {checkPayment} = require("./config/payment");
const {createShipment} = require("./services/shipment");
const fastify = require('fastify')({
logger: true,
});
async function runServer() {
try {
setupDatabase(fastify);
userRoutes(fastify);
// Initialize Kafka
await kafka.initializeKafka();
// consume orders
await kafka.consumeOrders(async (order) => {
const shipment = await createShipment(order)
console.log('shipment created successfully\n',shipment)
})
fastify.get('/health', async (request, reply) => {
reply.type('application/json').code(200);
return { status: 'ok', uptime: process.uptime() };
});
fastify.post('/orders', async (request, reply) => {
try {
const order = request.body;
const {orderId= "1234", txnId= "BEY23DD2"} = order;
const isPaymentSuccess = await checkPayment({orderId,txnId})
if(isPaymentSuccess) {
await kafka.produceOrder(order);
console.log('order created successfully\n', {...order, status: "success"})
reply.send({ success:true, message: `order with orderId ${orderId} placed successfully` });
} else {
reply.status(400).send({success:false, message: "payment failed"});
}
} catch (error) {
reply.status(500).send({ message: 'Error producing order' });
}
});
// Use await or return a promise for the listen operation
await fastify.listen({ port: process.env.PORT });
console.log(`Server listening on ${fastify.server.address().port}`);
} catch (error) {
console.error('Error starting the server:', error);
process.exit(1);
}
}
runServer();