-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): implement route handling and middleware execution in WebS…
…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
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |