Skip to content

Commit

Permalink
Support web socket endpoints as regular gets.
Browse files Browse the repository at this point in the history
  • Loading branch information
kunyavskiy committed Sep 25, 2023
1 parent 745be02 commit 900581e
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/backend/src/main/kotlin/org/icpclive/overlay/Routing.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.icpclive.overlay

import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.websocket.*
import kotlinx.coroutines.flow.*
Expand All @@ -10,18 +12,23 @@ import org.icpclive.scoreboard.ScoreboardAndContestInfo
import org.icpclive.scoreboard.toLegacyScoreboard
import org.icpclive.util.sendJsonFlow

private inline fun <reified T> Route.setUpScoreboard(crossinline process: (Flow<ScoreboardAndContestInfo>) -> Flow<T>) {
webSocket("/normal") { sendJsonFlow(process(DataBus.getScoreboardEvents(OptimismLevel.NORMAL))) }
webSocket("/optimistic") { sendJsonFlow(process(DataBus.getScoreboardEvents(OptimismLevel.OPTIMISTIC))) }
webSocket("/pessimistic") { sendJsonFlow(process(DataBus.getScoreboardEvents(OptimismLevel.PESSIMISTIC))) }
inline fun <reified T: Any> Route.flowEndpoint(name: String, crossinline dataProvider: suspend () -> Flow<T>) {
webSocket(name) { sendJsonFlow(dataProvider()) }
get(name) { call.respond(dataProvider().first()) }
}

private inline fun <reified T : Any> Route.setUpScoreboard(crossinline process: (Flow<ScoreboardAndContestInfo>) -> Flow<T>) {
flowEndpoint("/normal") { process(DataBus.getScoreboardEvents(OptimismLevel.NORMAL)) }
flowEndpoint("/optimistic") { process(DataBus.getScoreboardEvents(OptimismLevel.OPTIMISTIC)) }
flowEndpoint("/pessimistic") { process(DataBus.getScoreboardEvents(OptimismLevel.PESSIMISTIC)) }
}

fun Route.configureOverlayRouting() {
webSocket("/mainScreen") { sendJsonFlow(DataBus.mainScreenFlow.await()) }
webSocket("/contestInfo") { sendJsonFlow(DataBus.contestInfoFlow.await()) }
webSocket("/queue") { sendJsonFlow(DataBus.queueFlow.await()) }
webSocket("/statistics") { sendJsonFlow(DataBus.statisticFlow.await()) }
webSocket("/ticker") { sendJsonFlow(DataBus.tickerFlow.await()) }
flowEndpoint("/mainScreen") { DataBus.mainScreenFlow.await() }
flowEndpoint("/contestInfo") { DataBus.contestInfoFlow.await() }
flowEndpoint("/queue") { DataBus.queueFlow.await() }
flowEndpoint("/statistics") { DataBus.statisticFlow.await() }
flowEndpoint("/ticker") { DataBus.tickerFlow.await() }
route("/scoreboard") {
setUpScoreboard { flow -> flow.map { it.scoreboardSnapshot.toLegacyScoreboard(it.info) } }
route("v2") {
Expand Down

0 comments on commit 900581e

Please sign in to comment.