How do you structure and setup your routes in the ...
# ktor
a
How do you structure and setup your routes in the Ktor server? I wonder if there is built-in way to do it like express js framework
b
You can check out https://ktor.io/docs/routing-in-ktor.html It's expressed pretty similar to express js
a
I meant the Router pattern, you create a instance of a class, register all of your routes inside that instance and use this instance in the routing {} block to register them all
I wonder if it built-in into Ktor server, if not then guess I will have to build it myself :)
b
Generally people just use extension methods for this sort of thing, though I guess it's a personal preference on how you prefer to structure your routing code. I don't believe there's a common approach that adheres to exactly what you're describing.
1
a
If you are using Koin with tons of services then you have to pass them all the time to all the functions to where you need them
Because you can't inject them there in the Route extension function
a
Why not?
a
Because Koin support injecting only in the Application scope
a
https://insert-koin.io/docs/reference/koin-ktor/ktor
Koin inject() and get() functions are available from Application,Route,Routing classes:
a
I just tried it out and yes I can, maybe that's a feature in the new releases and I was using an old version back then or I misunderstood
👍 1
You can't inject it in the get or any http method handler though
a
What are you lacking if you inject dependencies outside of the route handler itself? IMO that makes a lot of sense, in this example at least