how do i get the EmbeddedServer in this code snipp...
# getting-started
c
how do i get the EmbeddedServer in this code snippet:
Copy code
var server = embeddedServer(
                    factory = Netty,
                    port = port,
                    host = host
                ) {
                    module(app)
                }.start(wait = false)
s
probably you're question is already a sign of you having the wrong idea. You don't need to get the server. The server is started and being configured in
app
. And then the server simply listens on its port of incoming requests. Here is a link to the doc that uses a slightly configuration style: https://ktor.io/docs/server-create-a-new-project.html#change-the-default-port
c
what should i do if I want to start/close the server programmatically?
h
Just call the embeddedServer function without start?
c
I need to test with real servers and close them accordingly.
Currently I can start the ktor server but cannot close it when testing is finished.
and the whole tests are stucked.
s
how about with
System.exit(0)
of course that'd also ends the tests 🤔
according to the doc,
embeddedServer
is not used if you'd do test anyway
Copy code
class ApplicationTest {
    @Test
    fun testRoot() = testApplication {
        application {
            module()
        }
        val response = client.get("/")
        assertEquals(HttpStatusCode.OK, response.status)
        assertEquals("Hello, world!", response.bodyAsText())
    }
}
but maybe you're doing some kind of integration tests, so this doesn't apply?
c
System.exit(0) is obviously not acceptable, because I may run multiple servers at the same process.
So EmbeddedServer should be accessible when created. and close it respectively.
should be able to run multiple servers
c
thanks.