Add Order Processing for Javascript:

Signed-off-by: Xavier Geerinck <xavier.geerinck@gmail.com>
This commit is contained in:
Xavier Geerinck 2022-02-01 19:41:33 +01:00 committed by Artur Souza
parent 68a8128eee
commit c309e16bc6
12 changed files with 2372 additions and 4803 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +0,0 @@
{
"name": "javascript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"dapr-client": "^1.0.2",
"express": "^4.17.1"
},
"devDependencies": {
"nodemon": "^2.0.14"
},
"type": "module"
}

View File

@ -1,28 +0,0 @@
import { DaprServer, CommunicationProtocolEnum } from 'dapr-client';
const daprHost = "127.0.0.1";
const serverHost = "127.0.0.1";
const serverPort = "6002";
start().catch((e) => {
console.error(e);
process.exit(1);
});
async function start(orderId, response) {
const PUBSUB_NAME = "order_pub_sub"
const TOPIC_NAME = "orders"
const server = new DaprServer(
serverHost,
serverPort,
daprHost,
process.env.DAPR_HTTP_PORT,
CommunicationProtocolEnum.HTTP
);
await server.pubsub.subscribe(PUBSUB_NAME, TOPIC_NAME, async (orderId) => {
console.log(`Subscriber received: ${JSON.stringify(orderId)}`)
}, (response) => {
response.sendStatus(200);
});
await server.startServer();
}

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +0,0 @@
{
"name": "javascript",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"dapr-client": "^1.0.2",
"express": "^4.17.1"
},
"devDependencies": {
"nodemon": "^2.0.14"
},
"type": "module"
}

View File

@ -1,29 +0,0 @@
import { DaprServer, DaprClient, CommunicationProtocolEnum } from 'dapr-client';
const daprHost = "127.0.0.1";
var main = function() {
for(var i=0;i<10;i++) {
sleep(5000);
var orderId = Math.floor(Math.random() * (1000 - 1) + 1);
start(orderId).catch((e) => {
console.error(e);
process.exit(1);
});
}
}
async function start(orderId) {
const client = new DaprClient(daprHost, process.env.DAPR_HTTP_PORT, CommunicationProtocolEnum.HTTP);
console.log("Published data:" + orderId)
await client.pubsub.publish("order_pub_sub", "orders", orderId);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main();

View File

@ -0,0 +1,20 @@
import { DaprClient } from 'dapr-client';
import { v4 as uuidv4 } from 'uuid';
const SIDECAR_HOST = process.env.SIDECAR_HOST || "127.0.0.1";
const SIDECAR_PORT = process.env.SIDECAR_PORT || 3500;
async function main() {
const client = new DaprClient(SIDECAR_HOST, SIDECAR_PORT);
while (true) {
await client.pubsub.publish("order_pub_sub", "orders", { orderId: uuidv4() });
await sleep(1000);
}
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
main().catch(e => console.error(e))

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,19 @@
{
"name": "checkout",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js",
"start:dapr": "dapr run --app-id checkout --app-protocol http --dapr-grpc-port 3501 --components-path ./components -- npm run start",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dapr-client": "^2.0.1",
"uuid": "^8.3.2"
}
}

View File

@ -0,0 +1,16 @@
// Order Processing = Server which processes items
// Checkout will do the pub sub
import { DaprServer } from 'dapr-client';
const SIDECAR_HOST = process.env.SIDECAR_HOST || "127.0.0.1";
const SIDECAR_PORT = process.env.SIDECAR_PORT || 3500;
const SERVER_HOST = process.env.SERVER_HOST || "127.0.0.1";
const SERVER_PORT = process.env.SERVER_PORT || 5000;
async function main() {
const server = new DaprServer(SERVER_HOST, SERVER_PORT, SIDECAR_HOST, SIDECAR_PORT);
server.pubsub.subscribe("order_pub_sub", "orders", (data) => console.log(data));
await server.start();
}
main().catch(e => console.error(e));

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,19 @@
{
"name": "order-processor",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"start": "node index.js",
"start:dapr": "dapr run --app-port 5000 --app-id order-processing --app-protocol http --dapr-http-port 3500 --components-path ./components -- npm run start",
"demo:publish": "dapr publish --publish-app-id order-processing --pubsub my-pubsub-name --topic my-topic --data '{\"image_url\": \"https://www.ikea.com/images/een-3-zitsbank-met-chaise-longue-een-stellingkast-met-deuren-04d392ffcd855db85a5373f188230c66.jpg\"}'",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dapr-client": "^2.0.1"
}
}