forked from nick87kelly/cns-dapr-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.js
56 lines (42 loc) · 1.17 KB
/
publish.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
// publish.js - CNS Dapr client example
// Copyright 2023 Padi, Inc. All Rights Reserved.
'use strict';
// Imports
const dapr = require('@dapr/dapr');
// Constants
const DAPR_HOST = process.env.CNS_DAPR_HOST || 'localhost';
const DAPR_PORT = process.env.CNS_DAPR_PORT || '3500';
const CNS_PUBSUB = process.env.CNS_PUBSUB || 'cns-pubsub';
const CNS_CONTEXT = process.env.CNS_CONTEXT || '';
// Dapr client
const client = new dapr.DaprClient({
daprHost: DAPR_HOST,
daprPort: DAPR_PORT
});
// Client application
async function start() {
// No context?
if (CNS_CONTEXT === '')
throw new Error('not configured');
// Start client
await client.start();
// dapr publish --publish-app-id cns-dapr --pubsub cns-pubsub --topic <context> --data '{"comment":"Testing"}'
try {
const topic = process.argv[2] || CNS_CONTEXT;
const payload = process.argv[3] || '{"comment":"Testing"}';
await client.pubsub.publish(
CNS_PUBSUB,
topic,
payload);
} catch(e) {
// Failure
throw new Error('bad request');
}
// Success
console.log('Ok');
}
// Start application
start().catch((e) => {
console.error('Error:', e.message);
process.exit(1);
});