0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

IntelliJでKtorのプラグインが見つからない

Last updated at Posted at 2022-12-19

はじめに

以下の書籍をやるのにIntelliJをいれました。そしてKtorのプラグインをいれようとしたところ躓いたのでまとめます

問題

「設定」→「プラグイン」でKtorと検索しても表示されない

image.png

解決方法

こちらの記事に答えがありました

現在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いれれば良いなと思いました

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?