Skip to content

Commit

Permalink
Wrting #68: example NestJS with TGrid case.
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed May 14, 2024
1 parent eba1297 commit 6c647a5
Show file tree
Hide file tree
Showing 24 changed files with 733 additions and 61 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ TypeScript Grid Computing Framework.

TypeScript RPC (Remote Procedure Call) framework for WebSocket and Worker protocols.

- `WebSocket`
- `Worker`
- `SharedWorker`
- `NestJS`

Also, extremely easy even when composing complicated network system like grid computing.

```typescript
Expand Down Expand Up @@ -68,7 +73,7 @@ npm install tgrid
Just install with `npm` command. That's all.
If you wanna use `tgrid` in `NestJS`, read `nestia` guide documents.
If you wanna use `TGrid` in `NestJS`, read `Nestia` guide documents.
- [Nestia > Guide Documents > Setup](https://nestia.io/docs/setup/)
- [Nestia > Guide Documents > WebSocketRoute](https://nestia.io/docs/core/WebSocketRoute/)
Expand All @@ -86,9 +91,8 @@ Check out the document in the [website](https://tgrid.com/docs):
### 📖 Tutorial
- Features
- [Components](https://tgrid.com/docs/features/components)
- [Worker Protocol](https://tgrid.com/docs/features/worker)
- [WebSocket Protocol](https://tgrid.com/docs/features/websocket)
- [NestJS WebSocket](https://tgrid.com/docs/features/nestjs)
- [Worker Protocol](https://tgrid.com/docs/features/worker)
- Learn from Examples
- [Remote Function Call](https://tgrid.com/docs/examples/remote-function-call)
- [Remote Object Call](https://tgrid.com/docs/examples/remote-object-call)
Expand Down
13 changes: 13 additions & 0 deletions examples/nestia.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { INestiaConfig } from "@nestia/sdk";
import { NestFactory } from "@nestjs/core";

import { CalculateModule } from "./src/nestjs/calculate.module";

export const NESTIA_CONFIG: INestiaConfig = {
input: async () => {
const app = await NestFactory.create(CalculateModule);
return app;
},
output: "src/api",
};
export default NESTIA_CONFIG;
21 changes: 17 additions & 4 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@
"description": "",
"main": "index.js",
"scripts": {
"start:nestjs": "ts-node src/nestjs",
"start:websocket": "ts-node src/websocket",
"start:worker": "ts-node src/worker"
"start:worker": "ts-node src/worker",
"prepare": "ts-patch install && typia patch"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^5.4.5"
"@nestia/sdk": "^3.1.4",
"nestia": "^5.3.0",
"ts-node": "^10.9.2",
"ts-patch": "^3.1.2",
"typescript": "^5.4.2"
},
"dependencies": {
"tgrid": "1.0.0-dev.20240507"
"@nestia/core": "^3.1.4",
"@nestia/e2e": "^0.4.3",
"@nestia/fetcher": "^3.1.4",
"@nestjs/common": "^10.3.8",
"@nestjs/core": "^10.3.8",
"@nestjs/platform-express": "^10.3.8",
"tgrid": "1.0.0-dev.20240507",
"typia": "^6.0.3"
}
}
}
1 change: 1 addition & 0 deletions examples/src/api/HttpError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { HttpError } from "@nestia/fetcher";
1 change: 1 addition & 0 deletions examples/src/api/IConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { IConnection } from "@nestia/fetcher";
1 change: 1 addition & 0 deletions examples/src/api/Primitive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { Primitive } from "@nestia/fetcher";
164 changes: 164 additions & 0 deletions examples/src/api/functional/calculate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* @packageDocumentation
* @module api.functional.calculate
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
//================================================================
import type { IConnection } from "@nestia/fetcher";
import { WebSocketConnector } from "tgrid";
import type { Driver } from "tgrid";

import type { ICalcConfig } from "../../../interfaces/ICalcConfig";
import type { ICalcEventListener } from "../../../interfaces/ICalcEventListener";
import type { ICompositeCalculator } from "../../../interfaces/ICompositeCalculator";
import type { IScientificCalculator } from "../../../interfaces/IScientificCalculator";
import type { ISimpleCalculator } from "../../../interfaces/ISimpleCalculator";
import type { IStatisticsCalculator } from "../../../interfaces/IStatisticsCalculator";

/**
* Prepare a composite calculator.
*
* @controller CalculateController.composite
* @path /calculate/composite
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function composite(
connection: IConnection<composite.Header>,
provider: composite.Provider,
): Promise<composite.Output> {
const connector: WebSocketConnector<
composite.Header,
composite.Provider,
composite.Listener
> = new WebSocketConnector(connection.headers ?? ({} as any), provider);
await connector.connect(
`${connection.host.endsWith("/") ? connection.host.substring(0, connection.host.length - 1) : connection.host}${composite.path()}`,
);
const driver: Driver<composite.Listener> = connector.getDriver();
return {
connector,
driver,
};
}
export namespace composite {
export type Output = {
connector: WebSocketConnector<Header, Provider, Listener>;
driver: Driver<Listener>;
};
export type Header = ICalcConfig;
export type Provider = ICalcEventListener;
export type Listener = ICompositeCalculator;

export const path = () => "/calculate/composite";
}

/**
* Prepare a simple calculator.
*
* @controller CalculateController.simple
* @path /calculate/simple
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function simple(
connection: IConnection<simple.Header>,
provider: simple.Provider,
): Promise<simple.Output> {
const connector: WebSocketConnector<
simple.Header,
simple.Provider,
simple.Listener
> = new WebSocketConnector(connection.headers ?? ({} as any), provider);
await connector.connect(
`${connection.host.endsWith("/") ? connection.host.substring(0, connection.host.length - 1) : connection.host}${simple.path()}`,
);
const driver: Driver<simple.Listener> = connector.getDriver();
return {
connector,
driver,
};
}
export namespace simple {
export type Output = {
connector: WebSocketConnector<Header, Provider, Listener>;
driver: Driver<Listener>;
};
export type Header = ICalcConfig;
export type Provider = ICalcEventListener;
export type Listener = ISimpleCalculator;

export const path = () => "/calculate/simple";
}

/**
* Prepare a scientific calculator.
*
* @controller CalculateController.scientific
* @path /calculate/scientific
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function scientific(
connection: IConnection<scientific.Header>,
provider: scientific.Provider,
): Promise<scientific.Output> {
const connector: WebSocketConnector<
scientific.Header,
scientific.Provider,
scientific.Listener
> = new WebSocketConnector(connection.headers ?? ({} as any), provider);
await connector.connect(
`${connection.host.endsWith("/") ? connection.host.substring(0, connection.host.length - 1) : connection.host}${scientific.path()}`,
);
const driver: Driver<scientific.Listener> = connector.getDriver();
return {
connector,
driver,
};
}
export namespace scientific {
export type Output = {
connector: WebSocketConnector<Header, Provider, Listener>;
driver: Driver<Listener>;
};
export type Header = ICalcConfig;
export type Provider = ICalcEventListener;
export type Listener = IScientificCalculator;

export const path = () => "/calculate/scientific";
}

/**
* Prepare a statistics calculator.
*
* @controller CalculateController.statistics
* @path /calculate/statistics
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
export async function statistics(
connection: IConnection<statistics.Header>,
provider: statistics.Provider,
): Promise<statistics.Output> {
const connector: WebSocketConnector<
statistics.Header,
statistics.Provider,
statistics.Listener
> = new WebSocketConnector(connection.headers ?? ({} as any), provider);
await connector.connect(
`${connection.host.endsWith("/") ? connection.host.substring(0, connection.host.length - 1) : connection.host}${statistics.path()}`,
);
const driver: Driver<statistics.Listener> = connector.getDriver();
return {
connector,
driver,
};
}
export namespace statistics {
export type Output = {
connector: WebSocketConnector<Header, Provider, Listener>;
driver: Driver<Listener>;
};
export type Header = ICalcConfig;
export type Provider = ICalcEventListener;
export type Listener = IStatisticsCalculator;

export const path = () => "/calculate/statistics";
}
7 changes: 7 additions & 0 deletions examples/src/api/functional/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @packageDocumentation
* @module api.functional
* @nestia Generated by Nestia - https://github.com/samchon/nestia
*/
//================================================================
export * as calculate from "./calculate";
4 changes: 4 additions & 0 deletions examples/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * as api from "./module";

export * from "./module";
export default api;
5 changes: 5 additions & 0 deletions examples/src/api/module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type * from "./IConnection";
export type * from "./Primitive";
export * from "./HttpError";

export * as functional from "./functional";
12 changes: 12 additions & 0 deletions examples/src/nestjs/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { WebSocketAdaptor } from "@nestia/core";
import { INestApplication } from "@nestjs/common";
import { NestFactory } from "@nestjs/core";

import { CalculateModule } from "./calculate.module";

export const bootstrap = async (): Promise<INestApplication> => {
const app: INestApplication = await NestFactory.create(CalculateModule);
await WebSocketAdaptor.upgrade(app);
await app.listen(37_000, "0.0.0.0");
return app;
};
98 changes: 98 additions & 0 deletions examples/src/nestjs/calculate.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { WebSocketRoute } from "@nestia/core";
import { Controller } from "@nestjs/common";
import { Driver, WebSocketAcceptor } from "tgrid";

import { ICalcConfig } from "../interfaces/ICalcConfig";
import { ICalcEventListener } from "../interfaces/ICalcEventListener";
import { ICompositeCalculator } from "../interfaces/ICompositeCalculator";
import { IScientificCalculator } from "../interfaces/IScientificCalculator";
import { ISimpleCalculator } from "../interfaces/ISimpleCalculator";
import { IStatisticsCalculator } from "../interfaces/IStatisticsCalculator";
import { CompositeCalculator } from "../providers/CompositeCalculator";
import { ScientificCalculator } from "../providers/ScientificCalculator";
import { SimpleCalculator } from "../providers/SimpleCalculator";
import { StatisticsCalculator } from "../providers/StatisticsCalculator";

@Controller("calculate")
export class CalculateController {
/**
* Prepare a composite calculator.
*/
@WebSocketRoute("composite")
public async composite(
@WebSocketRoute.Acceptor()
acceptor: WebSocketAcceptor<
ICalcConfig,
ICompositeCalculator,
ICalcEventListener
>,
@WebSocketRoute.Header() header: ICalcConfig,
@WebSocketRoute.Driver() listener: Driver<ICalcEventListener>,
): Promise<void> {
const provider: CompositeCalculator = new CompositeCalculator(
header,
listener,
);
await acceptor.accept(provider);
}

/**
* Prepare a simple calculator.
*/
@WebSocketRoute("simple")
public async simple(
@WebSocketRoute.Acceptor()
acceptor: WebSocketAcceptor<
ICalcConfig, // header
ISimpleCalculator, // provider for remote client
ICalcEventListener // provider from remote client
>,
): Promise<void> {
const header: ICalcConfig = acceptor.header;
const listener: Driver<ICalcEventListener> = acceptor.getDriver();
const provider: SimpleCalculator = new SimpleCalculator(header, listener);
await acceptor.accept(provider);
}

/**
* Prepare a scientific calculator.
*/
@WebSocketRoute("scientific")
public async scientific(
@WebSocketRoute.Acceptor()
acceptor: WebSocketAcceptor<
ICalcConfig,
IScientificCalculator,
ICalcEventListener
>,
): Promise<void> {
const header: ICalcConfig = acceptor.header;
const listener: Driver<ICalcEventListener> = acceptor.getDriver();
const provider: ScientificCalculator = new ScientificCalculator(
header,
listener,
);
await acceptor.accept(provider);
}

/**
* Prepare a statistics calculator.
*/
@WebSocketRoute("statistics")
public async statistics(
@WebSocketRoute.Acceptor()
acceptor: WebSocketAcceptor<
ICalcConfig,
IStatisticsCalculator,
ICalcEventListener
>,
): Promise<void> {
const header: ICalcConfig = acceptor.header;
const listener: Driver<ICalcEventListener> = acceptor.getDriver();
const provider: IStatisticsCalculator = new StatisticsCalculator(
header,
listener,
);
await acceptor.accept(provider);
}
}
8 changes: 8 additions & 0 deletions examples/src/nestjs/calculate.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from "@nestjs/common";

import { CalculateController } from "./calculate.controller";

@Module({
controllers: [CalculateController],
})
export class CalculateModule {}
Loading

0 comments on commit 6c647a5

Please sign in to comment.