pushpull
07/11/2026, 11:10 AMUserResource with koin
fun Route.userResource() {
val userResource: UserResource by inject()
val logger = noCoLoggerUserResource()
authenticate(SpuroAuthenticator.AUTH_JWT, SpuroAuthenticator.AUTH_API_KEY) {
getUserRoute.Ping { path ->
///...
}
}
}
///
routing {
userResource()
}
Is there any better way to pass UserResoure except than userResource(userResource, ...other dependency)
codeberg.org/spuro/…/UserResource.ktsimon.vergauwen
07/13/2026, 7:11 AMKoin class into Ktor's attributes and then search for the dependency through look-up. Afaik Metro doesn't offer this lookup mechanism but you can still inject Graphs into your routes if you want doing something like this:
interface AppGraph
val AppGraphKey = AttributeKeyAppGraph("AppGraph")
fun Application.install(graph: AppGraph) {
attributes[AppGraphKey] = graph
}
fun Route.graph() = lazy { attributes[AppGraphKey] }
// Get a graph extension or specific dependency
fun A Route.graph(block(): AppGraph.() -> A) = lazy { block(attributes[AppGraphKey]) }
I personally prefer explicit injection rather than lookup so if you're using Metro for the convenience of auto-wiring your graph I would suggest exposing specific "modules" for each route". In your example:
@DependencyGraph
interface AppGraph {
val users: UserModule
// ...
}
@GraphExtension
interface UserModule {
val userResource: UserResource
val logger: Logger
val somethingElse: SomethingElse
}
And then pass userResource(users: UserModule), or do val module by graph { it.userModule } or something like this.
You can elaborate on this when you need Application in your AppGraph using a .Factory.
@DependencyGraph
interface AppGraph {
val app: Application
@DependencyGraph.Factory
fun interface Factory {
fun create(@Provides app: Application): AppGraph
}
}
val AppGraphKey = AttributeKeyAppGraph("AppGraph")
fun Application.install(graph: (Application) -> AppGraph) {
attributes[AppGraphKey] = graph
}
If you're using route scoped dependencies you can so something similar.
@GraphExtension
interface RouteGraph {
val ctx: RoutingContext
@DependencyGraph.Factory
fun interface Factory {
fun create(@Provides ctx: RoutingContext): RouteGraph
}
}
val RouteGraphKey = AttributeKeyRouteGraph("RouteGraph")
val RoutingContext.routeGraph: RouteGraph =
call.attributes.computeIfAbsent(RouteGraphKey) { application.graph().routeGraphFactory(this) } }
That being said I personally prefer manually passing the instances even when using auto-wiring with Metro since Ktor's flexible DSL works really well with the explicit style but it's useful to group your dependencies in sub graphs or modules.pushpull
07/13/2026, 3:05 PMrouting {
appGraph.setupResource.buildRoute()
appGraph.userResource.buildRoute()
}
//------------------------
class SetupResource(
private val appConfig: AppConfig,
private val usersManager: UsersManager
) {
context(route: Route)
override fun buildRoute() {
with(route) {
getSetupRoute.Ping {
val response = this@SetupResource.ping()
call.respond(apiResponse = response)
}
}
}
}simon.vergauwen
07/14/2026, 6:45 AMwith(route) { } and any function using context(route: Route) within the function will still work fine.