diff --git a/.DS_Store b/.DS_Store
index c16f49fd6..855c4b071 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/codeSnippets/snippets/embedded-server/build.gradle.kts b/codeSnippets/snippets/embedded-server/build.gradle.kts
index 782c2ed18..dedc5f262 100644
--- a/codeSnippets/snippets/embedded-server/build.gradle.kts
+++ b/codeSnippets/snippets/embedded-server/build.gradle.kts
@@ -29,6 +29,9 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version")
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
+ implementation("io.ktor:ktor-server-jetty-jakarta:$ktor_version")
+ implementation("io.ktor:ktor-server-cio:$ktor_version")
+ implementation("io.ktor:ktor-server-tomcat-jakarta:$ktor_version")
implementation("ch.qos.logback:logback-classic:$logback_version")
testImplementation("junit:junit:$junit_version")
diff --git a/codeSnippets/snippets/embedded-server/src/main/kotlin/com/example/Application.kt b/codeSnippets/snippets/embedded-server/src/main/kotlin/com/example/Application.kt
index 2b5d5c024..e5a6db9b7 100644
--- a/codeSnippets/snippets/embedded-server/src/main/kotlin/com/example/Application.kt
+++ b/codeSnippets/snippets/embedded-server/src/main/kotlin/com/example/Application.kt
@@ -69,4 +69,49 @@ fun runServerWithCommandLineConfig(args: Array
- Ktor allows you to configure various server parameters directly in code, including the host address, port, server modules, and more. The method of configuration depends on the - way you set up a server - using embeddedServer - or EngineMain. -
-
- With embeddedServer
, you configure the server by passing the desired parameters directly to
- the function.
- The
-
- embeddedServer
-
- function accepts different parameters for configuring a server, including a server
- engine, the host and port for the server to listen on, and additional configurations.
-
- In this section, we'll take a look at several different examples of running embeddedServer
,
- illustrating how you can configure the server to your advantage.
-
- The code snippet below shows a basic server setup with the Netty engine and the 8080
port.
-
- Note that you can set the port
parameter to 0
to run the server on a random port.
- The embeddedServer
function returns an engine instance, so you can get a port value in code
- using the
-
- ApplicationEngine.resolvedConnectors
-
- function.
-
- The embeddedServer
function allows you to pass engine-specific options using the
- configure
parameter. This parameter includes options common for all engines and exposed by
- the
-
- ApplicationEngine.Configuration
-
- class.
-
- The example below shows how to configure a server using the Netty
engine. Within the
- configure
block, we define a connector
to specify the host and port, and
- customize various server parameters:
-
- The connectors.add()
method defines a connector with the specified host
- (127.0.0.1
)
- and port (8080
).
-
In addition to these options, you can configure other engine-specific properties.
-- Netty-specific options are exposed by the - - NettyApplicationEngine.Configuration - - class. -
-- Jetty-specific options are exposed by the - - JettyApplicationEngineBase.Configuration - - class. -
-You can configure the Jetty server inside the - - configureServer - - block, which provides access to a - Server - instance. -
-
- Use the idleTimeout
property to specify the duration of time a connection can be idle
- before it gets closed.
-
CIO-specific options are exposed by the - - CIOApplicationEngine.Configuration - - class. -
-If you use Tomcat as the engine, you can configure it using the - - configureTomcat - - property, which provides access to a - Tomcat - instance. -
-- The example below shows how to run a server with multiple connector endpoints - using a custom configuration represented by the - - ApplicationEngine - - interface. -
-- For the complete example, see - - embedded-server-multiple-connectors - . -
- -- You can also use a custom environment to - - serve HTTPS - . -
-
- Ktor allows you to dynamically configure an embeddedServer
using command-line arguments. This
- can be particularly useful in cases where configurations like ports, hosts, or timeouts need to be
- specified at runtime.
-
- To achieve this, use the - - CommandLineConfig - - class to parse command-line arguments into a configuration object and pass it within the configuration - block: -
-
- In this example, the
-
- takeFrom()
-
- function from Application.Configuration
is used to override engine configuration values, such
- as port
and host
.
- The
-
- loadCommonConfiguration()
-
- function loads configuration from the root environment, such as timeouts.
-
- To run the server, specify arguments in the following way: -
-- Ktor allows you to configure various server parameters, such as a host address and port, - modules - to load, and so on. - The configuration depends on the way you used to create a server - - - embeddedServer or EngineMain - - . -
-
- For EngineMain
, Ktor loads its configuration from a configuration file that uses the
-
- HOCON
-
- or YAML format. This way provides more flexibility to configure a server and allows you to change a
- configuration without recompiling your application. Moreover, you can run your application from a command line
- and override the required server parameters by passing corresponding
-
- command-line
-
- arguments.
-
- If you use
-
- EngineMain
-
- to start a server, Ktor loads configuration settings automatically from a file named
-
- HOCON (
-
- YAML (
-
- To use a YAML configuration file, you need to add the ktor-server-config-yaml
-
- dependency
-
- .
-
- A configuration file should contain at least
-
- modules to load
-
- specified using the ktor.application.modules
property, for example:
-
- In this case, Ktor calls the Application.module
function in the
-
- Besides modules to load, you can configure various server settings, including - predefined - (such as a port or host, SSL settings, etc.) and custom ones. - Let's take a look at several examples. -
- -
- In the example below, a server listening port is set to 8080
using the
- ktor.deployment.port
property.
-
- If you use EngineMain
, you can specify options common for all engines within the
- ktor.deployment
group.
-
- You can also configure Netty-specific options in a configuration file within the
- ktor.deployment
group:
-
- The example below enables Ktor to listen on the 8443
SSL port and specifies the required
-
- SSL settings
-
- in a separate security
block.
-
- Apart from specifying the predefined properties,
- Ktor allows you to keep custom settings in the configuration file.
- The configuration files below contain a custom jwt
group used to keep
- JWT
- settings.
-
- You can read and handle such settings in code. -
-- Note that sensitive data (like secret keys, database connection settings, and so on) should not be - stored in the configuration file as plain text. Consider using - - environment variables - - to specify such parameters. -
-- Below is a list of predefined settings that you can use inside a - - configuration file - . -
-- A host address. -
-
- 0.0.0.0
-
- A listening port. You can set this property to 0
to run the server on a random port.
-
- 8080
, 0
-
- A listening SSL port. You can set this property to 0
to run the server on a random
- port.
-
- 8443
, 0
-
- Note that SSL requires additional options listed below. -
-- Watch paths used for auto-reloading. -
-- A servlet context path. -
-
- /
-
- A shutdown URL. - Note that this option uses the plugin. -
-- A maximum time in milliseconds for a server to stop accepting new requests. -
-- A maximum time in milliseconds to wait until the server stops completely. -
-- A minimum size of a thread pool used to process application calls. -
-- A count of threads used to accept new connections and start call processing. -
-- A size of the event group for processing connections, parsing messages, and doing the engine's - internal work. -
-
- If you've set ktor.deployment.sslPort
, you need to specify the following
-
- SSL-specific
-
- properties:
-
- An SSL key store. -
-- An alias for the SSL key store. -
-- A password for the SSL key store. -
-- A password for the SSL private key. -
-
- In a configuration file, you can substitute parameters with environment variables by using the
- ${ENV}
/ $ENV
syntax.
- For example, you can assign the PORT
environment variable to the
- ktor.deployment.port
property in the following way:
-
- In this case, an environment variable value will be used to specify a listening port.
- If the PORT
environment variable variable doesn't exist at runtime, you can provide a default
- port value as follows:
-
- Ktor allows you to access property values specified inside a configuration file in code.
- For example, if you've specified the ktor.deployment.port
property,...
-
- ... you can access the application's configuration using - - ApplicationEnvironment.config - - and get the required property value in the following way: -
-- This is especially useful when you keep custom settings in a configuration - file and need to access its values. -
-- If you use EngineMain to create a - server, you can run a packaged application from a command line and override - the required server parameters by passing corresponding command-line arguments. For example, you can - override a port specified in a configuration file in the following way: -
-- The available command-line options are listed below: -
-- A path to JAR file. -
-
- A path to a custom configuration file used instead of
-
- java -jar sample-app.jar -config=anotherfile.conf
-
- java -jar sample-app.jar -config=config-base.conf
- -config=config-dev.conf
. In this case all configs will be merged, where values from configs
- on the right will have priority.
-
- A host address. -
-- A listening port. -
-- Watch paths used for auto-reloading. -
-- SSL-specific options: -
-- A listening SSL port. -
-- An SSL key store. -
-
- If you need to override a predefined property
- that doesn't have a corresponding command-line option, use the -P
flag, for example:
-
- You can also use the -P
flag to override a custom property.
-
- You might want to do different things depending on whether a server is running locally or on a production
- machine. To achieve this, you can add a custom property in
- KTOR_ENV
environment variable is assigned to a custom ktor.environment
property.
-
- You can access the ktor.environment
value at runtime by
-
- reading configuration in code
-
- and perform the required actions:
-
- You can find the full example here: - - engine-main-custom-environment - . -
-
The embeddedServer
function is a simple way to configure server parameters in
- code
+ code
and quickly run an application. In the code snippet below, it accepts an
engine
and port as parameters to start a server. In the example below, we run a server with the
diff --git a/topics/server-engines.md b/topics/server-engines.md
index 6b61f78b2..ee310a034 100644
--- a/topics/server-engines.md
+++ b/topics/server-engines.md
@@ -6,40 +6,30 @@
Learn about engines that process network requests.
-To run a Ktor server application, you need to [create](server-create-and-configure.topic) and configure a server first.
-Server configuration includes different settings:
-- an [engine](#supported-engines) for processing network requests;
-- host and port values used to access a server;
-- SSL settings;
-- ... and so on.
-
-## Supported engines {id="supported-engines"}
-
-The table below lists engines supported by Ktor, along with the supported platforms:
-
-| Engine | Platforms | HTTP/2 |
-|-----------------------------------------|------------------------------------------------------|--------|
-| `Netty` | JVM | ✅ |
-| `Jetty` | JVM | ✅ |
-| `Tomcat` | JVM | ✅ |
-| `CIO` (Coroutine-based I/O) | JVM, [Native](server-native.md), [GraalVM](graalvm.md) | ✖️ |
-| [ServletApplicationEngine](server-war.md) | JVM | ✅ |
-
## Add dependencies {id="dependencies"}
-Before using the desired engine, you need to add the corresponding dependency to
-your [build script](server-dependencies.topic):
+Before using the desired engine, you need to add the corresponding dependency to your [build script](server-dependencies.topic):
* `ktor-server-netty`
* `ktor-server-jetty-jakarta`
* `ktor-server-tomcat-jakarta`
* `ktor-server-cio`
+