-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-4082 Add native image sample with config file (#195)
* KTOR-4082 Add native image sample with config file * Update native-image-server-with-yaml-config/README.md Co-authored-by: Vik Nikolova <contact@viknikolova.com> * Update native-image-server-with-yaml-config/README.md Co-authored-by: Vik Nikolova <contact@viknikolova.com> * Update readme and project name --------- Co-authored-by: Vik Nikolova <contact@viknikolova.com>
- Loading branch information
Showing
28 changed files
with
1,730 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
.gradle | ||
build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
!**/src/main/**/build/ | ||
!**/src/test/**/build/ | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
bin/ | ||
!**/src/main/**/bin/ | ||
!**/src/test/**/bin/ | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
out/ | ||
!**/src/main/**/out/ | ||
!**/src/test/**/out/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Native image server using YAML configuration file | ||
|
||
This example shows how to build a native image for a Ktor application using YAML confinguration and [GraalVM](https://ktor.io/docs/graalvm.html). | ||
|
||
## Build | ||
|
||
To build the application in native image, you need to execute the following command: | ||
|
||
```bash | ||
./gradlew nativeCompile | ||
``` | ||
|
||
## Run | ||
|
||
To run the application, you need to execute the following command: | ||
|
||
```bash | ||
./build/native/nativeCompile/com.example.ktor-sample | ||
``` | ||
|
||
The application will be running at http://0.0.0.0:8080/. | ||
|
||
## Update metadata | ||
|
||
If new dependencies are added to the project or a project has been modified, some classes may not be included in | ||
the native image. To fix this, you need to update the metadata file. | ||
|
||
First you need to run the application with the agent to generate the metadata file: | ||
|
||
```bash | ||
./gradlew -Pagent run | ||
``` | ||
|
||
While the application is running, you need to access basic application functions | ||
|
||
It is important to gracefully shutdown the application to generate the metadata file. You can do this by using the [Shutdown URL](https://ktor.io/docs/server-shutdown-url.html): | ||
url: [http://localhost:8080/shutdown](http://localhost:8080/shutdown) | ||
|
||
Finally, you can copy to the metadata files: | ||
```bash | ||
./gradlew metadataCopy --task run --dir src/main/resources/META-INF/native-image | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
val kotlin_version: String by project | ||
val logback_version: String by project | ||
|
||
plugins { | ||
kotlin("jvm") version "2.0.10" | ||
id("io.ktor.plugin") version "3.0.0-beta-2" | ||
id("org.jetbrains.kotlin.plugin.serialization") version "2.0.10" | ||
id("org.graalvm.buildtools.native") version "0.10.2" | ||
} | ||
|
||
group = "example.com" | ||
version = "0.0.1" | ||
|
||
application { | ||
mainClass.set("io.ktor.server.netty.EngineMain") | ||
|
||
val isDevelopment: Boolean = project.ext.has("development") | ||
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment") | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation("io.ktor:ktor-server-content-negotiation-jvm") | ||
implementation("io.ktor:ktor-server-core-jvm") | ||
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm") | ||
implementation("io.ktor:ktor-server-netty-jvm") | ||
implementation("ch.qos.logback:logback-classic:$logback_version") | ||
implementation("io.ktor:ktor-server-config-yaml") | ||
testImplementation("io.ktor:ktor-server-test-host-jvm") | ||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
kotlin.code.style=official | ||
ktor_version=3.0.0-beta-2 | ||
kotlin_version=2.0.10 | ||
logback_version=1.4.14 |
Binary file added
BIN
+58.1 KB
native-image-server-with-yaml-config/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
native-image-server-with-yaml-config/gradle/wrapper/gradle-wrapper.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
Oops, something went wrong.