Verticles
As our project progresses, the server.kt
file, containing our current code, is growing increasingly large. To manage this, we need to separate different parts of the code. In Vert.x, this can be accomplished by organizing the code into distinct classes known as “verticles.”
You can think of a verticle as a lightweight actor. We discussed actors in Chapter 5, Introducing Functional Programming.
Let’s see how we can create a new verticle that will encapsulate our server:
class ServerVerticle : CoroutineVerticle() {
override suspend fun start() {
val router = router()
vertx.createHttpServer()
.requestHandler(router)
.listen(8081)
println("open http://localhost:8081/status")
}
private fun router(): Router {
// Our router code comes here now
val router = Router.router(vertx)
...
return router
}
}
Every verticle has a start()
method...