Skip to content

Commit

Permalink
Reconfigure serve command to host web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
milosmns committed Oct 16, 2023
1 parent 96f40a0 commit 199dc60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/commonMain/kotlin/commands/cli/ServeCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.cors.routing.CORS
import io.ktor.server.plugins.forwardedheaders.ForwardedHeaders
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import kotlinx.coroutines.Runnable
Expand All @@ -31,7 +32,7 @@ class ServeCommand(
) : Runnable {

@Serializable
private data class MessageResponse(val message: String)
data class MessageResponse(val message: String)

private lateinit var server: BaseApplicationEngine
private lateinit var storedRepos: List<Repository>
Expand Down Expand Up @@ -86,7 +87,7 @@ class ServeCommand(
install(ContentNegotiation) { json() }

routing {
get("/") { call.respond(MessageResponse("Yep, it runs…")) }
setUpRoot()
get("/repos") { call.respond(storedRepos) }
get("/metrics") { call.respond(metricsByName) }
get("/time-series") { call.respond(metricsByNameTimeSeries) }
Expand All @@ -98,3 +99,5 @@ class ServeCommand(
}

}

expect fun Route.setUpRoot(): Route
11 changes: 11 additions & 0 deletions src/jvmMain/kotlin/commands/cli/ServeCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package commands.cli

import io.ktor.server.http.content.singlePageApplication
import io.ktor.server.routing.Route

actual fun Route.setUpRoot(): Route = apply {
singlePageApplication {
useResources = true
filesPath = "web"
}
}
11 changes: 11 additions & 0 deletions src/macNativeMain/kotlin/commands/cli/ServeCommand.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package commands.cli

import commands.cli.ServeCommand.MessageResponse
import io.ktor.server.application.call
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.get

actual fun Route.setUpRoot() = get("/") {
call.respond(MessageResponse("UI runs only on the JVM"))
}

0 comments on commit 199dc60

Please sign in to comment.