Skip to content

Commit 5d767c6

Browse files
authored
chore: refactor DaprServer API (#462)
* Server side options config Signed-off-by: Deepanshu Agarwal <[email protected]> * Pretty Signed-off-by: Deepanshu Agarwal <[email protected]> * Communication Protocol Signed-off-by: Deepanshu Agarwal <[email protected]> * Incorporate review comment Signed-off-by: Deepanshu Agarwal <[email protected]> * Fix docs Signed-off-by: Deepanshu Agarwal <[email protected]> * UTs and docs Signed-off-by: Deepanshu Agarwal <[email protected]> * logger Signed-off-by: Deepanshu Agarwal <[email protected]> --------- Signed-off-by: Deepanshu Agarwal <[email protected]>
1 parent af9ed88 commit 5d767c6

File tree

21 files changed

+396
-133
lines changed

21 files changed

+396
-133
lines changed

daprdocs/content/en/js-sdk-docs/js-actors/_index.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const clientOptions = {
9898
// Use the options when creating DaprServer and DaprClient.
9999

100100
// Note, DaprServer creates a DaprClient internally, which needs to be configured with clientOptions.
101-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, clientOptions);
101+
const server = new DaprServer({ serverHost, serverPort, clientOptions });
102102

103103
const client = new DaprClient(clientOptions);
104104
```
@@ -116,7 +116,14 @@ const daprPort = "50000";
116116
const serverHost = "127.0.0.1";
117117
const serverPort = "50001";
118118

119-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
119+
const server = new DaprServer({
120+
serverHost,
121+
serverPort,
122+
clientOptions: {
123+
daprHost,
124+
daprPort,
125+
},
126+
});
120127

121128
await server.actor.init(); // Let the server know we need actors
122129
server.actor.registerActor(ParkingSensorImpl); // Register the actor

daprdocs/content/en/js-sdk-docs/js-logger/_index.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,14 @@ const client = new DaprClient({
3636
import { CommunicationProtocolEnum, DaprServer, LogLevel } from "@dapr/dapr";
3737

3838
// create a server instance with log level set to error.
39-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.HTTP, {
40-
logger: { level: LogLevel.Error },
39+
const server = new DaprServer({
40+
serverHost,
41+
serverPort,
42+
clientOptions: {
43+
daprHost,
44+
daprPort,
45+
logger: { level: LogLevel.Error },
46+
},
4147
});
4248
```
4349

daprdocs/content/en/js-sdk-docs/js-server/_index.md

Lines changed: 104 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,26 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
3535
const serverPort = "50051"; // App Port of this Example Server
3636

3737
// HTTP Example
38-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
38+
const server = new DaprServer({
39+
serverHost,
40+
serverPort,
41+
communicationProtocol: CommunicationProtocolEnum.HTTP, // DaprClient to use same communication protocol as DaprServer, in case DaprClient protocol not mentioned explicitly
42+
clientOptions: {
43+
daprHost,
44+
daprPort,
45+
},
46+
});
3947

4048
// GRPC Example
41-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
49+
const server = new DaprServer({
50+
serverHost,
51+
serverPort,
52+
communicationProtocol: CommunicationProtocolEnum.GRPC,
53+
clientOptions: {
54+
daprHost,
55+
daprPort,
56+
},
57+
});
4258
```
4359

4460
## Running
@@ -50,7 +66,14 @@ To run the examples, you can use two different protocols to interact with the Da
5066
```typescript
5167
import { DaprServer } from "@dapr/dapr";
5268

53-
const server = new DaprServer(appHost, appPort, daprHost, daprPort);
69+
const server = new DaprServer({
70+
serverHost: appHost,
71+
serverPort: appPort,
72+
clientOptions: {
73+
daprHost,
74+
daprPort,
75+
},
76+
});
5477
// initialize subscribtions, ... before server start
5578
// the dapr sidecar relies on these
5679
await server.start();
@@ -84,17 +107,15 @@ myApp.get("/my-custom-endpoint", (req, res) => {
84107
res.send({ msg: "My own express app!" });
85108
});
86109

87-
const daprServer = new DaprServer(
88-
"127.0.0.1", // App Host
89-
"50002", // App Port
90-
daprHost,
91-
daprPort,
92-
CommunicationProtocolEnum.HTTP,
93-
{},
94-
{
95-
serverHttp: myApp,
96-
},
97-
);
110+
const daprServer = new DaprServer({
111+
serverHost: "127.0.0.1", // App Host
112+
serverPort: "50002", // App Port
113+
serverHttp: myApp
114+
clientOptions: {
115+
daprHost,
116+
daprPort,
117+
}
118+
});
98119

99120
// Initialize subscriptions before the server starts, the Dapr sidecar uses it.
100121
// This will also initialize the app server itself (removing the need for `app.listen` to be called).
@@ -115,7 +136,15 @@ Since HTTP is the default, you will have to adapt the communication protocol to
115136
```typescript
116137
import { DaprServer, CommunicationProtocol } from "@dapr/dapr";
117138

118-
const server = new DaprServer(appHost, appPort, daprHost, daprPort, CommunicationProtocol.GRPC);
139+
const server = new DaprServer({
140+
serverHost: appHost,
141+
serverPort: appPort,
142+
communicationProtocol: CommunicationProtocolEnum.GRPC,
143+
clientOptions: {
144+
daprHost,
145+
daprPort,
146+
},
147+
});
119148
// initialize subscribtions, ... before server start
120149
// the dapr sidecar relies on these
121150
await server.start();
@@ -148,7 +177,14 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
148177
const serverPort = "50051"; // App Port of this Example Server "
149178

150179
async function start() {
151-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
180+
const server = new DaprServer({
181+
serverHost,
182+
serverPort,
183+
clientOptions: {
184+
daprHost,
185+
daprPort,
186+
},
187+
});
152188

153189
const callbackFunction = (data: DaprInvokerCallbackContent) => {
154190
console.log("Received body: ", data.body);
@@ -197,7 +233,14 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
197233
const serverPort = "50051"; // App Port of this Example Server "
198234

199235
async function start() {
200-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
236+
const server = new DaprServer({
237+
serverHost,
238+
serverPort,
239+
clientOptions: {
240+
daprHost,
241+
daprPort,
242+
},
243+
});
201244

202245
const pubSubName = "my-pubsub-name";
203246
const topic = "topic-a";
@@ -271,7 +314,14 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
271314
const serverPort = "50051"; // App Port of this Example Server "
272315

273316
async function start() {
274-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
317+
const server = new DaprServer({
318+
serverHost,
319+
serverPort,
320+
clientOptions: {
321+
daprHost,
322+
daprPort,
323+
},
324+
});
275325

276326
const pubSubName = "my-pubsub-name";
277327
const topic = "topic-a";
@@ -314,7 +364,14 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
314364
const serverPort = "50051"; // App Port of this Example Server "
315365

316366
async function start() {
317-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
367+
const server = new DaprServer({
368+
serverHost,
369+
serverPort,
370+
clientOptions: {
371+
daprHost,
372+
daprPort,
373+
},
374+
});
318375

319376
const pubSubName = "my-pubsub-name";
320377
const topic = "topic-a";
@@ -367,13 +424,20 @@ import { DaprServer } from "@dapr/dapr";
367424
const pubSubName = "orderPubSub";
368425
const topic = "topicbulk";
369426

370-
const DAPR_HOST = process.env.DAPR_HOST || "127.0.0.1";
371-
const DAPR_HTTP_PORT = process.env.DAPR_HTTP_PORT || "3502";
372-
const SERVER_HOST = process.env.SERVER_HOST || "127.0.0.1";
373-
const SERVER_PORT = process.env.APP_PORT || 5001;
427+
const daprHost = process.env.DAPR_HOST || "127.0.0.1";
428+
const daprHttpPort = process.env.DAPR_HTTP_PORT || "3502";
429+
const serverHost = process.env.SERVER_HOST || "127.0.0.1";
430+
const serverPort = process.env.APP_PORT || 5001;
374431

375432
async function start() {
376-
const server = new DaprServer(SERVER_HOST, SERVER_PORT, DAPR_HOST, DAPR_HTTP_PORT);
433+
const server = new DaprServer({
434+
serverHost,
435+
serverPort,
436+
clientOptions: {
437+
daprHost,
438+
daprPort: daprHttpPort,
439+
},
440+
});
377441

378442
// Publish multiple messages to a topic with default config.
379443
await client.pubsub.subscribeBulk(pubSubName, topic, (data) =>
@@ -422,7 +486,14 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
422486
const serverPort = "50051"; // App Port of this Example Server "
423487

424488
async function start() {
425-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
489+
const server = new DaprServer({
490+
serverHost,
491+
serverPort,
492+
clientOptions: {
493+
daprHost,
494+
daprPort,
495+
},
496+
});
426497

427498
const pubSubName = "my-pubsub-name";
428499

@@ -465,7 +536,14 @@ const serverHost = "127.0.0.1";
465536
const serverPort = "5051";
466537

467538
async function start() {
468-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
539+
const server = new DaprServer({
540+
serverHost,
541+
serverPort,
542+
clientOptions: {
543+
daprHost,
544+
daprPort,
545+
},
546+
});
469547

470548
const bindingName = "my-binding-name";
471549

examples/grpc/hello-world-distributed/server/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
1919
const serverPort = "50051"; // App Port of this Example Server
2020

2121
async function start() {
22-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
22+
const server = new DaprServer({
23+
serverHost,
24+
serverPort,
25+
communicationProtocol: CommunicationProtocolEnum.GRPC,
26+
clientOptions: {
27+
daprHost,
28+
daprPort,
29+
},
30+
});
2331
await server.start();
2432

2533
await server.invoker.listen(

examples/grpc/hello-world/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@ async function sleep(ms: number): Promise<void> {
2525
}
2626

2727
async function start() {
28-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort, CommunicationProtocolEnum.GRPC);
28+
const server = new DaprServer({
29+
serverHost,
30+
serverPort,
31+
communicationProtocol: CommunicationProtocolEnum.GRPC,
32+
clientOptions: {
33+
daprHost,
34+
daprPort,
35+
},
36+
});
2937
const client = new DaprClient({
3038
daprHost,
3139
daprPort,

examples/http/actor-parking-sensor/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
2121
const serverPort = "50001"; // App Port of this Example Server
2222

2323
async function start() {
24-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
24+
const server = new DaprServer({
25+
serverHost,
26+
serverPort,
27+
clientOptions: {
28+
daprHost,
29+
daprPort,
30+
},
31+
});
2532
const client = new DaprClient({ daprHost, daprPort });
2633

2734
logHeader("INITIALIZING");

examples/http/actor/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ const serverHost = "127.0.0.1"; // App Host of this Example Server
2929
const serverPort = "50001"; // App Port of this Example Server
3030

3131
async function start() {
32-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
32+
const server = new DaprServer({
33+
serverHost,
34+
serverPort,
35+
clientOptions: {
36+
daprHost,
37+
daprPort,
38+
},
39+
});
3340
const client = new DaprClient({ daprHost, daprPort });
3441

3542
logHeader("INITIALIZING");

examples/http/pubsub/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ const serverPort = "50001"; // App Port of this Example Server
2020

2121
async function start() {
2222
// Create a Server (will subscribe) and Client (will publish)
23-
const server = new DaprServer(serverHost, serverPort, daprHost, daprPort);
23+
const server = new DaprServer({
24+
serverHost,
25+
serverPort,
26+
clientOptions: {
27+
daprHost,
28+
daprPort,
29+
},
30+
});
2431
const client = new DaprClient({ daprHost, daprPort });
2532

2633
// Initialize the server to subscribe (listen)

examples/invocation/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ const daprHost = "127.0.0.1";
2020
const serverPort = "50051"; // App Port of this Example Server
2121

2222
async function start() {
23-
const server = new DaprServer(serverHost, serverPort, daprHost);
23+
const server = new DaprServer({
24+
serverHost,
25+
serverPort,
26+
clientOptions: {
27+
daprHost,
28+
},
29+
});
2430
const client = new DaprClient({ daprHost });
2531

2632
// Note that invoker listeners can be set up after start() has been called

examples/pubsub/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ By default, the example uses HTTP. To use gRPC instead:
3232
- Add `CommunicationProtocolEnum.GRPC` to the DaprServer and DaprClient object creations and update the client port to `DAPR_GRPC_PORT`:
3333

3434
```typescript
35-
const server = new DaprServer(
35+
const server = new DaprServer({
3636
serverHost,
3737
serverPort,
38-
daprHost,
39-
process.env.DAPR_GRPC_PORT,
40-
CommunicationProtocolEnum.GRPC,
41-
);
38+
communicationProtocol: CommunicationProtocolEnum.GRPC,
39+
clientOptions: {
40+
daprHost,
41+
daprPort: process.env.DAPR_GRPC_PORT,
42+
},
43+
});
4244
const client = new DaprClient(daprHost, process.env.DAPR_GRPC_PORT, CommunicationProtocolEnum.GRPC);
4345
```
4446

0 commit comments

Comments
 (0)