Routing
Like many RPC systems, our routing is based around the idea of defining an endpoint, specifying the methods that can be called remotely with their parameters and return types. We use data transfer object classes (DTO) in the common Kotlin module as the Interface Definition Language (IDL) for describing both the endpoint interface and the structure of the payload messages.
object HelloWorldEndPoint :
EndPoint<HelloWorldEndPoint.Request, HelloWorldEndPoint.Response>() {
class Request(
val data: String? = null,
)
class Response(
val data: String? = null,
)
}
Then, you have to specify the end point on the common routing DSL.
object Routing {
//...
fun init() = api {
"api" {
"v1" {
"hello_world" {
route(HelloWorldEndPoint)
}
}
}
}
//...
}
Finally, you have create the endpoint Handler in the jvm module
@CommonHandler
class HelloWorldController :
EndpointHandler<HelloWorldEndPoint.Request, HelloWorldEndPoint.Response>(
HelloWorldEndPoint.Request::class,
HelloWorldEndPoint
) {
override fun process(
requestDto: HelloWorldEndPoint.Request,
clientData: AuthorizedClientData?,
): DataResponse<HelloWorldEndPoint.Response> {
return DataResponse(
HelloWorldEndPoint.Response(data = "You sent: ${requestDto.data}")
)
}
}
Now, you can call the endpoint from the JS module using an extension function called remote
fun main() {
GlobalScope.launch {
val response = HelloWorldEndPoint.remoteProcess(HelloWorldEndPoint.Request(data = "Hi!"))
println(response.data)
//output: "You sent: Hi!"
}
}