-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrting #68: example NestJS with TGrid case.
- Loading branch information
Showing
24 changed files
with
733 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { HttpError } from "@nestia/fetcher"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { IConnection } from "@nestia/fetcher"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type { Primitive } from "@nestia/fetcher"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
Oops, something went wrong.