Skip to content

Commit 20d76fe

Browse files
authored
refactor(server): extract handlers in routing to functions (#1493)
Part of #1492. In the next change, we'll add alternative routes with the same logic, i.e. without the leading `/binding`. Later on, once we migrate all known usages, `/binding` will be removed.
1 parent 2b6be34 commit 20d76fe

File tree

1 file changed

+100
-88
lines changed
  • jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver

1 file changed

+100
-88
lines changed

jit-binding-server/src/main/kotlin/io/github/typesafegithub/workflows/jitbindingserver/Main.kt

Lines changed: 100 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import io.ktor.server.engine.embeddedServer
1616
import io.ktor.server.netty.Netty
1717
import io.ktor.server.response.respondBytes
1818
import io.ktor.server.response.respondText
19+
import io.ktor.server.routing.Route
1920
import io.ktor.server.routing.get
2021
import io.ktor.server.routing.head
2122
import io.ktor.server.routing.route
@@ -30,96 +31,13 @@ fun main() {
3031

3132
embeddedServer(Netty, port = 8080) {
3233
routing {
33-
route("/binding/{owner}/{name}/{version}/{file}") {
34-
get {
35-
val owner = call.parameters["owner"]!!
36-
val name = call.parameters["name"]!!
37-
val version = call.parameters["version"]!!
38-
val actionCoords =
39-
ActionCoords(
40-
owner = owner,
41-
name = name,
42-
version = version,
43-
)
44-
println("➡️ Requesting ${actionCoords.prettyPrint}")
45-
val bindingArtifacts =
46-
bindingsCache.get(actionCoords) {
47-
actionCoords.buildVersionArtifacts()?.let {
48-
Result.success(it)
49-
} ?: Result.failure(object : Throwable() {})
50-
}.getOrNull()
51-
52-
if (bindingArtifacts == null) {
53-
call.respondText("Not found", status = HttpStatusCode.NotFound)
54-
return@get
55-
}
56-
57-
val file = call.parameters["file"]!!
58-
if (file in bindingArtifacts) {
59-
when (val artifact = bindingArtifacts[file]) {
60-
is TextArtifact -> call.respondText(artifact.data)
61-
is JarArtifact ->
62-
call.respondBytes(
63-
bytes = artifact.data,
64-
contentType = ContentType.parse("application/java-archive"),
65-
)
66-
else -> call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
67-
}
68-
} else {
69-
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
70-
}
71-
}
72-
73-
head {
74-
val owner = call.parameters["owner"]!!
75-
val name = call.parameters["name"]!!
76-
val version = call.parameters["version"]!!
77-
val file = call.parameters["file"]!!
78-
val actionCoords =
79-
ActionCoords(
80-
owner = owner,
81-
name = name,
82-
version = version,
83-
)
84-
val bindingArtifacts =
85-
bindingsCache.get(actionCoords) {
86-
actionCoords.buildVersionArtifacts()?.let {
87-
Result.success(it)
88-
} ?: Result.failure(object : Throwable() {})
89-
}.getOrNull()
90-
91-
if (bindingArtifacts == null) {
92-
call.respondText("Not found", status = HttpStatusCode.NotFound)
93-
return@head
94-
}
95-
if (file in bindingArtifacts) {
96-
call.respondText("Exists", status = HttpStatusCode.OK)
97-
} else {
98-
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
99-
}
34+
route("/binding") {
35+
route("{owner}/{name}/{version}/{file}") {
36+
artifact(bindingsCache)
10037
}
101-
}
10238

103-
route("/binding/{owner}/{name}/{file}") {
104-
get {
105-
val owner = call.parameters["owner"]!!
106-
val name = call.parameters["name"]!!
107-
val file = call.parameters["file"]!!
108-
val actionCoords =
109-
ActionCoords(
110-
owner = owner,
111-
name = name,
112-
version = "irrelevant",
113-
)
114-
val bindingArtifacts = actionCoords.buildPackageArtifacts(githubToken = getGithubToken())
115-
if (file in bindingArtifacts) {
116-
when (val artifact = bindingArtifacts[file]) {
117-
is String -> call.respondText(artifact)
118-
else -> call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
119-
}
120-
} else {
121-
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
122-
}
39+
route("{owner}/{name}/{file}") {
40+
metadata()
12341
}
12442
}
12543

@@ -129,3 +47,97 @@ fun main() {
12947
}
13048
}.start(wait = true)
13149
}
50+
51+
private fun Route.metadata() {
52+
get {
53+
val owner = call.parameters["owner"]!!
54+
val name = call.parameters["name"]!!
55+
val file = call.parameters["file"]!!
56+
val actionCoords =
57+
ActionCoords(
58+
owner = owner,
59+
name = name,
60+
version = "irrelevant",
61+
)
62+
val bindingArtifacts = actionCoords.buildPackageArtifacts(githubToken = getGithubToken())
63+
if (file in bindingArtifacts) {
64+
when (val artifact = bindingArtifacts[file]) {
65+
is String -> call.respondText(artifact)
66+
else -> call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
67+
}
68+
} else {
69+
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
70+
}
71+
}
72+
}
73+
74+
private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String, Artifact>>>) {
75+
get {
76+
val owner = call.parameters["owner"]!!
77+
val name = call.parameters["name"]!!
78+
val version = call.parameters["version"]!!
79+
val actionCoords =
80+
ActionCoords(
81+
owner = owner,
82+
name = name,
83+
version = version,
84+
)
85+
println("➡️ Requesting ${actionCoords.prettyPrint}")
86+
val bindingArtifacts =
87+
bindingsCache.get(actionCoords) {
88+
actionCoords.buildVersionArtifacts()?.let {
89+
Result.success(it)
90+
} ?: Result.failure(object : Throwable() {})
91+
}.getOrNull()
92+
93+
if (bindingArtifacts == null) {
94+
call.respondText("Not found", status = HttpStatusCode.NotFound)
95+
return@get
96+
}
97+
98+
val file = call.parameters["file"]!!
99+
if (file in bindingArtifacts) {
100+
when (val artifact = bindingArtifacts[file]) {
101+
is TextArtifact -> call.respondText(artifact.data)
102+
is JarArtifact ->
103+
call.respondBytes(
104+
bytes = artifact.data,
105+
contentType = ContentType.parse("application/java-archive"),
106+
)
107+
108+
else -> call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
109+
}
110+
} else {
111+
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
112+
}
113+
}
114+
115+
head {
116+
val owner = call.parameters["owner"]!!
117+
val name = call.parameters["name"]!!
118+
val version = call.parameters["version"]!!
119+
val file = call.parameters["file"]!!
120+
val actionCoords =
121+
ActionCoords(
122+
owner = owner,
123+
name = name,
124+
version = version,
125+
)
126+
val bindingArtifacts =
127+
bindingsCache.get(actionCoords) {
128+
actionCoords.buildVersionArtifacts()?.let {
129+
Result.success(it)
130+
} ?: Result.failure(object : Throwable() {})
131+
}.getOrNull()
132+
133+
if (bindingArtifacts == null) {
134+
call.respondText("Not found", status = HttpStatusCode.NotFound)
135+
return@head
136+
}
137+
if (file in bindingArtifacts) {
138+
call.respondText("Exists", status = HttpStatusCode.OK)
139+
} else {
140+
call.respondText(text = "Not found", status = HttpStatusCode.NotFound)
141+
}
142+
}
143+
}

0 commit comments

Comments
 (0)