Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vert.x Web DSL routing #91

Open
vietj opened this issue Nov 11, 2018 · 17 comments
Open

Vert.x Web DSL routing #91

vietj opened this issue Nov 11, 2018 · 17 comments

Comments

@vietj
Copy link
Contributor

vietj commented Nov 11, 2018

The new Kotlin stack integration in 3.6 allows to have custom Kotlin DSL's.

It would be great to have a Kotlin DSL for Vert.x Web that makes templating more Kotlin-ish.

@gmariotti
Copy link
Contributor

do you have an example on how you think it should look like? Also, should it be part of the vertx-lang-kotlin module, autogenerate or a completely new module?

@vietj
Copy link
Contributor Author

vietj commented Nov 13, 2018

I am not a Kotlin expert and I leave that up to the community to come up with the best API that makes sense.

It should be custom code part of vertx-lang-kotlin without generation, just plain Kotlin simple integration code that makes Vert.x applications more Kotlin-ish.

@gmariotti
Copy link
Contributor

cool, I'm curious to try it, even just to give inspiration to others

@AlexeySoshin
Copy link
Contributor

I did something similar for one of my own Vert.x+Kotlin projects. Will try to come up with a proposal in the next week or two.

@vietj
Copy link
Contributor Author

vietj commented Dec 30, 2018

it would be great to involve also the Kotlin Vert.x slack channel for feedback :-)

@JohannesLichtenberger
Copy link

JohannesLichtenberger commented Jan 7, 2019

A proper coroutine handler would be great, supporting suspending functions. Workaround currently is to write an extension function.
Furthermore the route definitions could be as in Ktor:

route("a") { // matches first segment with the value "a"
  route("b") { // matches second segment with the value "b"
     get {…} // matches GET verb, and installs a handler 
     post {…} // matches POST verb, and installs a handler
  }
}

@oripwk
Copy link

oripwk commented Jan 17, 2019

@JohannesLichtenberger +1.

I cannot stress enough the importance of coroutine-enabled io.vertx.ext.web.Route#handler methods. The current workaround of manually using an extension function is not clean.

@gmariotti
Copy link
Contributor

@JohannesLichtenberger this is quite an interesting problem. Since 0.26.0, you need an instance of CoroutineScope to be able to start a coroutine, however, having coroutineHandler as an extension on Route, would require the scope to be passed as a parameter. An option would be to have a RouterK/RouteK that behaves exactly like the Router/Route except that it implements CoroutineScope, similar to what we do in CoroutineVerticle, and that expose a Route.coroutineHandler extension. What do you think?

@JohannesLichtenberger
Copy link

Currently I'm using this extension function:

private fun Route.coroutineHandler(fn: suspend (RoutingContext) -> Unit): Route {
        return handler { ctx ->
            launch(ctx.vertx().dispatcher()) {
                try {
                    fn(ctx)
                } catch (e: Exception) {
                    ctx.fail(e)
                }
            }
        }
}

So, this doesn't work anymore? But sounds great, implementing CoroutineScope.

@gmariotti
Copy link
Contributor

It still works in 3.6 as long as the extension is defined inside of a CoroutineVerticle, where the scope of the verticle is used. If you try to move it out, you'll see that the code doesn't compile anymore because launch is now an extension function of CoroutineScope

@Tim-Britton
Copy link
Contributor

Hello All,

I am currently working on a dsl for our internal use and I wanted to show it off a bit determine whether it would be something good to give back to the the vertx project.

fun main(): Unit {
    val vertx: Vertx = Vertx.vertx();

    server(vertx) {

        router("/v1") {
            //Create a route at localhost:8080/v1/hello
            getAwait("hello") {
                //this is a coroutine handler
                
                delay(500)
                it.response().end(jsonObjectOf("hello" to "world").toBuffer())
            }

        }

    }.listen(8080)
}

Its lightly inspired based on the dsl for koin which we use for dependency injection.

If interested I can start looking at getting the dsl code together for contribution.

@vietj
Copy link
Contributor Author

vietj commented Aug 28, 2019

that would be great @Tim-Britton

@JohannesLichtenberger
Copy link

Could we get rid of the "Await" method postfix? :-) I know it's otherwise for the Vert.x calls auto generated, but then it would be in-line with the Ktor routes, I think.

@vietj
Copy link
Contributor Author

vietj commented Aug 28, 2019 via email

@JohannesLichtenberger
Copy link

Ah okay..., but looks great :-)

@vietj
Copy link
Contributor Author

vietj commented Aug 28, 2019

we also discussed that with vertx 4, the new Future API should improve coroutines API

@Tim-Britton
Copy link
Contributor

@JohannesLichtenberger I use the await postfix to inform the user that they are getting a coroutine context rather than a standard handler context.

@vietj we'll start moving in that direction then! The implementation details require delegation similar to what is used with the rx versions of the vertx classes.

I'll try to block out some time to get this started!

@Tim-Britton Tim-Britton mentioned this issue Aug 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

6 participants