はじめに
以下の書籍をやるのにIntelliJ
をいれました。そしてKtor
のプラグインをいれようとしたところ躓いたのでまとめます
問題
「設定」→「プラグイン」でKtor
と検索しても表示されない
解決方法
こちらの記事に答えがありました
現在Intellij Idea Community Editionでは、プラグインを利用したKtorプロジェクト作成は行なえません
私が利用しているのはCommunity
ですがおそらく同じかと思います
なのでこちらからプロジェクトを手動で作成することにしました
もし書籍のハンズオンをやるのであればMustache
というプラグインを追加してください
あとからbuild.gradle.kt
に手動で追加したところなぜかプラグインの読み込みができませんでした
また、importと静的ファイルが少し変わっています。最後以下のコードで動きました
Application.kt
package com.example
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import com.example.plugins.*
import io.ktor.server.routing.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.http.*
import io.ktor.server.mustache.Mustache
import io.ktor.server.mustache.MustacheContent
import com.github.mustachejava.DefaultMustacheFactory
import io.ktor.content.*
import io.ktor.server.http.content.*
import java.io.File
fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
.start(wait = true)
}
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(Mustache) {
mustacheFactory = DefaultMustacheFactory("templates/mustache")
}
routing {
get("/") {
call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
}
get("/html-mustache") {
call.respond(MustacheContent("index.hbs", mapOf("user" to MustacheUser(1, "user1"))))
}
// Static feature. Try to access `/static/ktor_logo.svg`
static("/static") {
staticRootFolder = File("static")
files(".")
}
}
}
data class MustacheUser(val id: Int, val name: String)
画像はsrcディレクトリと同じ階層にstaticディレクトリを作り、そこにロゴの画像をいれました
おわりに
LinuxのPCを買ってIntelliJをいれましたが、ほとんどの設定をやってくれるので楽でした
Winowsしか使ったことなかったのでいつも選んでいましたが、今後は値段もやすいのでLinuxいれれば良いなと思いました
参考