-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(server): Restructure JitBindingServer
- Loading branch information
Showing
7 changed files
with
184 additions
and
176 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...erver/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ActionCoords.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package io.github.typesafegithub.workflows.jitbindingserver | ||
|
||
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords | ||
import io.ktor.http.Parameters | ||
|
||
fun ActionCoords( | ||
parameters: Parameters, | ||
extractVersion: Boolean, | ||
): ActionCoords { | ||
val owner = parameters["owner"]!! | ||
val nameAndPath = parameters["name"]!!.split("__") | ||
val name = nameAndPath.first() | ||
val path = nameAndPath.drop(1).joinToString("/").takeUnless { it.isBlank() } | ||
val version = if (extractVersion) parameters["version"]!! else "irrelevant" | ||
|
||
return ActionCoords(owner, name, version, path) | ||
} |
84 changes: 84 additions & 0 deletions
84
...rver/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/ArtifactRoute.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.github.typesafegithub.workflows.jitbindingserver | ||
|
||
import io.github.reactivecircus.cache4k.Cache | ||
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords | ||
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrint | ||
import io.github.typesafegithub.workflows.mavenbinding.Artifact | ||
import io.github.typesafegithub.workflows.mavenbinding.JarArtifact | ||
import io.github.typesafegithub.workflows.mavenbinding.TextArtifact | ||
import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.response.respondBytes | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.Route | ||
import io.ktor.server.routing.Routing | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.head | ||
import io.ktor.server.routing.route | ||
import kotlin.time.Duration.Companion.hours | ||
|
||
typealias ArtifactResult = Result<Map<String, Artifact>> | ||
|
||
val bindingsCache = Cache.Builder<ActionCoords, ArtifactResult>().expireAfterWrite(1.hours).build() | ||
|
||
fun Routing.artifactRoute() { | ||
route("{owner}/{name}/{version}/{file}") { | ||
artifact(refresh = false) | ||
} | ||
|
||
route("/refresh/{owner}/{name}/{version}/{file}") { | ||
artifact(refresh = true) | ||
} | ||
} | ||
|
||
fun Route.artifact(refresh: Boolean = false) { | ||
headArtifact(refresh) | ||
getArtifact(refresh) | ||
} | ||
|
||
private fun Route.headArtifact(refresh: Boolean) { | ||
head { | ||
val bindingArtifacts = call.toBindingArtifacts(refresh) ?: return@head call.respondNotFound() | ||
|
||
val file = call.parameters["file"] ?: return@head call.respondNotFound() | ||
|
||
if (file in bindingArtifacts) { | ||
call.respondText("Exists", status = HttpStatusCode.OK) | ||
} else { | ||
call.respondNotFound() | ||
} | ||
} | ||
} | ||
|
||
private fun Route.getArtifact(refresh: Boolean) { | ||
get { | ||
val bindingArtifacts = call.toBindingArtifacts(refresh) ?: return@get call.respondNotFound() | ||
|
||
if (refresh && !deliverOnRefreshRoute) return@get call.respondText("OK") | ||
|
||
val file = call.parameters["file"] ?: return@get call.respondNotFound() | ||
|
||
val artifact = bindingArtifacts[file] ?: return@get call.respondNotFound() | ||
|
||
when (artifact) { | ||
is TextArtifact -> call.respondText(artifact.data()) | ||
is JarArtifact -> call.respondBytes(artifact.data(), ContentType.parse("application/java-archive")) | ||
else -> call.respondNotFound() | ||
} | ||
} | ||
} | ||
|
||
private suspend fun ApplicationCall.toBindingArtifacts(refresh: Boolean): Map<String, Artifact>? { | ||
val actionCoords = ActionCoords(parameters, true) | ||
|
||
logger.info { "➡️ Requesting ${actionCoords.prettyPrint}" } | ||
return if (refresh) { | ||
actionCoords.buildVersionArtifacts().also { | ||
bindingsCache.put(actionCoords, runCatching { it!! }) | ||
} | ||
} else { | ||
bindingsCache.get(actionCoords) { runCatching { actionCoords.buildVersionArtifacts()!! } }.getOrNull() | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ver/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/InternalRoutes.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.github.typesafegithub.workflows.jitbindingserver | ||
|
||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.Routing | ||
import io.ktor.server.routing.get | ||
|
||
fun Routing.internalRoutes() { | ||
get("/status") { | ||
call.respondText("OK") | ||
} | ||
} |
This file contains 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
39 changes: 39 additions & 0 deletions
39
...rver/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/MetadataRoute.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package io.github.typesafegithub.workflows.jitbindingserver | ||
|
||
import io.github.typesafegithub.workflows.mavenbinding.buildPackageArtifacts | ||
import io.github.typesafegithub.workflows.shared.internal.getGithubToken | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.Route | ||
import io.ktor.server.routing.Routing | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.route | ||
|
||
fun Routing.metadataRoute() { | ||
route("{owner}/{name}/{file}") { | ||
metadata() | ||
} | ||
|
||
route("/refresh/{owner}/{name}/{file}") { | ||
metadata(true) | ||
} | ||
} | ||
|
||
fun Route.metadata(refresh: Boolean = false) { | ||
get { | ||
if (refresh && !deliverOnRefreshRoute) return@get call.respondNotFound() | ||
|
||
val file = call.parameters["file"] ?: return@get call.respondNotFound() | ||
val actionCoords = ActionCoords(call.parameters, false) | ||
|
||
val bindingArtifacts = actionCoords.buildPackageArtifacts(githubToken = getGithubToken()) | ||
if (file in bindingArtifacts) { | ||
when (val artifact = bindingArtifacts[file]) { | ||
is String -> call.respondText(artifact) | ||
else -> call.respondText(text = "Not found", status = HttpStatusCode.NotFound) | ||
} | ||
} else { | ||
call.respondText(text = "Not found", status = HttpStatusCode.NotFound) | ||
} | ||
} | ||
} |
This file contains 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
25 changes: 25 additions & 0 deletions
25
...ing-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Plugins.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.github.typesafegithub.workflows.jitbindingserver | ||
|
||
import io.ktor.http.HttpHeaders | ||
import io.ktor.server.application.Application | ||
import io.ktor.server.application.install | ||
import io.ktor.server.plugins.callid.CallId | ||
import io.ktor.server.plugins.callid.callIdMdc | ||
import io.ktor.server.plugins.callid.generate | ||
import io.ktor.server.plugins.calllogging.CallLogging | ||
import io.opentelemetry.instrumentation.ktor.v3_0.server.KtorServerTracing | ||
|
||
fun Application.installPlugins() { | ||
install(CallId) { | ||
generate(15, "abcdefghijklmnopqrstuvwxyz0123456789") | ||
replyToHeader(HttpHeaders.XRequestId) | ||
} | ||
|
||
install(CallLogging) { | ||
callIdMdc("request-id") | ||
} | ||
|
||
install(KtorServerTracing) { | ||
setOpenTelemetry(buildOpenTelemetryConfig("github-actions-bindings")) | ||
} | ||
} |