Skip to content

Commit

Permalink
feat(core): implement route handling and middleware execution in WebS…
Browse files Browse the repository at this point in the history
…erver

- Added lifecycle method to handle incoming HTTP requests and route them based on path and method.
- Integrated dynamic route matching with support for dynamic segments in URLs.
- Implemented middleware execution before invoking route handlers.
- Updated `WebServer` initialization to bind the `lifecycle` method to the `request` event.
- Enhanced logging for debugging route handling and middleware processing.

The new implementation ensures that routes are correctly matched and handlers are executed with appropriate middleware.
  • Loading branch information
m-mdy-m committed Aug 16, 2024
1 parent 4931f27 commit dbbe774
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/core/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { IncomingMessage, Server, ServerResponse } from 'http';
import { Gland } from '../types/gland';
import { ServerTools } from '../helper';
import { WebContext } from './context';
import Reflect from '../metadata/metadata';
import { routes } from './router';
import { SafeExecution } from './decorators/SafeExecution';
export class WebServer extends Server implements Gland.Listener {
constructor() {
super();
}
@SafeExecution()
lifecycle(req: IncomingMessage, res: ServerResponse) {
const { ctx } = new WebContext(req, res);

const path = req.url!;
const method = req.method!;
console.log('path:', path);
console.log('method:', method);

// Iterate over registered routes
for (const [routePath, controller] of routes.entries()) {
if (path.startsWith(routePath)) {
const routeInstance = new controller();
const keys = Object.getOwnPropertyNames(Object.getPrototypeOf(routeInstance)).filter((key) => key !== 'constructor');
console.log('keys:', keys);
for (const key of keys) {
const handlerMethod = Reflect.get('method', controller.prototype, key);
const handlerPath = Reflect.get('path', controller.prototype, key);
const fullRoutePath = handlerPath ? routePath + handlerPath : routePath;
console.log('handlerMethod:', handlerMethod);
console.log('handlerPath:', handlerPath);
console.log('fullRoutePath:', fullRoutePath);
if (handlerMethod === method && fullRoutePath === path) {
const handler = routeInstance[key].bind(routeInstance);
const mid = Reflect.get('middlewares', controller.prototype, key) || [];
const classMids = Reflect.get('classMiddlewares', controller.prototype) || [];

// Execute middlewares
[...classMids, ...mid].forEach((middleware: Function) => middleware(ctx));

// Execute the route handler
handler(ctx);
console.log('Response should be sent now.');
return;
}
}
}
}
}
init(opts: Gland.ListenOptions, listeningListener?: (info: Gland.ListenOptions) => void): void {
this.on('request', this.lifecycle.bind(this));

const listener = ServerTools.listener(opts, listeningListener);
if (opts.logger) {
ServerTools.log(opts);
}
if (opts.path) {
this.listen(opts.path, opts.backlog, listener);
} else {
this.listen(opts.port, opts.host, opts.backlog, listener);
}
}
}
export const g = new WebServer();

0 comments on commit dbbe774

Please sign in to comment.