6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

KtorでAuto-reloadを設定し開発効率を上げる

Posted at

学習メモ。開発時にソースを書き換えた後、いちいちサーバーを止めて再起動させるのは非常にめんどくさいので自動リロードを設定する方法を書く。

基本的にはドキュメントのまま。

application.yamlを修正

ktor:
    application:
        modules:
            - com.example.ApplicationKt.module
+    development: true
    deployment:
        port: 8080
+        watch:
+            - classes

watchはリソースの変更監視対象を書く。resourcesも対象にしたい場合は、classesの下にさらに追加すればよい。

変更時に再コンパイルされるように設定する

以下がそのコマンド。

./gradlew -t build -x test -i

テストを無視するために-x testを追加する。

これをIntelliJの実行構成にrecompileとして登録しておく。

image.png

検証

Application.ktでサーバーを起動後、recompileを実行する。

image.png

この時点でhttp://0.0.0.0:8080/へアクセスすると次のように表示される。

image.png

今度はRouting.ktを以下のように修正する。

package com.example

import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import org.jetbrains.exposed.sql.*
import org.koin.dsl.module
import org.koin.ktor.plugin.Koin
import org.koin.logger.slf4jLogger

fun Application.configureRouting() {
    routing {
        get("/") {
-            call.respondText("Hello Ktor!!")
+            call.respondText("Hello Ktor!! Reloaded!!")
        }
    }
}

保存すると再コンパイルが走る。

image.png

この状態でhttp://0.0.0.0:8080/へアクセスすると…

image.png

LGTM👍

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?