Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KTOR-4082 Add native image sample with config file #195

Merged
merged 4 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 31 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,58 @@
A collection of ready-to-use samples for [Ktor](https://ktor.io). If you're looking for older samples, please see our
note on [Docs and Samples Migration](https://blog.jetbrains.com/ktor/2020/09/16/docs-and-samples-migration/).

- [Applications](#applications)
- [Server](#server)
- [Client](#client)
- [Deployment](#deployment)
- [License](#license)
- [Samples for Ktor](#samples-for-ktor)
- [Applications](#applications)
- [Server](#server)
- [Client](#client)
- [Deployment](#deployment)
- [Testing](#testing)
- [License](#license)

## Applications

* [chat](chat/README.md) - A Chat application written using [WebSockets](https://ktor.io/docs/websocket.html) and [Sessions](https://ktor.io/docs/sessions.html).
* [fullstack-mpp](fullstack-mpp/README.md) - An example of using Ktor as a client and server in a Kotlin Multiplatform
- [chat](chat/README.md) - A Chat application written using [WebSockets](https://ktor.io/docs/websocket.html) and [Sessions](https://ktor.io/docs/sessions.html).
- [fullstack-mpp](fullstack-mpp/README.md) - An example of using Ktor as a client and server in a Kotlin Multiplatform
project.
* [httpbin](httpbin/README.md) - HttpBin application implementing (large parts of) [httpbin(1)](https://httpbin.org/) HTTP request & response service.
* [kweet](kweet/README.md) - A messaging application that uses [FreeMarker](https://ktor.io/docs/freemarker.html) templates and the [Locations](https://ktor.io/docs/locations.html) plugin.
* [reverse-proxy](reverse-proxy/README.md) - A simple reverse proxy application.
* [reverse-proxy-ws](reverse-proxy-ws/README.md) - A reverse proxy application written using [WebSockets](https://ktor.io/docs/websocket.html).
* [youkube](youkube/README.md) - A video upload/view application.
* [version-diff](version-diff/README.md) - An application showing the difference between artifacts in two versions of a project.
* [postgres](postgres/README.md) - An application for creating, editing and deleting articles that uses Postgres database running on Docker image as a storage.
* [mongodb](mongodb/README.md) - An application for creating, editing and deleting articles that uses Mongodb running on Docker image as a storage.
* [mvc-web](mvc-web/README.md) - An application for adding and removing wishes to wishlist that uses [FreeMarker](https://ktor.io/docs/freemarker.html) templates and Exposed.
* [opentelemetry](opentelemetry/README.md) - An application that uses Kotlin DSL to work with OpenTelemetry Ktor plugins.
- [httpbin](httpbin/README.md) - HttpBin application implementing (large parts of) [httpbin(1)](https://httpbin.org/) HTTP request & response service.
- [kweet](kweet/README.md) - A messaging application that uses [FreeMarker](https://ktor.io/docs/freemarker.html) templates and the [Locations](https://ktor.io/docs/locations.html) plugin.
- [reverse-proxy](reverse-proxy/README.md) - A simple reverse proxy application.
- [reverse-proxy-ws](reverse-proxy-ws/README.md) - A reverse proxy application written using [WebSockets](https://ktor.io/docs/websocket.html).
- [youkube](youkube/README.md) - A video upload/view application.
- [version-diff](version-diff/README.md) - An application showing the difference between artifacts in two versions of a project.
- [postgres](postgres/README.md) - An application for creating, editing and deleting articles that uses Postgres database running on Docker image as a storage.
- [mongodb](mongodb/README.md) - An application for creating, editing and deleting articles that uses Mongodb running on Docker image as a storage.
- [mvc-web](mvc-web/README.md) - An application for adding and removing wishes to wishlist that uses [FreeMarker](https://ktor.io/docs/freemarker.html) templates and Exposed.
- [opentelemetry](opentelemetry/README.md) - An application that uses Kotlin DSL to work with OpenTelemetry Ktor plugins.

## Server

* [di-kodein](di-kodein/README.md) - An application showing how to use [Kodein](https://kodein.org)
- [di-kodein](di-kodein/README.md) - An application showing how to use [Kodein](https://kodein.org)
with a [Ktor](https://ktor.io) server.
* [filelisting](filelisting/README.md) - An application showing how to [serve static files](https://ktor.io/docs/serving-static-content.html).
* [location-header](location-header/README.md) - An application demonstrating how to use the HTTP `Location`
- [filelisting](filelisting/README.md) - An application showing how to [serve static files](https://ktor.io/docs/serving-static-content.html).
- [location-header](location-header/README.md) - An application demonstrating how to use the HTTP `Location`
headers.
* [sse](sse/README.md) - A Server Sent Events application.
* [structured-logging](structured-logging/README.md) - An application showing how to use [Ktor](https://ktor.io)
- [sse](sse/README.md) - A Server Sent Events application.
- [structured-logging](structured-logging/README.md) - An application showing how to use [Ktor](https://ktor.io)
structured logging.

## Client

* [client-mpp](client-mpp/README.md) - A sample project showing how to use a Ktor client in
- [client-mpp](client-mpp/README.md) - A sample project showing how to use a Ktor client in
a [multiplatform application](https://ktor.io/docs/getting-started-ktor-client-multiplatform-mobile.html).
* [client-multipart](client-multipart/README.md) - A sample showing how to send multipart data with the HTTP client.
* [client-tools](client-tools/README.md) - A sample showing several useful extension methods not included in Ktor itself.
- [client-multipart](client-multipart/README.md) - A sample showing how to send multipart data with the HTTP client.
- [client-tools](client-tools/README.md) - A sample showing several useful extension methods not included in Ktor itself.

## Deployment

* [graalvm](graalvm/README.md) - Explains how to build and run a Ktor application in the [GraalVM](https://ktor.io/docs/graalvm.html) native image.
* [maven-google-appengine-standard](maven-google-appengine-standard/README.md) - A sample showing how to deploy Ktor
- [graalvm](graalvm/README.md) - Explains how to build and run a Ktor application in the [GraalVM](https://ktor.io/docs/graalvm.html) native image.
- [native-image-server-with-yaml-config](native-image-server-with-yaml-config/README.md) - A sample showing how to build and run a Ktor application in the GraalVM native image with a YAML configuration file.
- [maven-google-appengine-standard](maven-google-appengine-standard/README.md) - A sample showing how to deploy Ktor
application to Google App Engine using [Maven](https://maven.apache.org/) and [Google App Engine](https://cloud.google.com/appengine/).

## Testing

* [jwt-auth-tests](jwt-auth-tests/README.md) - Shows how to write tests for RSA-signed JWT secured endpoints.
- [jwt-auth-tests](jwt-auth-tests/README.md) - Shows how to write tests for RSA-signed JWT secured endpoints.

## License

Expand Down
36 changes: 36 additions & 0 deletions native-image-server-with-yaml-config/.gitignore
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/
42 changes: 42 additions & 0 deletions native-image-server-with-yaml-config/README.md
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
vnikolova marked this conversation as resolved.
Show resolved Hide resolved
```

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
e5l marked this conversation as resolved.
Show resolved Hide resolved

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
```
35 changes: 35 additions & 0 deletions native-image-server-with-yaml-config/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

val kotlin_version: String by project

Check warning

Code scanning / detekt

Variable names should follow the naming convention set in the projects configuration. Warning

Variable names should match the pattern: [a-z][A-Za-z0-9]*
val logback_version: String by project

Check warning

Code scanning / detekt

Variable names should follow the naming convention set in the projects configuration. Warning

Variable names should match the pattern: [a-z][A-Za-z0-9]*

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")
}
4 changes: 4 additions & 0 deletions native-image-server-with-yaml-config/gradle.properties
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 not shown.
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
Loading
Loading