forked from dapr/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
90 lines (79 loc) · 2.39 KB
/
app.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------
const daprPort = process.env.DAPR_HTTP_PORT || "3500";
// The Dapr endpoint for the state store component to store the tweets.
const pubsubEndpoint = `http://localhost:${daprPort}/v1.0/publish/pubsub/inventory`;
const express = require('express');
const axios = require('axios');
const app = express();
// Dapr publishes messages with the application/cloudevents+json content-type
app.use(express.json({ type: ['application/json', 'application/*+json'] }));
const port = 3000;
app.get('/dapr/subscribe', (_req, res) => {
// Programmatic subscriptions
// See ./components/subscription.yaml for declarative subscription.
//
res.json([
// Previous subscription structure.
// {
// pubsubname: "pubsub",
// topic: "inventory",
// route: "products"
// }
//
// Subscription with routing rules and default route.
// {
// pubsubname: "pubsub",
// topic: "inventory",
// routes: {
// rules: [
// {
// match: `event.type == "widget"`,
// path: "widgets"
// },
// {
// match: `event.type == "gadget"`,
// path: "gadgets"
// }
// ],
// default: "products"
// }
// }
]);
});
// Default product handler.
app.post('/products', (req, res) => {
console.log("🤔 PRODUCT (default): ", req.body);
console.log();
res.sendStatus(200);
});
// Specific handler for widgets.
app.post('/widgets', (req, res) => {
console.log("🪛 WIDGET: ", req.body);
console.log();
res.sendStatus(200);
});
// Specific handler for gadgets.
app.post('/gadgets', (req, res) => {
console.log("📱 GADGET: ", req.body);
console.log();
res.sendStatus(200);
});
// Allow publishing freeform cloud events to the topic.
app.post('/publish', async (req, res) => {
console.log("publishing", req.body);
console.log();
axios.post(pubsubEndpoint, req.body, {
headers: {
'content-type': 'application/cloudevents+json'
}
})
.then(() => { res.sendStatus(200); })
.catch(error => {
res.sendStatus(500);
console.error('There was an error!', error);
});
});
app.listen(port, () => console.log(`Node App listening on port ${port}!`));