multiple request method on one path

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

multiple request method on one path

stasmarkin
I want assign different request handlers for one path based on request method. For example, a have '/mail/templates' path. So, on POST:/mail/templates I need to save data, but on GET:/mail/templates I expect to get list on templates.

My first approach was:
RatpackServer.start(server -> server.handlers(chain -> chain
    .get("/mail/templates", controller::list)
    .post("/mail/templates", controller::save)

But it didn't work (it gives 405 error on POST method)

In previous version of ratpack there was .handler() method in Chain, so I could do something like this:
chain.handler("/mail/templates", ctx -> ctx.byMethod(action -> action
    .get(controller::list)
    .post(controller::save)


But now I have to do that:
                        .prefix("mail/templates", action -> action.all(ctx -> ctx.byMethod(method -> method
                                .get(() -> templatesController.list(ctx))
                                .post(() -> templatesController.newMailTemplate(ctx)))))

It looks really too monstrous. Are there prettier way to assign different handlers depends on request method ?
Reply | Threaded
Open this post in threaded view
|

Re: multiple request method on one path

danhyun
You should be able to achieve the same effect via

.path("mail/templates", ctx -> ctx.byMethod(method -> method
                                .get(() -> templatesController.list(ctx))
                                .post(() -> templatesController.newMailTemplate(ctx))))
Reply | Threaded
Open this post in threaded view
|

Re: multiple request method on one path

stasmarkin
danhyun wrote
You should be able to achieve the same effect via

.path("mail/templates", ctx -> ctx.byMethod(method -> method
                                .get(() -> templatesController.list(ctx))
                                .post(() -> templatesController.newMailTemplate(ctx))))
Thanks. that's better than my previous decision.
But still uglier than it was in 0.9.*
Reply | Threaded
Open this post in threaded view
|

Re: multiple request method on one path

danveloper
Administrator
Just curious, uglier in what way? The only change is `handler(..)` => `path(..)`.

Thank you
Reply | Threaded
Open this post in threaded view
|

Re: multiple request method on one path

stasmarkin
danveloper wrote
Just curious, uglier in what way? The only change is `handler(..)` => `path(..)`.

Thank you
There were get/post/...(Handler handler) methods in ByMethodSpec, so you could use java8 method references like that:
.path("mail/templates", ctx -> ctx.byMethod(method -> method
                                .get(controller::list)
                                .post(controller::add)

In current ByMethodSpec there are methods with Block argument only, so I have to 'unfold' method reference into lambda function:
.path("mail/templates", ctx -> ctx.byMethod(method -> method
                                .get(() -> controller.list(ctx))
                                .post(() -> controller.add(ctx))
Reply | Threaded
Open this post in threaded view
|

Re: multiple request method on one path

Luke Daley
Administrator
It's a good point.

https://github.com/ratpack/ratpack/issues/773

We'll add Handler overloads back once the Groovy issue linked to there is fixed.