https://kotlinlang.org logo
Title
h

hyukchan

04/25/2019, 4:20 PM
Hi guys, I'm building a server side application. I would like to know what is the best practice to avoid having a big routing resource file ? How can we divide ? Imagine I have those urls for my API: GET /animal POST /animal GET /animal/:id POST /animal/:id and 20 URLs more starting by /animal.... I think having a "AnimalResource.kt" for all those url is not the best practice. How can I do ?
k

Kevin Schmeichel

04/25/2019, 5:14 PM
I found this article pretty helpful:
l

luke

04/25/2019, 5:15 PM
h

hdarritchon

04/26/2019, 8:44 AM
Myself I use extension functions on Routes to do the job.
install(Routing) {
        val konfig = HoconKonfigAdapter()
        val contextPath = konfig.get("ktor.deployment.context-path")
        route("$contextPath/api/v1") {
            val registry = feature(Metrics).registry

            healthEndPoints()
            metricsEndPoints(registry)
            routeWithMeasureTime {
                catalogSiEndPoints()
                reunionCatalogEditoEndPoints()
                telesurveillanceCatalogEditoEndPoints()
                catalogLegacyEndPoints()
            }
        }
    }
And the extension function looks like, each extension function in a separate kt file :
@KtorExperimentalLocationsAPI
fun Route.healthEndPoints() {
    get<HealthEndpoint> {
        call.respond(Health(HealthCheck.systemIs.state))
    }
}
Quite easy and simple.
k

Kevin Schmeichel

04/26/2019, 5:40 PM
a bit off topic, but when I try and use Locations like above, I get an error:
No type arguments expected for fun Route.get(...)
any idea what I'm doing wrong?