Ktor + Gradleでwebアプリケーションの雛形を表示するまで

Kotlin製のWeb framework「Ktor」
https://ktor.io/

ブラウザでHello, Worldを表示するまでを試してみます。


Gradle



IntelliJ IDEAでの実行が多いと思いますが、Gradleのみでのビルド&実行を試してみます。

まずはKotlinアプリケーションの雛形を作成。
gradleでKotlinプロジェクトの雛形を生成・ビルド

プロジェクト名は「ktor_sample」としました。


$ mkdir ktor_sample
$ cd ktor_sample
$ gradle init --type kotlin-application

Select build script DSL:
1: Groovy
2: Kotlin
Enter selection (default: Kotlin) [1..2] 1

Project name (default: ktor_sample): (そのままエンター)
Source package (default: ktor_sample): (そのままエンター)

BUILD SUCCESSFUL in 12s
2 actionable tasks: 2 executed




これでKotlinの雛形が作成されます。



build.gradle



自動生成されたbuild.gradleを編集します。
編集前の内容は以下の通り。
※コメントは削除してあります。


  1. plugins {
  2.     id 'org.jetbrains.kotlin.jvm' version '1.3.72'
  3.     id 'application'
  4. }
  5. repositories {
  6.     jcenter()
  7. }
  8. dependencies {
  9.     implementation platform('org.jetbrains.kotlin:kotlin-bom')
  10.     implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
  11.     testImplementation 'org.jetbrains.kotlin:kotlin-test'
  12.     testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
  13. }
  14. application {
  15.     mainClassName = 'ktor_sample.AppKt'
  16. }



Setting up a Gradle Build
KtorでJSONを返すまで
こちらを参考に、Ktorと内蔵webサーバーの設定を追記しました。


  1. // 使用するkotlinとktorのバージョン情報
  2. buildscript {
  3.     ext.kotlin_version = '1.4.0'
  4.     ext.ktor_version = '1.4.0'
  5. }
  6. plugins {
  7.     // 使用するkotlinは変数化したバージョン情報を指定
  8.     id 'org.jetbrains.kotlin.jvm' version "$kotlin_version"
  9.     id 'application'
  10. }
  11. repositories {
  12.     jcenter()
  13. }
  14. dependencies {
  15.     implementation platform('org.jetbrains.kotlin:kotlin-bom')
  16.     implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
  17.     testImplementation 'org.jetbrains.kotlin:kotlin-test'
  18.     testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
  19.     // Ktorの設定を追加
  20.     compile "io.ktor:ktor-server-netty:$ktor_version"
  21.     // Loggerの設定を追加
  22.     compile "ch.qos.logback:logback-classic:1.2.3"
  23. }
  24. application {
  25.     mainClassName = 'ktor_sample.AppKt'
  26. }





App.kt



雛形として生成されたsrc/main/kotlin/ktor_sample/App.ktを編集します。

編集前のデフォルト


  1. /*
  2. * This Kotlin source file was generated by the Gradle 'init' task.
  3. */
  4. package ktor_sample
  5. class App {
  6.     val greeting: String
  7.         get() {
  8.             return "Hello world."
  9.         }
  10. }
  11. fun main(args: Array<String>) {
  12.     println(App().greeting)
  13. }




編集後のApp.kt


  1. package ktor_sample
  2. import io.ktor.application.*
  3. import io.ktor.http.*
  4. import io.ktor.response.*
  5. import io.ktor.routing.*
  6. import io.ktor.server.engine.*
  7. import io.ktor.server.netty.*
  8. fun main(args: Array<String>) {
  9.     embeddedServer(Netty, 8080) {
  10.         routing {
  11.             get("/") {
  12.                 call.respondText("Hello, World!", ContentType.Text.Html)
  13.             }
  14.         }
  15.     }.start(wait = true)
  16. }




編集できたら実行します。


$ gradle run
...
<==========---> 80% EXECUTING [16s]
> :run




「EXECUTING」の表示で停止します。
これでwebサーバーが起動している状態となるので、ブラウザでhttp://localhost:8080を表示。

a29_01.png

ちゃんと実行できたようです。



【参考URL】
Setting up a Gradle Build
KtorでJSONを返すまで
関連記事

コメント

プロフィール

Author:symfo
blog形式だと探しにくいので、まとめサイト作成中です。
https://symfo.web.fc2.com/

PR

検索フォーム

月別アーカイブ