Reputation: 7799
I defined my routes in the separate file:
PostRoutes.kt:
fun Route.getPostsRoute() {
get("/posts") {
call.respondText("Posts")
}
}
// Some other routes
fun Application.postRoutes() {
routing {
getPostsRoute()
// Some other routes
}
}
And I setup these routes in Application.kt as it shown below:
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module(testing: Boolean = false) {
routing { // I want to provide the root endpoint (/api/v1) here
postRoutes()
}
}
How can I setup my root endpoint (/api/v1
) in this case?
P.S. I've checked their docs, it says to use nested route
s but I can't because I need to call routing
in postRoutes()
that breaks nested routes.
P.P.S. I am a noobie in Ktor and Kotlin.
Upvotes: 0
Views: 1379
Reputation: 6999
You can either wrap the getPostsRoute()
with the route("/api/v1")
inside the postRoutes
method or get rid of the postRoutes
method and nest your routes inside the routing {}
.
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
fun main() {
embeddedServer(Netty, port = 5555, host = "0.0.0.0") {
postRoutes()
}.start(wait = false)
}
fun Route.getPostsRoute() {
get("/posts") {
call.respondText("Posts")
}
}
fun Application.postRoutes() {
routing {
route("/api/v1") {
getPostsRoute()
}
}
}
Upvotes: 2