initial commit

This commit is contained in:
2025-09-05 18:06:18 -04:00
commit 7733ef2488
11 changed files with 2256 additions and 0 deletions

9
server/eslint.config.ts Normal file
View File

@@ -0,0 +1,9 @@
import js from "@eslint/js";
import globals from "globals";
import tseslint from "typescript-eslint";
import { defineConfig } from "eslint/config";
export default defineConfig([
{ files: ["**/*.{js,mjs,cjs,ts,mts,cts}"], plugins: { js }, extends: ["js/recommended"], languageOptions: { globals: globals.node } },
tseslint.configs.recommended,
]);

26
server/package.json Normal file
View File

@@ -0,0 +1,26 @@
{
"name": "server",
"version": "0.0.0",
"description": "A server for the chatting rooms",
"license": "ISC",
"author": "Alexander Zinn",
"type": "module",
"main": "index.js",
"scripts": {
"dev": "nodemon --watch 'src/**/*.ts' --exec 'ts-node-esm' src/index.ts"
},
"dependencies": {
"@techniker-me/logger": "0.0.15"
},
"devDependencies": {
"@eslint/js": "9.35.0",
"@types/node": "^20.0.0",
"eslint": "9.35.0",
"globals": "14.0.0",
"jiti": "2.5.1",
"nodemon": "^3.1.0",
"ts-node": "^10.9.0",
"typescript": "^5.0.0",
"typescript-eslint": "8.42.0"
}
}

29
server/src/Server.ts Normal file
View File

@@ -0,0 +1,29 @@
import {LoggerFactory} from '@techniker-me/logger';
import {createServer as createHttpServer, type IncomingMessage, type ServerResponse, type Server as HTTPServer, type ServerOptions as HTTPServerOptions} from 'node:http';
export class Server {
private readonly _logger = LoggerFactory.getLogger('Server');
private readonly _http: HTTPServer;
constructor(serverOptions: HTTPServerOptions) {
this._http = createHttpServer(serverOptions, this.httpRequestHandler.bind(this));
}
public async listen(port: number, callback?: (port: number) => void) {
await new Promise<void>((resolve, reject) => {
this._http.listen(port, () => {
resolve();
});
this._http.on('error', (error) => {
reject(error);
});
});
callback?.(port);
}
private httpRequestHandler(req: IncomingMessage, res: ServerResponse) {
this._logger.info(`Received request: ${req.url}`);
res.end('Hello World');
}
}

View File

@@ -0,0 +1,12 @@
import {Server} from './Server.ts';
import { type ServerOptions as HTTPServerOptions } from 'node:http';
export class ServerFactory {
public static createServer(serverOptions: HTTPServerOptions): Server {
return new Server(serverOptions);
}
private constructor() {
throw new Error('ServerFactory is a static class and cannot be instantiated');
}
}

10
server/src/index.ts Normal file
View File

@@ -0,0 +1,10 @@
import {ServerFactory} from './ServerFactory.ts';
const server = ServerFactory.createServer({
keepAlive: true,
keepAliveTimeout: 10000,
});
server.listen(3000, (port: number) => {
console.log(`Server is running on port [${port}]`);
});

17
server/tsconfig.json Normal file
View File

@@ -0,0 +1,17 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"module": "nodenext",
"moduleResolution": "nodenext",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"allowImportingTsExtensions": true,
"noEmit": true
},
"include": ["src"],
"exclude": ["node_modules", "dist"],
"ts-node": {
"esm": true,
"experimentalSpecifierResolution": "node"
}
}