A multiplatform asynchronous framework for creating microservices, web applications, and HTTP clients written in Kotlin from the ground up
—
Complete server-side web framework for building REST APIs, web applications, and microservices with Ktor.
Creates and starts an embedded server with the specified engine and configuration.
/**
* Creates an embedded server with the specified engine factory
* @param factory The engine factory to use (Netty, CIO, Jetty, Tomcat)
* @param port The port to bind to
* @param host The host to bind to
* @param watchPaths Paths to watch for reloading
* @param configure Configuration block for the server environment
* @param module Application module to install
* @return Configured ApplicationEngine instance
*/
fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configuration> embeddedServer(
factory: ApplicationEngineFactory<TEngine, TConfiguration>,
port: Int = 80,
host: String = "0.0.0.0",
watchPaths: List<String> = listOf(WORKING_DIRECTORY_PATH),
configure: ApplicationEngineEnvironmentBuilder.() -> Unit = {},
module: suspend Application.() -> Unit
): TEngine
/**
* Creates an embedded server from environment
* @param factory The engine factory to use
* @param environment Pre-configured application environment
* @return Configured ApplicationEngine instance
*/
fun <TEngine : ApplicationEngine, TConfiguration : ApplicationEngine.Configuration> embeddedServer(
factory: ApplicationEngineFactory<TEngine, TConfiguration>,
environment: ApplicationEnvironment
): TEngineUsage Examples:
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.application.*
import io.ktor.server.routing.*
import io.ktor.server.response.*
// Basic embedded server
fun main() {
embeddedServer(Netty, port = 8080) {
routing {
get("/") {
call.respondText("Hello World!")
}
}
}.start(wait = true)
}
// Custom configuration
fun main() {
embeddedServer(Netty, port = 8080, host = "127.0.0.1") {
// Configure environment
developmentMode = true
watchPaths = listOf("build")
} {
// Application module
install(Routing) {
get("/health") {
call.respondText("OK")
}
}
}.start(wait = true)
}Central application instance that manages the server lifecycle and provides access to core functionality.
/**
* Main application interface for Ktor server applications
*/
public class Application internal constructor(
environment: ApplicationEnvironment,
developmentMode: Boolean,
public var rootPath: String,
public val monitor: Events,
public val parentCoroutineContext: CoroutineContext,
private val engineProvider: () -> ApplicationEngine
) : ApplicationCallPipeline, CoroutineScope {
/**
* The engine instance this application is running on
*/
val engine: ApplicationEngine
/**
* Application environment containing configuration and other resources
*/
val environment: ApplicationEnvironment
/**
* Event monitor for application lifecycle events
*/
val monitor: Events
/**
* Application-scoped attributes storage
*/
val attributes: Attributes
/**
* Parent coroutine context for the application
*/
override val coroutineContext: CoroutineContext
}Configuration container and resource provider for Ktor applications.
/**
* Application environment provides access to configuration and other resources
*/
interface ApplicationEnvironment {
/**
* Application configuration from config files
*/
val config: ApplicationConfig
/**
* Logger instance for the application
*/
val log: Logger
/**
* Development mode flag
*/
val developmentMode: Boolean
/**
* Application root path
*/
val rootPath: String
/**
* Class loader for loading application resources
*/
val classLoader: ClassLoader
/**
* Monitor for application lifecycle events
*/
val monitor: Events
}Core request/response processing pipeline that handles HTTP calls.
/**
* Base class for applications and routes that provides call processing pipeline
*/
open class ApplicationCallPipeline(
val developmentMode: Boolean = false,
val environment: ApplicationEnvironment? = null
) : Pipeline<Unit, ApplicationCall> {
/**
* Pipeline for processing incoming calls
*/
val call: PipelinePhase = PipelinePhase("Call")
/**
* Interceptors for handling specific functionality
*/
fun intercept(
phase: PipelinePhase,
block: suspend PipelineContext<Unit, ApplicationCall>.(Unit) -> Unit
)
}Configuration system supporting HOCON and YAML formats.
/**
* Application configuration interface
*/
interface ApplicationConfig {
/**
* Get property value by path
*/
fun property(path: String): ApplicationConfigValue
/**
* Get property value or null if not found
*/
fun propertyOrNull(path: String): ApplicationConfigValue?
/**
* Get list of property values
*/
fun configList(path: String): List<ApplicationConfig>
/**
* Get nested configuration
*/
fun config(path: String): ApplicationConfig
/**
* Check if property exists
*/
fun hasProperty(path: String): Boolean
/**
* Get all property keys
*/
fun keys(): Set<String>
}
/**
* Configuration value wrapper
*/
interface ApplicationConfigValue {
fun getString(): String
fun getList(): List<String>
}Usage Examples:
// application.conf (HOCON format)
/*
ktor {
application {
modules = [ com.example.ApplicationKt.module ]
}
deployment {
port = 8080
host = "0.0.0.0"
}
database {
url = "jdbc:postgresql://localhost/mydb"
driver = "org.postgresql.Driver"
}
}
*/
// Using configuration in application
fun Application.module() {
val dbUrl = environment.config.property("database.url").getString()
val dbDriver = environment.config.property("database.driver").getString()
// Configure database connection
connectToDatabase(dbUrl, dbDriver)
}Built-in server engine implementations for different deployment scenarios.
/**
* Netty engine factory for high-performance applications
*/
object Netty : ApplicationEngineFactory<NettyApplicationEngine, NettyApplicationEngine.Configuration> {
override fun create(
environment: ApplicationEnvironment,
configure: NettyApplicationEngine.Configuration.() -> Unit
): NettyApplicationEngine
}
/**
* CIO (Coroutine I/O) engine factory for pure Kotlin implementation
*/
object CIO : ApplicationEngineFactory<CIOApplicationEngine, CIOApplicationEngine.Configuration> {
override fun create(
environment: ApplicationEnvironment,
configure: CIOApplicationEngine.Configuration.() -> Unit
): CIOApplicationEngine
}
/**
* Jetty engine factory for servlet container compatibility
*/
object Jetty : ApplicationEngineFactory<JettyApplicationEngine, JettyApplicationEngine.Configuration> {
override fun create(
environment: ApplicationEnvironment,
configure: JettyApplicationEngine.Configuration.() -> Unit
): JettyApplicationEngine
}
/**
* Tomcat engine factory for servlet container compatibility
*/
object Tomcat : ApplicationEngineFactory<TomcatApplicationEngine, TomcatApplicationEngine.Configuration> {
override fun create(
environment: ApplicationEnvironment,
configure: TomcatApplicationEngine.Configuration.() -> Unit
): TomcatApplicationEngine
}Base interface for all server engine implementations.
/**
* Base interface for all application engines
*/
interface ApplicationEngine {
/**
* Engine environment
*/
val environment: ApplicationEnvironment
/**
* Application instance
*/
val application: Application
/**
* Start the engine
*/
fun start(wait: Boolean = false): ApplicationEngine
/**
* Stop the engine
*/
fun stop(gracePeriodMillis: Long, timeoutMillis: Long)
/**
* Engine configuration
*/
interface Configuration {
var parallelism: Int
var connectionGroupSize: Int
var workerGroupSize: Int
var callGroupSize: Int
}
}Install with Tessl CLI
npx tessl i tessl/maven-io-ktor--ktor