SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

Custom logic around handlers? #29

Closed JasonKissinger-MDT closed 9 years ago

JasonKissinger-MDT commented 9 years ago

Is there an extension point for us to add custom logic around the existing handlers? After a PostCollection call, I'd like to send a message to a queue to evaluate the new content. Initial thought was to extend PostCollectionHandler and pushing that onto the request dispatcher, but that's doesn't seem clean.

ujibang commented 9 years ago

you can pipe a so called ApplicationLogic handler

check configuration documentation http://restheart.org/docs/configuration.html#conf-applicationlogic

you can also refer to this blog post http://www.softinstigate.com/blog/2015/01/15/restheart-stop-deploying-and-enjoy-piping/

JasonKissinger-MDT commented 9 years ago

thanks Andrea. is the idea that I should create a new /_logic endpoint with my new pipe? and that existing 'semantic' pipes are not open for applicationlogichandler customizations? my initial design was to add additional logic when a resource is added to a collection, so that POST /db/mycollection of my resource performed some additional logic when a resource is persisted. The users of my REST apis need not be aware of where I've added logic.

I've gotten this working by replacing the existing PostCollectionHandler with my own. But it doesn't feel right. I was hoping to plug into and not replace this pipe:

    requestDispacherHandler.putPipedHttpHandler(RequestContext.TYPE.COLLECTION, RequestContext.METHOD.POST, new RequestTransformerMetadataHandler(new MyHandler()));

and then my handler with before/after hooks

class MyHandler extends PostCollectionHandler {
    void handleRequest(HttpServerExchange exchange, RequestContext context) throws Exception {
        beforePost()
        super.handleRequest(exchange, context)
        afterPost()
    }
}
ujibang commented 9 years ago

I see what you mean. We have in the roadmap to allow defining web hooks via collection metadata and this would solve elegantly your need. Unfortunately we haven't yet added this feature.

May I suggest you to add your custom handler extending PostCollectionHandler as an ApplicationHandler so that you don't change the code (and will be able updating RESTHeart seamlessly)

In that case you would have to POST /_logic/notifyingcollection to get the notifications, however you would still be able to POST /db/notifyingcollection with usual semantic.

ujibang commented 9 years ago

Of course you'll need to pipe the whole handlers chain.....

mkjsix commented 8 years ago

https://softinstigate.atlassian.net/browse/RH-73