Ktor Integration Plugin
The KotlinInjectPlugin bridges IDK's Metro DI system with Ktor's request pipeline. It is not an application-level service in its own right, but the foundation that all other IDK HTTP services run on. Every IDK HTTP service depends on this plugin being installed.
What It Does
On every incoming request, the plugin runs a UserContextInterceptor before your route handlers:
- Resolves the tenant from the request via
TenantResolver(default: reads theX-Tenant-IDheader, falls back to"default") - Resolves the principal from the request via
PrincipalResolver(default: checksX-User-IDheader, then Ktor auth plugin, falls back to"anonymous") - Creates or retrieves the
UserContextInstancefor that tenant+principal pair - Creates a
SessionInstancescoped to this specific request - Makes all session-scoped services available to route handlers through
call.getSessionService<T>(),call.getUserService<T>(), andcall.getAppService<T>()
All of this happens automatically. By the time your handler runs, the DI scopes are set up and every service is ready to use.
Auto-Discovery of Adapters
When you call installUniversalHttpAdapters(), the plugin discovers all CommandBackedHttpAdapter instances contributed to the DI graph and mounts them as routes. This is how services register their endpoints without manual route wiring. Each adapter declares a mount point and a list of endpoint commands; the dispatcher indexes them and routes incoming requests by path and HTTP method.
Including in Your Server
dependencies {
implementation("com.sphereon.idk:ktor-server-kotlin-inject:0.25.0")
}
Configuration
install(KotlinInjectPlugin) {
appGraph = myAppGraph
tenantResolver = JwtTenantResolver() // optional: custom tenant extraction
principalResolver = JwtPrincipalResolver() // optional: custom principal extraction
}
| Option | Default | Description |
|---|---|---|
appGraph | (required) | Your root IDK AppGraph |
tenantHeader | "X-Tenant-ID" | HTTP header for tenant ID extraction (used by default resolver) |
principalHeader | "X-User-ID" | HTTP header for principal ID extraction (used by default resolver) |
tenantResolver | DefaultTenantResolver | Custom strategy for resolving the tenant from a request |
principalResolver | DefaultPrincipalResolver | Custom strategy for resolving the principal from a request |
The plugin also integrates with the YAML configuration system on JVM. It registers property sources that load application.yml from the working directory, config directory, or classpath. These participate in the IDK configuration system at all three scope levels (app, tenant, principal).
See Ktor Integration for full configuration details, including custom resolvers, YAML scope layering, and route-level adapter mounting.
Next Steps
- Services Overview for an introduction to all available IDK services and the
CommandBackedHttpAdapterpattern - Ktor Integration Guide for full configuration details, including custom resolvers and YAML scope layering
- Dependency Injection for understanding the App/User/Session scope architecture
- KMS REST API, OAuth2 Authorization Server, and other service pages for services that build on this plugin