Skip to content

Commit

Permalink
refactor https&ws servers (#65)
Browse files Browse the repository at this point in the history
* refactor https&ws servers

* remove leftowers
  • Loading branch information
adam4leos authored Sep 21, 2023
1 parent 475563a commit 795d93b
Show file tree
Hide file tree
Showing 8 changed files with 592 additions and 538 deletions.
10 changes: 0 additions & 10 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@ module.exports = {
},
],
'prefer-const': ['error', { destructuring: 'all' }],
'sort-imports': [
'error',
{
ignoreCase: true,
ignoreDeclarationSort: false,
ignoreMemberSort: false,
memberSyntaxSortOrder: ['none', 'all', 'single', 'multiple'],
allowSeparatedGroups: true,
},
],
// enforce github issue reference for every TO-DO comment
'todo-plz/ticket-ref': [
'error',
Expand Down
796 changes: 401 additions & 395 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@web5/dwn-server",
"type": "module",
"version": "0.1.1",
"version": "0.1.2",
"files": [
"dist",
"src"
Expand Down
48 changes: 32 additions & 16 deletions src/dwn-server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { Config } from './config.js';

import { config as defaultConfig } from './config.js';
import { Dwn } from '@tbd54566975/dwn-sdk-js';
import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';
import type { Server } from 'http';
import { type WebSocketServer } from 'ws';

import { getDWNConfig } from './storage.js';
import { HttpApi } from './http-api.js';
import { HttpServerShutdownHandler } from './lib/http-server-shutdown-handler.js';
import log from 'loglevel';
import prefix from 'loglevel-plugin-prefix';
import { setProcessHandlers } from './process-handlers.js';
import { WsApi } from './ws-api.js';
import { type Config, config as defaultConfig } from './config.js';

export type DwnServerOptions = {
dwn?: Dwn;
Expand All @@ -18,40 +19,55 @@ export type DwnServerOptions = {
export class DwnServer {
dwn?: Dwn;
config: Config;
httpServerShutdownHandler: HttpServerShutdownHandler;
#httpServerShutdownHandler: HttpServerShutdownHandler;
#httpApi: HttpApi;
#wsApi: WsApi;

constructor(options: DwnServerOptions = {}) {
this.config = options.config ?? defaultConfig;
this.dwn = options.dwn;

log.setLevel(this.config.logLevel as log.LogLevelDesc);

prefix.reg(log);
prefix.apply(log);
}

async start(): Promise<void> {
await this.listen();
async start(callback?: () => void): Promise<void> {
await this.#setupServer();
setProcessHandlers(this);
callback?.();
}

async listen(): Promise<void> {
async #setupServer(): Promise<void> {
if (!this.dwn) {
this.dwn = await Dwn.create(getDWNConfig(this.config));
}

const httpApi = new HttpApi(this.dwn);
const httpServer = httpApi.listen(this.config.port, () => {
log.info(`server listening on port ${this.config.port}`);
this.#httpApi = new HttpApi(this.dwn);
this.#httpApi.start(this.config.port, () => {
log.info(`HttpServer listening on port ${this.config.port}`);
});

this.httpServerShutdownHandler = new HttpServerShutdownHandler(httpServer);
this.#httpServerShutdownHandler = new HttpServerShutdownHandler(
this.#httpApi.server,
);

if (this.config.webSocketServerEnabled) {
const wsServer = new WsApi(httpServer, this.dwn);
wsServer.listen();
this.#wsApi = new WsApi(this.#httpApi.server, this.dwn);
this.#wsApi.start(() => log.info(`WebSocketServer ready...`));
}
}

stop(callback: () => void): void {
this.httpServerShutdownHandler.stop(callback);
this.#httpServerShutdownHandler.stop(callback);
}

get httpServer(): Server {
return this.#httpApi.server;
}

get wsServer(): WebSocketServer {
return this.#wsApi.server;
}
}
57 changes: 42 additions & 15 deletions src/http-api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import http from 'http';
import {
type Dwn,
RecordsRead,
type RecordsReadReply,
} from '@tbd54566975/dwn-sdk-js';
import type { Express, Request, Response } from 'express';

import type { JsonRpcRequest } from './lib/json-rpc.js';
import { RecordsRead } from '@tbd54566975/dwn-sdk-js';
import type { RequestContext } from './lib/json-rpc-router.js';
import type { Server } from 'http';
import type { Dwn, RecordsReadReply } from '@tbd54566975/dwn-sdk-js';
import type { Express, Request, Response } from 'express';

import cors from 'cors';
import express from 'express';
Expand All @@ -20,15 +24,31 @@ import {
import { requestCounter, responseHistogram } from './metrics.js';

export class HttpApi {
api: Express;
#api: Express;
#server: http.Server;
dwn: Dwn;

constructor(dwn: Dwn) {
this.api = express();
this.#api = express();
this.#server = http.createServer(this.#api);
this.dwn = dwn;

this.api.use(cors({ exposedHeaders: 'dwn-response' }));
this.api.use(
this.#setupMiddleware();
this.#setupRoutes();
}

get server(): http.Server {
return this.#server;
}

get api(): Express {
return this.#api;
}

#setupMiddleware(): void {
this.#api.use(cors({ exposedHeaders: 'dwn-response' }));

this.#api.use(
responseTime((req: Request, res: Response, time) => {
const url = req.url === '/' ? '/jsonrpc' : req.url;
const route = (req.method + url)
Expand All @@ -41,13 +61,15 @@ export class HttpApi {
log.info(req.method, decodeURI(req.url), res.statusCode);
}),
);
}

this.api.get('/health', (_req, res) => {
#setupRoutes(): void {
this.#api.get('/health', (_req, res) => {
// return 200 ok
return res.json({ ok: true });
});

this.api.get('/metrics', async (req, res) => {
this.#api.get('/metrics', async (req, res) => {
try {
res.set('Content-Type', register.contentType);
res.end(await register.metrics());
Expand All @@ -56,7 +78,7 @@ export class HttpApi {
}
});

this.api.get('/:did/records/:id', async (req, res) => {
this.#api.get('/:did/records/:id', async (req, res) => {
const record = await RecordsRead.create({ recordId: req.params.id });
const reply = (await this.dwn.processMessage(
req.params.did,
Expand All @@ -82,15 +104,15 @@ export class HttpApi {
}
});

this.api.get('/', (_req, res) => {
this.#api.get('/', (_req, res) => {
// return a plain text string
res.setHeader('content-type', 'text/plain');
return res.send(
'please use a web5 client, for example: https://github.com/TBD54566975/web5-js ',
);
});

this.api.post('/', async (req: Request, res) => {
this.#api.post('/', async (req: Request, res) => {
const dwnRequest = req.headers['dwn-request'] as any;

if (!dwnRequest) {
Expand Down Expand Up @@ -157,7 +179,12 @@ export class HttpApi {
});
}

listen(port: number, callback?: () => void): Server {
return this.api.listen(port, callback);
#listen(port: number, callback?: () => void): void {
this.#server.listen(port, callback);
}

start(port: number, callback?: () => void): http.Server {
this.#listen(port, callback);
return this.#server;
}
}
Loading

0 comments on commit 795d93b

Please sign in to comment.