Core server functionality for the Ktor asynchronous web framework, providing essential building blocks for HTTP servers including application lifecycle management, routing foundations, request/response handling, and plugin architecture specifically compiled for JVM targets.
—
Core application framework providing the main application instance, environment configuration, and plugin management system for Ktor server applications.
The main application instance that serves as the container for all server functionality, managing plugins, configuration, and lifecycle.
/**
* Main application instance extending ApplicationCallPipeline
*/
class Application : ApplicationCallPipeline(), CoroutineScope {
/** Application environment containing configuration and logging */
val environment: ApplicationEnvironment
/** Attributes for storing application-scoped data */
val attributes: Attributes
/** Event monitor for application lifecycle events */
val monitor: Events
/** Coroutine context for application scope */
override val coroutineContext: CoroutineContext
/** Get installed plugin instance by plugin key */
fun <TPlugin : Any> plugin(plugin: Plugin<*, *, TPlugin>): TPlugin
/** Get installed plugin instance or null if not installed */
fun <TPlugin : Any> pluginOrNull(plugin: Plugin<*, *, TPlugin>): TPlugin?
/** Access application configuration */
val config: ApplicationConfig get() = environment.config
/** Access application logger */
val log: Logger get() = environment.log
/** Property access to configuration with serialization */
inline fun <reified T> property(path: String): T
/** Safe property access that returns null if not found */
inline fun <reified T> propertyOrNull(path: String): T?
}
/**
* Server configuration containing modules and settings
*/
class ServerConfig {
/** List of application modules to install */
val modules: List<suspend Application.() -> Unit>
/** Paths to watch for application reload */
val watchPaths: List<String>
/** Application's root path (prefix) */
val rootPath: String
/** Development mode flag */
val developmentMode: Boolean
/** Application environment */
val environment: ApplicationEnvironment
}
/**
* Builder for creating ServerConfig instances
*/
class ServerConfigBuilder(val environment: ApplicationEnvironment) {
/** List of application modules */
val modules: MutableList<suspend Application.() -> Unit>
/** Paths to watch for application reload */
var watchPaths: List<String>
/** Application's root path (prefix) */
var rootPath: String
/** Development mode flag */
var developmentMode: Boolean
/** Add a module to the server configuration */
fun module(module: suspend Application.() -> Unit)
/** Build the final ServerConfig */
fun build(): ServerConfig
}
/**
* Create a server configuration
*/
fun serverConfig(
environment: ApplicationEnvironment,
configure: ServerConfigBuilder.() -> Unit = {}
): ServerConfigProvides access to application configuration, logging, development mode settings, and other environment-specific properties.
/**
* Application environment providing access to configuration, classloader, and logging
*/
interface ApplicationEnvironment {
/** Application configuration */
val config: ApplicationConfig
/** Application logger */
val log: Logger
/** Event monitor for lifecycle events */
val monitor: Events
/** Whether application is running in development mode */
val developmentMode: Boolean
/** Root path for the application */
val rootPath: String
/** Class loader for the application */
val classLoader: ClassLoader
}Represents a single HTTP request-response cycle with access to request, response, and application context.
/**
* Represents a single HTTP request-response cycle
*/
interface ApplicationCall {
/** The application instance handling this call */
val application: Application
/** The HTTP request */
val request: ApplicationRequest
/** The HTTP response */
val response: ApplicationResponse
/** URL and form parameters */
val parameters: Parameters
/** Call-scoped attributes for storing data */
val attributes: Attributes
}
/** Access application from within a call context */
suspend fun ApplicationCall.application(): Application = application
/**
* Property to access the application from within a call
*/
val ApplicationCall.application: ApplicationCore pipeline system for processing HTTP requests and responses through interceptor chains.
/**
* Main pipeline for processing application calls
*/
open class ApplicationCallPipeline : Pipeline<Unit, ApplicationCall> {
companion object {
/** Setup phase for call processing */
val Setup: PipelinePhase = PipelinePhase("Setup")
/** Monitoring phase for call processing */
val Monitoring: PipelinePhase = PipelinePhase("Monitoring")
/** Plugins phase for call processing */
val Plugins: PipelinePhase = PipelinePhase("Plugins")
/** Call processing phase */
val Call: PipelinePhase = PipelinePhase("Call")
/** Fallback phase for unhandled calls */
val Fallback: PipelinePhase = PipelinePhase("Fallback")
}
/** Install a plugin into this pipeline */
fun <TConfiguration : Any, TPlugin : Any> install(
plugin: BaseApplicationPlugin<*, TConfiguration, TPlugin>,
configure: TConfiguration.() -> Unit = {}
): TPlugin
/** Get installed plugin instance */
fun <TPlugin : Any> plugin(plugin: Plugin<*, *, TPlugin>): TPlugin
/** Get installed plugin instance or null if not installed */
fun <TPlugin : Any> pluginOrNull(plugin: Plugin<*, *, TPlugin>): TPlugin?
}
/**
* Pipeline for processing incoming content
*/
class ApplicationReceivePipeline : Pipeline<ApplicationReceiveRequest, ApplicationCall> {
companion object {
/** Before transformation phase */
val Before: PipelinePhase = PipelinePhase("Before")
/** Transformation phase */
val Transform: PipelinePhase = PipelinePhase("Transform")
/** After transformation phase */
val After: PipelinePhase = PipelinePhase("After")
}
}
/**
* Pipeline for processing outgoing content
*/
class ApplicationSendPipeline : Pipeline<Any, ApplicationCall> {
companion object {
/** Before transformation phase */
val Before: PipelinePhase = PipelinePhase("Before")
/** Transform phase */
val Transform: PipelinePhase = PipelinePhase("Transform")
/** Render phase */
val Render: PipelinePhase = PipelinePhase("Render")
/** Content encoding phase */
val ContentEncoding: PipelinePhase = PipelinePhase("ContentEncoding")
/** Transfer encoding phase */
val TransferEncoding: PipelinePhase = PipelinePhase("TransferEncoding")
/** After phase */
val After: PipelinePhase = PipelinePhase("After")
}
}
/**
* Pipeline for processing call responses
*/
class ApplicationResponsePipeline : Pipeline<Any, ApplicationCall> {
companion object {
/** Transform phase */
val Transform: PipelinePhase = PipelinePhase("Transform")
/** Render phase */
val Render: PipelinePhase = PipelinePhase("Render")
/** Content encoding phase */
val ContentEncoding: PipelinePhase = PipelinePhase("ContentEncoding")
/** After phase */
val After: PipelinePhase = PipelinePhase("After")
}
}
/**
* Pipeline context for interceptors
*/
interface PipelineContext<TSubject : Any, TContext : Any> {
/** Current pipeline subject */
val subject: TSubject
/** Pipeline execution context */
val context: TContext
/** Proceed to next interceptor */
suspend fun proceed(): TSubject
/** Proceed with modified subject */
suspend fun proceedWith(subject: TSubject): TSubject
/** Finish pipeline execution */
fun finish()
}
/**
* Type alias for pipeline interceptor function
*/
typealias PipelineInterceptor<TSubject, TContext> =
suspend PipelineContext<TSubject, TContext>.(TSubject) -> UnitEvent-driven extension points for cross-cutting concerns and application lifecycle management.
/**
* Hook definition for event-driven extensions
*/
class Hook<T : Function<Unit>> {
/** Install hook handler */
fun install(handler: T)
/** Uninstall hook handler */
fun uninstall(handler: T)
}
/**
* Application call hooks for lifecycle events
*/
object ApplicationCallHooks {
/** Called when application call starts */
val CallStart: Hook<(ApplicationCall) -> Unit>
/** Called when application call completes */
val CallFinished: Hook<(ApplicationCall) -> Unit>
/** Called when application call fails */
val CallFailed: Hook<(ApplicationCall, Throwable) -> Unit>
}
/** Install hook handler */
fun <T : Function<Unit>> Application.hook(hook: Hook<T>, handler: T)Function type for configuring application modules and defining server behavior.
/**
* Application module function type for configuration
*/
typealias ApplicationModule = Application.() -> Unit
/**
* Configure application with module function
*/
fun Application.module(body: Application.() -> Unit) = body()Usage Examples:
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
// Basic application module
fun Application.myModule() {
routing {
get("/health") {
call.respondText("OK")
}
}
}
// Access application properties
fun Application.configExample() {
val dbUrl = config.property("database.url").getString()
val isDev = environment.developmentMode
log.info("Starting application in ${if (isDev) "development" else "production"} mode")
log.info("Database URL: $dbUrl")
}
// Hook installation
fun Application.hookExample() {
hook(ApplicationCallHooks.CallStart) { call ->
log.info("Processing ${call.request.httpMethod.value} ${call.request.uri}")
}
hook(ApplicationCallHooks.CallFinished) { call ->
log.info("Completed ${call.request.uri} with status ${call.response.status()}")
}
}Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor-server-core-jvm