问题
I want to make a simple http server using ktor. However, when I enter the site (127.0.0.1:8080 or 0.0.0.0:8080), it just isn't there. It doesn't print and doesn't respond.
However if I use NanoHttpd instead of ktor, everything works fine. What is my issue?
import io.ktor.application.call
import io.ktor.http.ContentType
import io.ktor.response.respondText
import io.ktor.routing.get
import io.ktor.routing.routing
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
fun main() {
val server = embeddedServer(Netty, port = 8080) {
routing {
get("/") {
println("TEST")
call.respondText("Hello World!", ContentType.Text.Plain)
}
}
}
server.start(wait = true)
}
The output is just:
[main] INFO ktor.application - No ktor.deployment.watch patterns specified, automatic reload is not active
[main] INFO ktor.application - Responding at http://0.0.0.0:8080
回答1:
It could be one of the following things:
- The application code
- The run configuration
I am leaning towards there being a problem with the run configuration rather than the application code.
Application code
Even though I think your issue is with the run configuration, in case that it isn't, I'm providing sample code here.
When I use the IntelliJ Ktor plugin, the Ktor app is bootstrapped as follows:
Application.kt
package com.example
import io.ktor.application.*
import io.ktor.http.*
import io.ktor.response.*
import io.ktor.routing.*
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@kotlin.jvm.JvmOverloads
@Suppress("unused") // Referenced in application.conf
fun Application.module(testing: Boolean = false) {
routing {
get("/") {
call.respondText("Hello, world!", ContentType.Text.Plain)
}
}
}
application.conf
ktor {
deployment {
port = 8080
port = ${?PORT}
}
application {
modules = [ com.example.ApplicationKt.module ]
}
}
I've provided an example Ktor code base here: https://gitlab.com/tinacious/ktor-example
Run configuration
It could be your run configuration in IntelliJ. It needs to be set up as a Kotlin script (and not an Application). When it's set up as an Application, I get the same error you do. Here is how to set up the run configuration I have, allowing me to run the server in IntelliJ:
- Add a configuration
- Choose Kotlin script from the Templates section. Do not choose Application.
- Near the bottom where it says "Use classpath of module" choose your
application.main
. - Near the top where it says "Main class" you should be able to choose your main application. I found this only showed up after I chose the classpath of the module.
Here are the relevant sections in the configuration:
Here is what I describe in step 4, i.e. I can choose my main class after the class path is added:
The run symbol should be a Kotlin logo:
I would recommend installing the IntelliJ Ktor plugin for bootstrapping your project. It uses Gradle and bootstraps everything so when you run ./gradlew run
, you are running it properly and can access it without the manual build configuration step.
来源:https://stackoverflow.com/questions/62712508/ktor-site-not-reachable