rpc.ts 766 B

123456789101112131415161718192021222324
  1. import express from "express";
  2. import { createServer } from "http";
  3. import { ModuleRpcServer } from "rpc_ts/lib/server";
  4. import { ModuleRpcProtocolServer } from "rpc_ts/lib/protocol/server";
  5. import { NoctBotService } from "@shared/rpc/backend";
  6. import { logger } from "./logging";
  7. const PORT = +(process.env.WEB_PORT ?? "8181");
  8. const app = express();
  9. const handler: ModuleRpcServer.ServiceHandlerFor<typeof NoctBotService> = {
  10. async getPing({ ping }): Promise<{ text: string }> {
  11. return { text: `pong: ${ping}` };
  12. }
  13. };
  14. app.use(ModuleRpcProtocolServer.registerRpcRoutes(NoctBotService, handler));
  15. export function startRpcServer(): void {
  16. logger.info(`Starting RPC at *:${PORT}`);
  17. createServer(app).listen(PORT);
  18. }