-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
standardized controller/policies API #89
Comments
How about run a function before web server was configure to check controller and if they extends a |
One idea might be to use the builtin App using Hapi as Webserverconst HapiController = require('trailpack-hapi/lib/HapiController')
class ExampleController extends HapiController {
exampleHandler (request, reply) {
// ... hapi handler
}
} The "Trails Controller"I think this is a good abstraction, but I want to emphasize in the documentation that it's only really necessary to use this pattern when building trailpacks. I don't want to signal to potential users that they might have to learn some new API to use Hapi or Express or whatever, when hapi and express already have perfectly good APIs that these potential users probably already know. @jaumard I think since the trails router is based on hapi, I think it might make sense to model this Controller interface after hapi as well. Now we have something like this: Custom Trailpack that needs to work with all webservers, e.g. swagger:const Controller = require('trails-controller')
class SwaggerController extends Controller {
exampleHandler (request, response) {
// implementation according to standard trails controller api
}
} Then it's the responsibility of the webserver trailpacks to implement the conversions. This would be easy for hapi, since the interface can be the same on both sides. Even though we'd have to build translation layers for express4, koa, et al, I think this is preferable to requiring all trailpack authors to build-in support for each webserver individually. |
I'm ok with the fact the standardized controller should be learn/use only for trailpack. That's a good thing. In the doc we can only mention Trails controller on Trailpack part and never on Trails :) Hapi(Express|Koa)Controller are not really needed... Controllers are map directly with web server so no need to have a specific class controller right ? So if I understand you, under trailpack-express4 (for example) I need wrap all SwaggerController methods (cause it extends trails controller) and transform the express |
Here my interface proposal :
We can create 2 classes for them (in trailpack-webserver) and each web server trailpack extends them to be compatible. |
I think it might make sense to try to re-use the hapi interface as our own generic interface, and conform other webserver trailpacks to it. This gives us three immediate benefits:
There will be a slight performance hit on every request to go through this additional layer. If we use the hapi interface, then at least for the hapi trailpack, the performance decrease will be zero, since it can be a direct passthrough. |
I don't like hapi interface but yeah ^^ it make sens to do it this way. I will start implement this in express4 and Controller template (https://github.com/trailsjs/generator-trails/blob/master/src/controller/templates/Controller.js) to remove the extends or all controller will be interpreted as Trails Standard Controller. |
Sounds good. Honestly the hapi API isn't my favorite either (I don't know why |
Ok cool let's go on this ! :D we will be hapi to trust it ;) lol |
@tjwebb ok first problem ^^ if controllers didn't extends trails-controller |
Another thing, if I use this on trailpack-express4 :
I suppose On my unit test all pass cause controllers are instance of Did you have a way to fix this ? Or an idea ? |
Ok for now I use this to detect if Controller is a
I was able to run swagger Trailpack on express4 without problem :) that's a start ^^ |
Cool! I think this kind of method may be useful in other trailpacks as well. Would you mind adding this to lib/trails.js and adding a couple tests? I'm planning to expose a |
@tjwebb Yeah sure in witch repo you want me to add this ? But in order to do it we need to create the |
@tjwebb: are you ok if I create a repo Related issue : trailsjs/trailpack-router#47 who block me to continue this. |
So the solution for this is to create a dummy Request/Response object with same interface as hapi and under the hood each web server will implement his own API. I don't know hapi interface enough to create those interface but if someone can help me create them I can implement them into express and we'll have a unified controllers/policies for hapi/express. #needHapiDev ^^ |
Blocked by trailsjs/trailpack-express#42 |
Hey all, thanks for contributing your thoughts. We've been able to think about this awhile, and I've tried to see how this would fit into the overall strategy of trails. After reading through these threads again, I think this kind of interface remains out of scope for the project. It'd certainly be nice if our controllers/policies were re-usable, but this re-usability would not come for free, and I think the cost may not be worth the benefit. Re-usable logic can and should be implemented in Service classes. In designing our systems, it is sometimes desirable for some components to be disposable in order to maintain simplicity and focus, and I think that is the case here. |
@tjwebb I understand your decision about this common interface and you're right. But in that case we should be able to provide multiple version of controllers/policies under trailpacks to be compatible with multiple server. For example I can provide express and hapi controllers/policies under my trailpack and it will take the correct one to load depending on the one installed on the project, and both controllers/policies will use the common re-usable services. For me it's an acceptable solution. But currently we don't have any way to make a trailpack compatible with both easily and it's a shame in my opinion. Should I open a new issue about this ? |
Maybe a trailpack could solve this problem. We could create a trailpack-bridge that would implement a standardized api between common node servers. Then, developers could choose to create a trailpack that ties into this trailpack-bridge api. In order to use trailpacks that use the trailpack-bridge api, you would have to install the trailpack-brigde, but it would be a compromise that would solve this problem while still following the Trailjs design philosophy, keeping Trailjs a simple, minimal, framework like it was designed to be. |
@jamrizzi the chosen solution to have a trailpack-controller and trailpack-policy was the best approach having a trailpack that do this purpose is overkill as it's just two classes. |
Could you rephrase this response. I didn't fully understand it. |
Sorry for my english :) I started to try to create a wrapper hapi to express for trailpack-express but didn't manage to do it. Also it will be a bit painful to maintain when hapi or express change there API. That's why I think that embedded multiple versions is more the way to go here. |
Thanks for the explanation. I think I'm understanding it. What do you mean by "embedded multiple versions". So, when creating a trailpack, if I build it using the hapi interface, it will work on express??? |
I mean under the trailpack you will have MyControllerExpress and MyControllerHapi. |
Is that currently implemented? If not, how can I help? |
No nothing is implemented ^^ it will touch some core feature so the @trailsjs/maintainers team should be ok to do something for this. |
In order to make all Trailpacks compatible whatever the web server choice, we need a unified API for controllers and policies.
But actually controllers/policies are bind directly to web server so I don't see how we can implement this :/.
Any idea ?
The text was updated successfully, but these errors were encountered: