Ktor server core module for iOS ARM64 target providing essential server-side functionality for building asynchronous web applications and microservices in Kotlin Multiplatform
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Ktor Server Core provides the essential foundation for building asynchronous web applications and microservices in Kotlin Multiplatform projects. This module includes core server-side functionality with support for routing, request/response handling, application lifecycle management, and a comprehensive plugin architecture. Built with Kotlin coroutines for asynchronous programming, it supports multiple hosting environments and offers a DSL-based configuration approach.
implementation("io.ktor:ktor-server-core-iosarm64:3.2.0")import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.http.*
// Create and start an embedded server (requires engine implementation)
fun main() {
embeddedServer(SomeEngine, port = 8080) {
routing {
get("/") {
call.respondText("Hello, Ktor!")
}
get("/users/{id}") {
val id = call.parameters["id"]
call.respondText("User ID: $id")
}
}
}.start(wait = true)
}
// Configure application with plugins
fun Application.module() {
install(CallLogging)
routing {
route("/api") {
get("/health") {
call.respond(mapOf("status" to "healthy"))
}
}
}
}Ktor Server Core is built around several key architectural components:
Core application structure, configuration, and lifecycle management. Includes the main Application class, environment setup, and server configuration.
interface Application {
val environment: ApplicationEnvironment
val monitor: Events
val parentCoroutineContext: CoroutineContext
suspend fun disposeAndJoin()
}
interface ApplicationCall : CoroutineScope {
val attributes: Attributes
val request: ApplicationRequest
val response: ApplicationResponse
val application: Application
val parameters: Parameters
suspend fun <T> receiveNullable(typeInfo: TypeInfo): T?
suspend fun respond(message: Any?, typeInfo: TypeInfo?)
}
fun serverConfig(block: ServerConfigBuilder.() -> Unit): ServerConfigApplication and Lifecycle Management
Application configuration system supporting multiple formats and sources with hierarchical merging capabilities.
interface ApplicationConfig {
fun property(path: String): ApplicationConfigValue
fun propertyOrNull(path: String): ApplicationConfigValue?
fun config(path: String): ApplicationConfig
fun configList(path: String): List<ApplicationConfig>
fun keys(): Set<String>
fun toMap(): Map<String, Any?>
}
interface ApplicationConfigValue {
val type: Type
fun getString(): String
fun getList(): List<String>
fun getAs(type: TypeInfo): Any?
}
object ConfigLoader {
fun load(path: String? = null): ApplicationConfig
}Application engine abstraction and embedded server functionality for hosting Ktor applications.
interface ApplicationEngine {
val environment: ApplicationEnvironment
suspend fun resolvedConnectors(): List<EngineConnectorConfig>
fun start(wait: Boolean = false): ApplicationEngine
suspend fun startSuspend(wait: Boolean = false): ApplicationEngine
fun stop(gracePeriodMillis: Long, timeoutMillis: Long)
}
fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configuration>
embeddedServer(
factory: ApplicationEngineFactory<TEngine, TConfiguration>,
port: Int = 80,
host: String = "0.0.0.0",
watchPaths: List<String> = emptyList(),
configure: TConfiguration.() -> Unit = {},
module: Application.() -> Unit
): EmbeddedServer<TEngine, TConfiguration>Server Engines and Embedded Servers
Comprehensive request parsing and response building capabilities with content transformation pipelines.
interface ApplicationRequest {
val call: ApplicationCall
val pipeline: ApplicationReceivePipeline
val queryParameters: Parameters
val headers: Headers
val local: RequestConnectionPoint
val cookies: RequestCookies
}
interface ApplicationResponse {
val call: ApplicationCall
val pipeline: ApplicationSendPipeline
val headers: ResponseHeaders
val cookies: ResponseCookies
fun status(): HttpStatusCode?
fun status(value: HttpStatusCode)
}
// Extension functions for common operations
suspend inline fun <reified T> ApplicationCall.receive(): T
suspend fun ApplicationCall.receiveText(): String
suspend fun ApplicationCall.respond(message: Any)
suspend fun ApplicationCall.respondText(text: String, contentType: ContentType? = null, status: HttpStatusCode? = null)Powerful routing DSL with pattern matching, parameter extraction, and nested route organization.
interface Route {
val parent: Route?
val selector: RouteSelector
val developmentMode: Boolean
val environment: ApplicationEnvironment
}
interface RoutingBuilder {
fun route(path: String, build: Route.() -> Unit): Route
fun method(method: HttpMethod, body: Route.() -> Unit): Route
fun handle(body: suspend PipelineContext<Unit, PipelineCall>.() -> Unit)
}
// DSL functions
fun Application.routing(configuration: Routing.() -> Unit)
fun Route.get(path: String = "", body: suspend PipelineContext<Unit, PipelineCall>.() -> Unit): Route
fun Route.post(path: String = "", body: suspend PipelineContext<Unit, PipelineCall>.() -> Unit): Route
fun Route.put(path: String = "", body: suspend PipelineContext<Unit, PipelineCall>.() -> Unit): Route
fun Route.delete(path: String = "", body: suspend PipelineContext<Unit, PipelineCall>.() -> Unit): RouteHTTP-specific utilities, link headers, server push support, and various helper functions.
// HTTP utilities
data class Link(
val uri: String,
val rel: List<String>,
val type: ContentType? = null,
val title: String? = null
)
// Extension properties for requests
val ApplicationRequest.uri: String
val ApplicationRequest.httpMethod: HttpMethod
val ApplicationRequest.path: String
val ApplicationRequest.host: String
val ApplicationRequest.port: Int
val ApplicationRequest.contentType: ContentType
val ApplicationRequest.userAgent: String?
// Logging and MDC support
interface MDCProvider {
fun get(key: String): String?
fun put(key: String, value: String?)
fun remove(key: String)
fun clear()
}Ktor Server Core includes a comprehensive plugin system for extending functionality:
interface Plugin<TPipeline, TConfiguration, TPlugin> {
val key: AttributeKey<TPlugin>
fun install(pipeline: TPipeline, configure: TConfiguration.() -> Unit = {}): TPlugin
}
interface ApplicationPlugin<TConfiguration> : BaseApplicationPlugin<Application, TConfiguration, PluginInstance>
interface RouteScopedPlugin<TConfiguration> : Plugin<RoutingNode, TConfiguration, PluginInstance>fun createApplicationPlugin(
name: String,
body: PluginBuilder<Unit>.() -> Unit
): ApplicationPlugin<Unit>
fun <PluginConfigT> createApplicationPlugin(
name: String,
createConfiguration: () -> PluginConfigT,
body: PluginBuilder<PluginConfigT>.() -> Unit
): ApplicationPlugin<PluginConfigT>
fun createRouteScopedPlugin(
name: String,
body: RouteScopedPluginBuilder<Unit>.() -> Unit
): RouteScopedPlugin<Unit>// Install plugins into application or routes
fun <TConfiguration> Application.install(
plugin: ApplicationPlugin<TConfiguration>,
configure: TConfiguration.() -> Unit = {}
): PluginInstance
fun <TConfiguration> Route.install(
plugin: RouteScopedPlugin<TConfiguration>,
configure: TConfiguration.() -> Unit = {}
): PluginInstance
// Access installed plugins
fun <TConfiguration> Application.plugin(plugin: ApplicationPlugin<TConfiguration>): PluginInstance
fun <TConfiguration> Application.pluginOrNull(plugin: ApplicationPlugin<TConfiguration>): PluginInstance?// Application environment and configuration
interface ApplicationEnvironment {
val log: Logger
val config: ApplicationConfig
}
// Request/Response connection information
interface RequestConnectionPoint {
val scheme: String
val version: String
val port: Int
val host: String
val uri: String
val method: HttpMethod
val remoteHost: String
val userAgent: String?
}
// Parameter collections
typealias Parameters = StringValues
// Pipeline phases and contexts
interface PipelineContext<TSubject, TContext>
interface PipelineInterceptor<TSubject, TContext>
// Hook system for plugins
interface Hook<HookHandler> {
fun install(pipeline: ApplicationCallPipeline, handler: HookHandler)
}
// Event monitoring
interface Events {
fun <T> subscribe(definition: EventDefinition<T>, handler: suspend (T) -> Unit): EventSubscription<T>
fun <T> raise(definition: EventDefinition<T>, value: T)
}