LoginSignup
4
3

More than 3 years have passed since last update.

Ktor で Hello World !

Posted at

はじめに

 今回は、人気急上昇中の言語 Kotlin の Web フレームワーク「Ktor」で "Hello World" していきます。「ケイター」って読むみたいです。(途中までコターって読むのかと思ってました。。笑)

Ktor の特徴

 Easy to use, fun and asynchronous.

  • 簡単
  • 楽しい(ここ重要)
  • 非同期

環境

種類 名前 バージョン
OS macOS Mojave 10.14.6
開発環境 IntelliJ IDEA Ultimate 2019.2.3
開発言語 Kotlin 1.3.50
依存ライブラリ Ktor 1.2.4

IntelliJ IDEA の設定

 Jetbrain の IDE を利用したことがない方のために設定についても記述しておきます。

カスタマイズ

 特にこだわりなければ「Skip Remaining and Set Default」をクリック
(UI Themes は Darcula が目に優しくおすすめです笑)
image.png

ライセンス

 ライセンス購入していない方は30日間の無料期間があるので「Evaluate for free」を選択して「Evaluate」をクリック
image.png

プラグイン

 「Configure」から「Plugins」をクリック
image.png

 「Marketplace」タブで「Ktor」と検索して、「install」して「Restart IDE」で再起動
image.png

Ktor プロジェクト作成

 「Create New Project」をクリック
image.png

 「Ktor」タブを選択して「Next」をクリック
image.png

 GroupId, ArtifactId, Version を入力して「Next」をクリック
image.png

 Project name, Project location を入力して「Finish」をクリック
image.png

 「Automatically import this project on changes in build script file」にチェックを入れて「OK」をクリック
image.png

 プロジェクト作成完了!
image.png

Hello World

 Hello World を表示していきます!

Plain Text

 生のテキストをブラウザで表示します。

src/Application.kt
// 自分のパッケージ名
package com.example.helloworld

import io.ktor.application.*
import io.ktor.response.*
import io.ktor.request.*
import io.ktor.routing.get
import io.ktor.routing.routing

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {

    routing {
        get("/") {
            call.respondText { "Hello World" }
        }
    }
}

 mainメソッドの左側にある実行ボタンをクリックして「Run'~'」をクリック
image.png

 すると下に http://0.0.0.0:8080 と出てくるのでこれをクリックしてブラウザを起動
image.png

 Hello World !!
image.png

HTML & CSS

 通常の開発では、生のテキストではなく HTML と CSS を組み合わせて表示しますよね。

resources/hello_world.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello World</title>
    <link rel="stylesheet" type="text/css" href="/hello_world.css">
</head>
<body>

<h1>
    Hello World
</h1>

</body>
</html>
resources/hello_world.css
h1 {
    color:red;
}
src/Application.kt
// 自分のパッケージ名
package com.example.helloworld

import io.ktor.application.*
import io.ktor.http.content.default
import io.ktor.http.content.file
import io.ktor.http.content.static
import io.ktor.routing.routing

fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {

    routing {
        static("/") {
            file("hello_world.css", "./resources/hello_world.css")
            default("resources/hello_world.html")
        }
    }
}

 同様に、mainメソッドの左側にある実行ボタンをクリックして「Run'~'」をクリックして、http://0.0.0.0:8080 をクリックしてブラウザ起動
image.png

その他

 HTML DSLという動的に HTML を生成する方法もあるので、ぜひお試しあれ〜

最後に

 いかがでしたか?少しでも Kotlin, Ktor いいなと思ってくれていたら嬉しいです(^ω^)
ぜひ Kotlin, Ktor で開発やってみてください!

 最後までご視聴ありがとうございました。

4
3
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
4
3