Skip to content

Instantly share code, notes, and snippets.

@nomisRev
Created August 29, 2025 11:34
Show Gist options
  • Select an option

  • Save nomisRev/53a46c46490d650f19204f39ecf50e9a to your computer and use it in GitHub Desktop.

Select an option

Save nomisRev/53a46c46490d650f19204f39ecf50e9a to your computer and use it in GitHub Desktop.
Example of splitting up your services across different ports within the same Ktor service
package org.jetbrains.demo
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
fun main() {
embeddedServer(Netty, configure = {
connector {
host = "0.0.0.0"
port = 8080
}
connector {
host = "0.0.0.0"
port = 8081
}
}) {
configureRouting()
}.start(wait = true)
}
fun Application.configureRouting() {
routing {
localPort(8080) { restApi() }
localPort(8081) { graphQLApi() }
get("/health") { call.respondText("OK") }
}
}
fun Route.restApi() {
route("/api") {
get("/hello") {
call.respondText("Hello from the REST API!")
}
}
}
fun Route.graphQLApi() {
route("/graphql") {
post {
call.respondText("Hello from the GraphQL endpoint!")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment