in sources/amper-cli/src/org/jetbrains/amper/cli/commands/ServerCommand.kt [79:151]
override suspend fun run(cliContext: CliContext, model: Model) {
// Note: with the current approach, the server will not see changes in Amper module files.
// TODO should we re-parse the model every time /task is called?
withBackend(cliContext, model = model, runSettings = AllRunSettings(composeHotReloadMode = composeHotReloadMode)) { backend ->
embeddedServer(Netty, port = port) {
install(ContentNegotiation) { json() }
install(SSE)
routing {
route("/task", HttpMethod.Post) {
sse {
val requestId = Uuid.random().toHexString()
MDC.put(useKey, useServerValue)
MDC.put(sessionIdKey, requestId)
val task = call.receive<Task>()
launch {
ServerWriter.logFlow
.filter { it.uuid == requestId }
.map { it.entry }
.filter { it.level.ordinal >= logLevel.ordinal }
.collect {
send(
ServerSentEvent(
it.message,
it.level.name.lowercase(),
"log"
)
)
}
}
runCatching {
send(
ServerSentEvent(
"Build started",
"event",
"event"
)
)
withContext(MDCContext()) {
backend.runTask(task = TaskName.fromHierarchy(task.taskHierarchy))
}
}.onFailure { e ->
when (e) {
is UserReadableError -> {
logger.error("BUILD FAILED")
send(
ServerSentEvent(
e.message,
"error",
"error"
)
)
}
}
}
send(
ServerSentEvent(
"Build finished",
"event",
"event"
)
)
close()
}
}
}
}.start(wait = true)
}
}