Verticles

Now come across a problem, though. Our code resides in the Main.kt file, which grows bigger and bigger. We can start splitting it by using verticles.

You can think of a verticle as a lightweight actor.  Let's see an example; look at the following code:

class ServerVerticle: CoroutineVerticle() {

override suspend fun start() {
val router = router()
vertx.createHttpServer().requestHandler(router::accept).listen(8080)
}

private fun router(): Router {
val router = Router.router(vertx)
// Our router code comes here now
...
return router
}
}

Now we need to start this verticle. There are different ways of doing that, but the simplest way is to pass the instance of this class to the deployVerticle() method:

vertx.deployVerticle(ServerVerticle())

Now our code is split into two files, ServerVerticle.kt and Main.kt.

Notice, though, how /api/v1/cats/ is repeated every time. Isn't there a way to remove that redundancy? Actually, there is. And it's called subrouter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.60.62