Skip to content

Commit eb22142

Browse files
committed
Initial implementation for the RPC service
1 parent c05b920 commit eb22142

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

packages/host/src/lib/host.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { ObjLogger, prettyPrint } from "@scramjet/obj-logger";
3434

3535
import { CommonLogsPipe } from "./common-logs-pipe";
3636
import { CPMConnector } from "./cpm-connector";
37-
import { InstanceStore } from "./instance-store";
37+
import { IInstanceStore, InstanceStore } from "./instance-store";
3838

3939
import { DuplexStream } from "@scramjet/api-server";
4040
import { ConfigService, development } from "@scramjet/sth-config";
@@ -65,6 +65,7 @@ import { loadModule, logger as loadModuleLogger } from "@scramjet/module-loader"
6565
import { CSIDispatcher, DispatcherChimeEvent as DispatcherChimeEventData, DispatcherErrorEventData, DispatcherInstanceEndEventData, DispatcherInstanceEstablishedEventData, DispatcherInstanceTerminatedEventData } from "./csi-dispatcher";
6666

6767
import { parse } from "path";
68+
import { RPCService } from "./rpc-service";
6869

6970
const buildInfo = readJsonFile("build.info", __dirname, "..");
7071
const packageFile = findPackage(__dirname).next();
@@ -125,13 +126,18 @@ export class Host implements IComponent {
125126
/**
126127
* Object to store CSIControllers.
127128
*/
128-
instancesStore = InstanceStore;
129+
instancesStore: IInstanceStore = InstanceStore;
129130

130131
/**
131132
* Sequences store.
132133
*/
133134
sequenceStore = new SequenceStore();
134135

136+
/**
137+
* RPC service.
138+
*/
139+
rpcService: RPCService;
140+
135141
/**
136142
* Instance of class providing logging utilities.
137143
*/
@@ -165,6 +171,7 @@ export class Host implements IComponent {
165171
private instanceProxy: HostProxy = {
166172
onInstanceRequest: (socket: Duplex) => { this.api.server.emit("connection", socket); },
167173
};
174+
rpcBase: string;
168175

169176
public get service(): string {
170177
return name;
@@ -205,6 +212,8 @@ export class Host implements IComponent {
205212
ObjLogger.levels[ObjLogger.levels.length - 1]
206213
);
207214

215+
this.rpcService = new RPCService(this.logger, this.instancesStore);
216+
208217
const prettyLog = new DataStream().map(prettyPrint({ colors: this.config.logColors }));
209218

210219
this.logger.addOutput(prettyLog);
@@ -269,6 +278,7 @@ export class Host implements IComponent {
269278

270279
this.apiBase = this.config.host.apiBase;
271280
this.instanceBase = `${this.config.host.apiBase}/instance`;
281+
this.rpcBase = `${this.config.host.apiBase}/rpc`;
272282
this.topicsBase = `${this.config.host.apiBase}/topic`;
273283

274284
this.csiDispatcher = new CSIDispatcher({
@@ -692,6 +702,8 @@ export class Host implements IComponent {
692702
this.api.use(`${this.apiBase}/cpm`, (req, res) => this.spaceMiddleware(req, res));
693703

694704
this.api.use(`${this.instanceBase}/:id`, (req, res, next) => this.instanceMiddleware(req, res, next));
705+
706+
this.api.use(`${this.rpcBase}/:topic`, (req, res, next) => this.rpcService.handleRequest(req, res, next));
695707
}
696708

697709
/**
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { CSIController } from "./csi-controller";
22

3+
export type IInstanceStore = { [key: string]: CSIController };
4+
35
/**
46
* Object storing Instance controllers.
57
*/
6-
export const InstanceStore: { [key: string]: CSIController } = {};
8+
export const InstanceStore: IInstanceStore = {};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { APIRoute, IObjectLogger, NextCallback, ParsedMessage } from "@scramjet/types";
2+
import { ServerResponse } from "http";
3+
import { IInstanceStore } from "./instance-store";
4+
import { getRouter } from "@scramjet/api-server";
5+
6+
export class RPCService {
7+
private handlers: Map<string, string> = new Map();
8+
private router: APIRoute;
9+
10+
constructor(
11+
private logger: IObjectLogger,
12+
private sequenceStore: IInstanceStore,
13+
) {
14+
this.router = getRouter();
15+
}
16+
17+
registerHandler(path: string, instanceId: string): void {
18+
this.handlers.set(path, instanceId);
19+
throw new Error("Method not implemented.");
20+
}
21+
22+
handleRequest(request: ParsedMessage, response: ServerResponse, next: NextCallback): void {
23+
return this.router.lookup(request, response, next);
24+
}
25+
}

0 commit comments

Comments
 (0)