Enhance WebSocket server with new HTTP server implementation and type assertions

* Updated package.json to specify the entry point for the development script and added morgan as a dependency.
* Introduced HttpServer class for handling HTTP requests with Express and integrated logging.
* Added new assertion methods in the Assert class for better type validation.
* Created IRoutes interface to define route handling structure.
* Added optional and nullable type definitions for improved type safety.
* Implemented initial server setup in src/index.ts.
This commit is contained in:
2025-09-27 14:36:06 -04:00
parent cd40ed2bca
commit e895704785
6 changed files with 142 additions and 3 deletions

View File

@@ -0,0 +1,68 @@
import { Nullable } from '../../types/optional';
import Assert from '../../lang/Assert';
import type {Server} from 'node:http';
import http from 'node:http';
import {EventEmitter} from 'node:events';
import type {ILogger} from '@techniker-me/logger';
import {LoggerFactory} from '@techniker-me/logger';
import IRoutes from './IRoutes';
import {Subject} from '@techniker-me/tools';
import express from 'express';
import morgan from 'morgan';
export default class HttpServer {
private readonly _logger: ILogger = LoggerFactory.getLogger('HttpServer');
private readonly _protocol: 'http' | 'https';
private readonly _port: number;
private readonly _routes: IRoutes;
private readonly _viewsPath: object;
private readonly _viewParameters: string;
private readonly _resourcesPaths: string[];
private readonly _favicon: string;
private readonly _cors: object;
private readonly _app: Subject<Nullable<express.Application>>;
private readonly _eventEmitter: EventEmitter;
private readonly _server: Subject<Nullable<Server>>;
constructor(protocol: 'https' | 'http', port: number, routes: IRoutes, viewsPath: object, viewParameters: string, resourcesPaths: string[], favicon: string, cors: object) {
Assert.isString('protocol', protocol);
Assert.isNumber('port', port);
Assert.satisfiesInterface ('routes', routes, ['getGETRoutes', 'getPOSTRoutes', 'getPUTRoutes', 'getPATCHRoutes', 'getDELETERoutes']);
Assert.isObject('viewsPath', viewsPath);
Assert.isString('viewParameters', viewParameters);
Assert.isStringArray('resourcesPaths', resourcesPaths);
Assert.isString('favicon', favicon);
Assert.isObject('cors', cors);
this._protocol = protocol;
this._port = port;
this._routes = routes;
this._viewsPath = viewsPath;
this._viewParameters = viewParameters;
this._resourcesPaths = resourcesPaths;
this._favicon = favicon;
this._cors = cors;
this._app = new Subject<Nullable<express.Application>>(null);
this._eventEmitter = new EventEmitter();
this._server = new Subject<Nullable<Server>>(null);
}
public async start() {
const app = this._app.value = express();
app.use(morgan('common'));
this._server.value = http.createServer(app);
this._server.value.listen(this._port, () => {
this._logger.info(`Server is running on port ${this._port}`);
});
}
public on(event: string, handler: () => void): void {
Assert.isNonEmptyString('event', event);
Assert.isFunction('handler', handler);
this._eventEmitter.on(event, handler);
}
}

View File

@@ -0,0 +1,8 @@
export default interface IRoutes {
getGETRoutes(): Record<string, () => (Promise<void> | void);
getPOSTRoutes(): Record<string, () => (Promise<void> | void);
getPUTRoutes(): Record<string, () => (Promise<void> | void);
getPATCHRoutes(): Record<string, () => (Promise<void> | void);
getDELETERoutes(): Record<string, () => (Promise<void> | void);
}