Since Vert.x architecture is asynchronous and non-blocking, you should never block a thread in your application code. If you have to use some blocking code (e.g. blocking I/O, JDBC) always use the dedicated coroutine dispatcher.
Vert.x services are deployed as "verticles". Main verticle is the application starting point. It's used to initialize and configure application modules and features. Minimal implementation for Kilua RPC integration contains initRpc and applyRoutes function calls.
When using manual service registration, you call initRpc with a lambda function, which binds interfaces with their implementations. Different overloads of registerService function allow injecting server objects into your service classes.
When using Koin, you call initRpc with a list of Koin modules. Constructor parameter injection is automatically supported by Koin.
import dev.kilua.rpc.applyRoutes
import dev.kilua.rpc.getAllServiceManagers
import dev.kilua.rpc.getServiceManager
import dev.kilua.rpc.initRpc
import io.vertx.core.AbstractVerticle
import io.vertx.ext.web.Router
import org.koin.core.annotation.ComponentScan
import org.koin.core.annotation.Module
import org.koin.ksp.generated.module
@Module
@ComponentScan
class AddressModule
// val addressModule = module { // manual Koin module declaration
// factoryOf(::AddressService)
// }
class MainVerticle : AbstractVerticle() {
override fun start() {
val router = Router.router(vertx)
val server = vertx.createHttpServer()
vertx.initRpc(router, server, getAllServiceManagers(), null, AddressModule().module)
vertx.applyRoutes(router, getServiceManager<IAddressService>())
vertx.createHttpServer().requestHandler(router).listen(8080)
}
}
If you are using websockets, you need to use a special version of initRcp function, and pass a HttpServer instance and a list of all services declaring websocket channel connections.
class MainVerticle : AbstractVerticle() {
override fun start() {
val router = Router.router(vertx)
val server = vertx.createHttpServer()
vertx.initRpc(router, server, getAllServiceManagers()) {
registerService<IAddressService> { AddressService() }
}
server.requestHandler(router).listen(8080)
}
}
Security
You can use standard Vert.x configuration to add authentication and authorization to your application. You can use serviceRoute extension function to apply your AuthHandler to the selected Kilua RPC services.
You can use constructor parameters to inject server objects - RoutingContext , Vertx and ServerWebSocket (see chapter) into your service classes. These objects give you access to the application configuration, its state, the current request and the user session.