Skip to content

Commit

Permalink
fix(graphql): migrate to ktor v3 (#3844)
Browse files Browse the repository at this point in the history
  • Loading branch information
AngeloFilaseta authored Jan 24, 2025
1 parent 6b12039 commit aaa8926
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

package it.unibo.alchemist.boundary.monitors

import io.ktor.server.engine.ApplicationEngine
import io.ktor.server.engine.EmbeddedServer
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.netty.NettyApplicationEngine
import it.unibo.alchemist.boundary.OutputMonitor
import it.unibo.alchemist.boundary.graphql.monitor.EnvironmentSubscriptionMonitor
import it.unibo.alchemist.boundary.graphql.server.modules.graphQLModule
Expand All @@ -38,13 +39,13 @@ class GraphQLMonitor<T, P : Position<out P>>
@JvmOverloads
constructor(
val environment: Environment<T, P>,
val host: String = DefaultGraphQLSettings.DEFAULT_HOST,
val port: Int = DefaultGraphQLSettings.DEFAULT_PORT,
val teardownOnSimulationTermination: Boolean = true,
private val host: String = DefaultGraphQLSettings.DEFAULT_HOST,
private val port: Int = DefaultGraphQLSettings.DEFAULT_PORT,
private val teardownOnSimulationTermination: Boolean = true,
private val serverDispatcher: CoroutineDispatcher = Dispatchers.Default,
) : OutputMonitor<Any, Nothing> {
private val subscriptionMonitor = EnvironmentSubscriptionMonitor<Any, Nothing>()
private lateinit var server: ApplicationEngine
private lateinit var server: EmbeddedServer<NettyApplicationEngine, NettyApplicationEngine.Configuration>

override fun initialized(environment: Environment<Any, Nothing>) {
environment.simulation.addOutputMonitor(subscriptionMonitor)
Expand All @@ -62,7 +63,7 @@ class GraphQLMonitor<T, P : Position<out P>>
"alchemist-graphql-server@$host:$port",
).start()
runBlocking {
logger.info("Starting GraphQL server at $host:${server.resolvedConnectors().first().port}")
logger.info("Starting GraphQL server at $host:${server.engine.resolvedConnectors().first().port}")
}
mutex.acquireUninterruptibly()
}
Expand All @@ -77,7 +78,7 @@ class GraphQLMonitor<T, P : Position<out P>>
}
}

private fun makeServer(): ApplicationEngine =
private fun makeServer() =
embeddedServer(
Netty,
port = port,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@

package it.unibo.alchemist.boundary.webui.server.modules

import io.ktor.server.engine.ApplicationEngineEnvironment
import io.ktor.server.application.Application
import java.awt.Desktop
import java.net.URI

/**
* Start the default browser of the user on the server address.
*/
fun ApplicationEngineEnvironment.startBrowserModule() {
suspend fun Application.startBrowserModule() {
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
connectors.forEach {
engine.resolvedConnectors().forEach {
Desktop.getDesktop().browse(URI("${it.type.name.lowercase()}://${it.host}:${it.port}"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ package it.unibo.alchemist.boundary.webui.server.routes
import io.ktor.http.HttpStatusCode.Companion.Conflict
import io.ktor.http.HttpStatusCode.Companion.InternalServerError
import io.ktor.http.HttpStatusCode.Companion.OK
import io.ktor.server.application.ApplicationCall
import io.ktor.server.routing.Route
import io.ktor.server.routing.RoutingContext
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.util.pipeline.PipelineContext
import it.unibo.alchemist.boundary.webui.common.utility.Routes.SIMULATION_PAUSE_PATH
import it.unibo.alchemist.boundary.webui.common.utility.Routes.SIMULATION_PLAY_PATH
import it.unibo.alchemist.boundary.webui.common.utility.Routes.SIMULATION_STATUS_PATH
Expand Down Expand Up @@ -82,7 +81,7 @@ object SimulationRoute {
* is the error message.
* @param action the action to execute on the simulation.
*/
private suspend fun PipelineContext<Unit, ApplicationCall>.respondAction(
private suspend fun RoutingContext.respondAction(
additionalCheck: Pair<Status, String>,
action: (Simulation<Any, Nothing>) -> Unit,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ package it.unibo.alchemist.boundary.webui.server.utility

import io.ktor.http.HttpStatusCode
import io.ktor.http.HttpStatusCode.Companion.OK
import io.ktor.server.application.ApplicationCall
import io.ktor.server.application.call
import io.ktor.server.response.respond
import io.ktor.util.pipeline.PipelineContext
import io.ktor.server.routing.RoutingContext

/**
* Class representing an HTTP response.
Expand All @@ -32,7 +31,7 @@ data class Response<C>(
/**
* Utility function to dry-run the response process.
*/
suspend inline fun <reified C : Any> PipelineContext<Unit, ApplicationCall>.respond(response: Response<C>) {
suspend inline fun <reified C : Any> RoutingContext.respond(response: Response<C>) {
call.respond(response.code, response.content)
}
}
Expand Down
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ antlr4 = "4.13.2"
apollo ="4.0.0-beta.7"
arrow = "2.0.0"
dokka = "2.0.0"
graphql = "8.3.0"
graphhopper = "10.2"
graphql = "8.2.1"
graphstream = "2.0"
ignite = "2.16.0"
junit = "5.11.4"
konf = "1.1.2"
kotest = "6.0.0.M1"
ktor = "2.3.13"
kotlin = "2.1.0"
ktor = "3.0.0"
mockito = "5.15.2"
protelis = "17.7.1"
react = "2025.1.6-19.0.0"
Expand Down

0 comments on commit aaa8926

Please sign in to comment.