Skip to content

Commit

Permalink
Refactor GRPC API into GRPCClient and GRPCServer for #12 and #11
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierGeerinck committed May 24, 2021
1 parent 6aca780 commit 3e90ef0
Show file tree
Hide file tree
Showing 23 changed files with 286 additions and 332 deletions.
36 changes: 21 additions & 15 deletions examples/grpc/hello-world/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
import Dapr, { HttpMethod } from "@roadwork/dapr-js-sdk/grpc";
import { DaprServer, DaprClient, HttpMethod } from "@roadwork/dapr-js-sdk/grpc";

const daprHost = "127.0.0.1";
const daprPort = "50050"; // gRPC Port for Dapr Client
const daprInternalServerPort = "50051"; // gRPC Port for Dapr Server
const daprPort = "50050"; // Dapr Sidecar Port of this Example Server
const daprPortActor = "10002"; // Dapr Sidecar Port of the Actor Server
const daprInternalServerPort = "50051"; // App Port of this Example Server
const daprAppId = "example-hello-world";

async function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function start() {
const client = new Dapr(daprHost, daprPort, daprInternalServerPort);
const server = new DaprServer(daprHost, daprPort, daprInternalServerPort);
const client = new DaprClient(daprHost, daprPort);
const clientActor = new DaprClient(daprHost, daprPortActor);

// console.log("===============================================================");
// console.log("REGISTERING SERVER HANDLERS")
// console.log("===============================================================");
// await client.pubsub.subscribe("pubsub-redis", "test-topic", async (data: any) => console.log(`[Dapr-JS][Example][PubSub Subscribe CB] Data: ${data}`));
// await client.binding.receive("binding-rabbitmq", async (data: any) => console.log(`[Dapr-JS][Example][Binding Receive CB] Data: ${data}`));
console.log("===============================================================");
console.log("REGISTERING SERVER HANDLERS")
console.log("===============================================================");
await server.binding.receive("binding-rabbitmq", async (data) => console.log(`[Dapr-JS][Example][Binding Receive] Got Data: ${JSON.stringify(data)}`));
await server.pubsub.subscribe("pubsub-redis", "test-topic", async (data) => console.log(`[Dapr-JS][Example][PubSub Subscribe] Got Data: ${JSON.stringify(data)}`));

console.log("===============================================================");
console.log("INITIALIZING")
console.log("===============================================================");
// We initialize after registering our listeners since these should be defined upfront
// this is how Dapr works, it waits until we are listening on the port. Once that is detected
// it will scan the binding list and pubsub subscriptions list to process
await client.startServer();
await client.startClient();
await server.startServer();

console.log("===============================================================");
console.log("EXECUTING CLIENT -INVOKER")
console.log("===============================================================");
await client.invoker.listen("hello-world", async (data: any) => {
await server.invoker.listen("hello-world", async (data: any) => {
console.log("[Dapr-JS][Example] POST /hello-world");
console.log(`[Dapr-JS][Example] Received: ${JSON.stringify(data.body)}`);
console.log(`[Dapr-JS][Example] Replying to Client`);
return { hello: "world received from POST" };
}, { method: HttpMethod.POST });

await client.invoker.listen("hello-world", async () => {
await server.invoker.listen("hello-world", async () => {
console.log("[Dapr-JS][Example] GET /hello-world");
console.log(`[Dapr-JS][Example] Replying to Client`);
return { hello: "world received from GET" };
Expand All @@ -55,6 +61,8 @@ async function start() {
const resPubSub = await client.pubsub.publish("pubsub-redis", "test-topic", { hello: "world" });
console.log(`[Dapr-JS][Example][PubSub RES] Data: ${JSON.stringify(resPubSub)}`);

await sleep(500); // wait a bit to receive the messages

console.log("===============================================================");
console.log("EXECUTING CLIENT - SECRETS");
console.log("===============================================================");
Expand Down Expand Up @@ -117,8 +125,6 @@ async function start() {
console.log("EXECUTING CLIENT - ACTORS");
console.log("Note: we create new client for now since Actors are not supported internally!")
console.log("===============================================================");
const clientActor = new Dapr(daprHost, "10002");
await clientActor.startClient();
await clientActor.actor.invoke("POST", "DemoActor", "MyActorId1", "SetDataAsync", { PropertyA: "hello", PropertyB: "world", ToNotExistKey: "this should not exist since we only have PropertyA and PropertyB" });
const resActorInvoke = await clientActor.actor.invoke("GET", "DemoActor", "MyActorId1", "GetDataAsync"); // will only return PropertyA and PropertyB since these are the only properties that can be set
console.log(`[Dapr-JS][Example][Actors] Invoked Method and got data: ${JSON.stringify(resActorInvoke)}`);
Expand Down
22 changes: 11 additions & 11 deletions examples/http/hello-world/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import Dapr, { HttpMethod } from "@roadwork/dapr-js-sdk/http";

const daprHost = "127.0.0.1";
const daprPort = "50050"; // HTTP Port for Dapr Client
const daprInternalServerPort = "50051"; // HTTP Port for Dapr Server
const daprPort = "50050"; // Dapr Sidecar Port of this Example Server
const daprPortActor = "10002"; // Dapr Sidecar Port of the Actor Server
const daprInternalServerPort = "50051"; // App Port of this Example Server
const daprAppId = "example-hello-world";

async function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function start() {
const client = new Dapr(daprHost, daprPort, daprInternalServerPort);
const server = new DaprServer(daprHost, daprPort, daprInternalServerPort);
const client = new DaprClient(daprHost, daprPort);
const clientActor = new DaprClient(daprHost, daprPortActor);

console.log("===============================================================");
console.log("REGISTERING SERVER HANDLERS")
console.log("===============================================================");
await client.binding.receive("binding-rabbitmq", async (data) => console.log(`[Dapr-JS][Example][Binding Receive] Got Data: ${JSON.stringify(data)}`));
await client.pubsub.subscribe("pubsub-redis", "test-topic", async (data) => console.log(`[Dapr-JS][Example][PubSub Subscribe] Got Data: ${JSON.stringify(data)}`));
await server.binding.receive("binding-rabbitmq", async (data) => console.log(`[Dapr-JS][Example][Binding Receive] Got Data: ${JSON.stringify(data)}`));
await server.pubsub.subscribe("pubsub-redis", "test-topic", async (data) => console.log(`[Dapr-JS][Example][PubSub Subscribe] Got Data: ${JSON.stringify(data)}`));

console.log("===============================================================");
console.log("INITIALIZING")
console.log("===============================================================");
// We initialize after registering our listeners since these should be defined upfront
// this is how Dapr works, it waits until we are listening on the port. Once that is detected
// it will scan the binding list and pubsub subscriptions list to process
await client.startServer();
await client.startClient();
await server.startServer();

console.log("===============================================================");
console.log("EXECUTING CLIENT -INVOKER")
Expand Down Expand Up @@ -59,7 +61,7 @@ async function start() {
await client.pubsub.publish("pubsub-redis", "test-topic", { hello: "world" });
console.log(`[Dapr-JS][Example][PubSub] Published to pubsub pubsub-redis on topic "test-topic"`);

await sleep(1000); // wait a bit to receive the messages
await sleep(500); // wait a bit to receive the messages

console.log("===============================================================");
console.log("EXECUTING CLIENT - SECRETS");
Expand Down Expand Up @@ -123,12 +125,10 @@ async function start() {
console.log("EXECUTING CLIENT - ACTORS");
console.log("Note: we create new client for now since Actors are not supported internally!")
console.log("===============================================================");
const clientActor = new Dapr(daprHost, "10002");
await clientActor.startClient();
await clientActor.actor.invoke("POST", "DemoActor", "MyActorId1", "SetDataAsync", { PropertyA: "hello", PropertyB: "world", ToNotExistKey: "this should not exist since we only have PropertyA and PropertyB" });
const resActorInvoke = await clientActor.actor.invoke("GET", "DemoActor", "MyActorId1", "GetDataAsync"); // will only return PropertyA and PropertyB since these are the only properties that can be set
console.log(`[Dapr-JS][Example][Actors] Invoked Method and got data: ${JSON.stringify(resActorInvoke)}`);

await clientActor.actor.stateTransaction("DemoActor", "MyActorId1", [
{
operation: "upsert",
Expand Down
49 changes: 0 additions & 49 deletions src/grpc/Dapr.ts

This file was deleted.

33 changes: 33 additions & 0 deletions src/grpc/DaprClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import DaprClientBinding from './lib/GRPCClient/binding';
import DaprClientPubSub from './lib/GRPCClient/pubsub';
import DaprClientState from './lib/GRPCClient/state';
import DaprClientInvoker from './lib/GRPCClient/invoker';
import DaprClientSecret from './lib/GRPCClient/secret';
import DaprClientActor from './lib/GRPCClient/actor';
import GRPCClient from './lib/GRPCClient/GRPCClient';

export default class DaprClient {
daprHost: string;
daprPort: string;
daprClient: GRPCClient;
pubsub: DaprClientPubSub;
state: DaprClientState;
binding: DaprClientBinding;
invoker: DaprClientInvoker;
secret: DaprClientSecret;
actor: DaprClientActor;

constructor(daprHost: string, daprPort: string) {
this.daprHost = daprHost || '127.0.0.1';
this.daprPort = daprPort || "5005";

this.daprClient = new GRPCClient(daprHost, daprPort);

this.state = new DaprClientState(this.daprClient);
this.pubsub = new DaprClientPubSub(this.daprClient);
this.binding = new DaprClientBinding(this.daprClient);
this.invoker = new DaprClientInvoker(this.daprClient);
this.secret = new DaprClientSecret(this.daprClient);
this.actor = new DaprClientActor(this.daprClient);
}
}
40 changes: 40 additions & 0 deletions src/grpc/DaprServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import DaprServerBinding from './lib/GRPCServer/binding';
import DaprServerPubSub from './lib/GRPCServer/pubsub';
import DaprServerInvoker from './lib/GRPCServer/invoker';
import DaprServerActor from './lib/GRPCServer/actor';
import GRPCServer from './lib/GRPCServer/GRPCServer';

export default class DaprServer {
daprHost: string;
daprPort: string;
daprInternalServerPort: string; // The port for our app server (e.g. dapr binding receives, pubsub receive, ...)\
daprServer: GRPCServer;
pubsub: DaprServerPubSub;
binding: DaprServerBinding;
invoker: DaprServerInvoker;
actor: DaprServerActor;

constructor(daprHost: string, daprPort: string, daprInternalServerPort: string = "50050") {
this.daprHost = daprHost || '127.0.0.1';
this.daprPort = daprPort || "5005";
this.daprInternalServerPort = process.env.DAPR_INTERNAL_SERVER_PORT || daprInternalServerPort;
this.daprServer = new GRPCServer(this.daprHost, this.daprInternalServerPort);

// If DAPR_INTERNAL_SERVER_PORT was not set, we set it
// This will be fetched by the GRPCServerSingleton
process.env.DAPR_INTERNAL_SERVER_PORT = this.daprPort;

// // Get the App Port as set in the Dapr constructor
// const randomPort = Math.floor(Math.random() * (20000 - 10000 + 1)) + 10000;
// const appPort = parseInt(process.env.DAPR_INTERNAL_SERVER_PORT || "", 10) || randomPort;

this.pubsub = new DaprServerPubSub(this.daprServer);
this.binding = new DaprServerBinding(this.daprServer);
this.invoker = new DaprServerInvoker(this.daprServer);
this.actor = new DaprServerActor(this.daprServer);
}

async startServer() {
await this.daprServer.startServer(this.daprHost, this.daprInternalServerPort.toString());
}
}
9 changes: 5 additions & 4 deletions src/grpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Dapr from './Dapr';
import DaprClient from './DaprClient';
import DaprServer from './DaprServer';

import { Request as Req, Response as Res } from 'restana';
import { HttpMethod } from './enum/HttpMethod.enum';
import HttpStatusCode from './enum/HttpStatusCode.enum';

export default Dapr;

export {
HttpMethod,
HttpStatusCode,
Req,
Res
Res,
DaprClient,
DaprServer
}
4 changes: 4 additions & 0 deletions src/grpc/lib/GRPCClient/GRPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ export default class GRPCClient {
console.log(`[Dapr-JS][gRPC] Opening connection to ${this.clientHost}:${this.clientPort}`);
this.client = new DaprClient(`${this.clientHost}:${this.clientPort}`, this.clientCredentials);
}

getClient() {
return this.client;
}
}
48 changes: 0 additions & 48 deletions src/grpc/lib/GRPCClient/GRPCClientSingleton.ts

This file was deleted.

Loading

0 comments on commit 3e90ef0

Please sign in to comment.