Hi all! I have develop a ktor app locally and I ha...
# ktor
s
Hi all! I have develop a ktor app locally and I have routes likes
/account
or
/events
that I would call at
<http://localhost:8080/events>
for example. Now I have deployed it on a server at something like
<https://some.domain/api/>
and I call endpoints at
<https://some.domain/api/events>
. Now there aren’t any route that is matched when I call that endpoint. I have an intercept where I print the request path and it says the path is
/api/events
which makes sense. My question is, how do I tell ktor that it’s hosted under a specific path ? In this case
/api
. This way, it would strip the
/api
part and the route would get matched and my code would run as expected. Thanks 🙂
a
Hey. Did you deploy a Ktor server inside a servlet container?
You can configure a
rootPath
in the server environment to solve your problem. Here is an example:
Copy code
embeddedServer(Netty, applicationEngineEnvironment {
    rootPath = "/api"

    connector {
        port = 4444
    }

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

}).start(true)
s
@Aleksei Tirman [JB] Thanks! No it’s not deployed in a container. It was but Tomcat was taking too much RAM for my web hosting so I decided to deploy as a fat jar (ShadowJar). Is it possible to set that
rootPath
inside the
application.conf
file? On the same level as the port.
a
Yes. This should work.
s
Currently I’m using this way of starting my web engine:
Copy code
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
And my understanding is that it gets the port from the
application.conf
and probably other configuration. Now is having the embeddedServer inside the main function is a better way?
Copy code
embeddedServer(Netty, applicationEngineEnvironment { } )
Thanks
a
No. It’s just a matter of taste.
s
Great, thanks!
Just a little follow up. I had the time to make the changes and everything worked as expected! Thanks again!