Add HTTP routing and server management classes
- Introduced UserApiRoute for managing user-related API routes - Implemented HttpRouteManager for registering and retrieving HTTP routes - Created HttpServer class to initialize and start an Express server with route management - Added middleware and static route handling in HttpServer
This commit is contained in:
95
src/net/http/HttpRouteManger.ts
Normal file
95
src/net/http/HttpRouteManger.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import assertUnreachable from '../../lang/assertUnreachable';
|
||||
import type express from 'express';
|
||||
|
||||
export type HttpRoute = string;
|
||||
export type RoutePath = string;
|
||||
export type RouteHandler = (request: express.Request, response: express.Response) => void | Promise<void>;
|
||||
|
||||
export class HttpRouteManager {
|
||||
private readonly _staticGETRoutes: Record<HttpRoute, RoutePath>;
|
||||
private readonly _getRoutes: Record<HttpRoute, RouteHandler>;
|
||||
private readonly _postRoutes: Record<HttpRoute, RouteHandler>;
|
||||
private readonly _putRoutes: Record<HttpRoute, RouteHandler>;
|
||||
private readonly _deleteRoutes: Record<HttpRoute, RouteHandler>;
|
||||
|
||||
constructor() {
|
||||
this._staticGETRoutes = {};
|
||||
this._getRoutes = {};
|
||||
this._postRoutes = {};
|
||||
this._putRoutes = {};
|
||||
this._deleteRoutes = {};
|
||||
}
|
||||
|
||||
public registerStaticRoute(routePath: RoutePath, destination: string) {
|
||||
if (this._staticGETRoutes[routePath]) {
|
||||
throw new Error(`Error: Static route [${routePath}] is already defined`);
|
||||
}
|
||||
|
||||
this._staticGETRoutes[routePath] = destination;
|
||||
}
|
||||
|
||||
public register(verb: 'GET' | 'POST' | 'PUT' | 'DELETE', route: string, handler: RouteHandler) {
|
||||
const isRouteDefined = (routes: Record<HttpRoute, RouteHandler>, routePath: string) => !!routes[routePath];
|
||||
|
||||
switch (verb) {
|
||||
case 'GET':
|
||||
if (isRouteDefined(this._getRoutes, route)) {
|
||||
throw new Error(`Error: Route [${route}] is already defined`);
|
||||
}
|
||||
|
||||
this._getRoutes[route] = handler;
|
||||
|
||||
break;
|
||||
|
||||
case 'POST':
|
||||
if (isRouteDefined(this._postRoutes, route)) {
|
||||
throw new Error(`Error: Route [${route}] is already defined`);
|
||||
}
|
||||
|
||||
this._postRoutes[route] = handler;
|
||||
|
||||
break;
|
||||
|
||||
case 'PUT':
|
||||
if (isRouteDefined(this._putRoutes, route)) {
|
||||
throw new Error(`Error: Route [${route}] is already defined`);
|
||||
}
|
||||
|
||||
this._putRoutes[route] = handler;
|
||||
|
||||
break;
|
||||
|
||||
case 'DELETE':
|
||||
if (isRouteDefined(this._deleteRoutes, route)) {
|
||||
throw new Error(`Error: Route [${route}] is already defined`);
|
||||
}
|
||||
|
||||
this._deleteRoutes[route] = handler;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
assertUnreachable(verb);
|
||||
}
|
||||
}
|
||||
|
||||
public getStaticGETRoutes(): Record<HttpRoute, RoutePath> {
|
||||
return this._staticGETRoutes;
|
||||
}
|
||||
|
||||
public getGETRoutes(): Record<HttpRoute, RouteHandler> {
|
||||
return this._getRoutes;
|
||||
}
|
||||
|
||||
public getPOSTRoutes(): Record<HttpRoute, RouteHandler> {
|
||||
return this._postRoutes;
|
||||
}
|
||||
|
||||
public getPUTRoutes(): Record<HttpRoute, RouteHandler> {
|
||||
return this._putRoutes;
|
||||
}
|
||||
|
||||
public getDELETERoutes(): Record<HttpRoute, RouteHandler> {
|
||||
return this._deleteRoutes;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user