Created
August 29, 2025 11:34
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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