Native サーバー
コード例: embedded-server-native
KtorはKotlin/Nativeをサポートしており、追加のランタイムや仮想マシンなしでサーバーを実行できます。現在、Kotlin/NativeでKtorサーバーを実行する場合、以下の制限があります:
undefined
依存関係の追加
Kotlin/NativeプロジェクトのKtorサーバーには、少なくとも2つの依存関係が必要です。ktor-server-core依存関係とエンジン依存関係(CIO)です。以下のコードスニペットは、build.gradle.ktsファイルのnativeMainソースセットに依存関係を追加する方法を示しています。
kotlin
}
sourceSets {
val nativeMain by getting {
dependencies {
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-cio:$ktor_version")
}
}Nativeサーバーをテストするには、nativeTestソースセットにktor-server-test-hostアーティファクトを追加します。
kotlin
}
}
val nativeTest by getting {
dependencies {
implementation(kotlin("test"))
implementation("io.ktor:ktor-server-test-host:$ktor_version")
}
}Nativeターゲットの設定
必要なNativeターゲットを指定し、binariesプロパティを使用してNativeバイナリを宣言します。
kotlin
val arch = System.getProperty("os.arch")
val nativeTarget = when {
hostOs == "Mac OS X" && arch == "x86_64" -> macosX64("native")
hostOs == "Mac OS X" && arch == "aarch64" -> macosArm64("native")
hostOs == "Linux" && (arch == "x86_64" || arch == "amd64") -> linuxX64("native")
hostOs == "Linux" && arch == "aarch64" -> linuxArm64("native")
hostOs.startsWith("Windows") -> mingwX64("native")
// その他のサポートされているターゲットはこちらに記載されています: https://ktor.io/docs/server-native.html#targets
else -> throw GradleException("Host OS is not supported in Kotlin/Native.")
}
nativeTarget.apply {
binaries {
executable {
entryPoint = "main"
}
}完全な例はこちらで確認できます: embedded-server-native。
サーバーの作成
Gradleビルドスクリプトを設定した後、サーバーの作成で説明されているようにKtorサーバーを作成できます。
