Skip to content

Commit e82fcbc

Browse files
committed
feat(server): expose HEAD endpoint for metadata
An immediate reason to add it is enabling uptimerobot.com to display a status page: https://stats.uptimerobot.com/LHZ1ok6INN. In the free tier, it uses HEAD requests instead of GET, and HEAD is missing for metadata/versions.
1 parent 328b35f commit e82fcbc

File tree

1 file changed

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

1 file changed

+31
-3
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import com.sksamuel.aedile.core.LoadingCache
44
import io.github.oshai.kotlinlogging.KotlinLogging.logger
55
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.ActionCoords
66
import io.github.typesafegithub.workflows.actionbindinggenerator.domain.prettyPrintWithoutVersion
7+
import io.ktor.http.HttpStatusCode
78
import io.ktor.server.response.respondText
89
import io.ktor.server.routing.Route
910
import io.ktor.server.routing.Routing
1011
import io.ktor.server.routing.get
12+
import io.ktor.server.routing.head
1113
import io.ktor.server.routing.route
1214
import io.micrometer.core.instrument.binder.cache.CaffeineCacheMetrics
1315
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
@@ -25,15 +27,41 @@ fun Routing.metadataRoutes(
2527
}
2628

2729
route("{owner}/{name}/{file}") {
28-
metadata(metadataCache)
30+
headMetadata(metadataCache)
31+
getMetadata(metadataCache)
2932
}
3033

3134
route("/refresh/{owner}/{name}/{file}") {
32-
metadata(metadataCache, refresh = true)
35+
headMetadata(metadataCache, refresh = true)
36+
getMetadata(metadataCache, refresh = true)
3337
}
3438
}
3539

36-
private fun Route.metadata(
40+
private fun Route.headMetadata(
41+
metadataCache: LoadingCache<ActionCoords, CachedMetadataArtifact>,
42+
refresh: Boolean = false,
43+
) {
44+
head {
45+
val actionCoords = call.parameters.extractActionCoords(extractVersion = false)
46+
47+
if (refresh) {
48+
metadataCache.invalidate(actionCoords)
49+
}
50+
val metadataArtifacts = metadataCache.get(actionCoords)
51+
52+
if (refresh && !deliverOnRefreshRoute) return@head call.respondText(text = "OK")
53+
54+
val file = call.parameters["file"] ?: return@head call.respondNotFound()
55+
56+
if (file in metadataArtifacts) {
57+
call.respondText(text = "Exists", status = HttpStatusCode.OK)
58+
} else {
59+
call.respondNotFound()
60+
}
61+
}
62+
}
63+
64+
private fun Route.getMetadata(
3765
metadataCache: LoadingCache<ActionCoords, CachedMetadataArtifact>,
3866
refresh: Boolean = false,
3967
) {

0 commit comments

Comments
 (0)