tsframework / ts-framework

A Web Framework for Nodejs
http://tsframework.github.io
MIT License
43 stars 6 forks source link

Proposal: Controller and Router refactor #26

Closed atrakeur closed 8 years ago

atrakeur commented 8 years ago

I post a proposal for Router and Controller refactoring. Here are the different parts of a request processing chain. We have to take care to keep the whole chain asynchronous (this is not the case with the actual code!).

The first part is routing request to actual handling code: I propose to keep the already implemented action anotations because it leave Controllers loosely coupled with the Router. It also keep the responsibility to the router to handle controller instantiation/release. The router must also handle a (configurable) timeout before auto closing a request with an error (to avoid bad async code going nowhere)

The second part is the actual Controller: This part is public and must be implemented by the end user in his application. Because the code must be async, I suggest to instantiate one controller per request to avoid accidental property sharing. The controller must expose to the end-user properties like request/responce. It must also provide a helpers methods to populate request. Finaly, it must also provide a method to auto send (terminate) the responce to the client.

Lastly the responce must be handled by our application. For now we will just handle basic requests like json or Raw content data. The code must be extensible to others content (like html from a template renderer).

My intent is to implement two sorts of Controllers: HttpControllers that handle http requests, and ConsoleControllers that handle console requests (imagine something like tsfw generate database?). ConsoleControllers are out of the question for now but I think the naming is pretty good even if we never implement these.

Okay so here is my proposal:

I'm actually working on a PR to show this implementation working. However feel free to discuss and add your opinion here.

jhonmike commented 8 years ago

I liked and I think all valid items for me! :+1: