@@ -11,6 +11,7 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
1111import io.github.typesafegithub.workflows.shared.internal.getGithubToken
1212import io.ktor.http.ContentType
1313import io.ktor.http.HttpStatusCode
14+ import io.ktor.server.application.ApplicationCall
1415import io.ktor.server.application.call
1516import io.ktor.server.application.install
1617import io.ktor.server.engine.embeddedServer
@@ -58,15 +59,30 @@ fun main() {
5859 metadata()
5960 }
6061
62+ route(" /refresh" ) {
63+ route(" {owner}/{name}/{version}/{file}" ) {
64+ artifact(bindingsCache, refresh = true )
65+ }
66+
67+ route(" {owner}/{name}/{file}" ) {
68+ metadata(refresh = true )
69+ }
70+ }
71+
6172 get(" /status" ) {
6273 call.respondText(" OK" )
6374 }
6475 }
6576 }.start(wait = true )
6677}
6778
68- private fun Route.metadata () {
79+ private fun Route.metadata (refresh : Boolean = false ) {
6980 get {
81+ if (refresh && ! deliverOnRefreshRoute) {
82+ call.respondText(text = " Not found" , status = HttpStatusCode .NotFound )
83+ return @get
84+ }
85+
7086 val owner = call.parameters[" owner" ]!!
7187 val name = call.parameters[" name" ]!!
7288 val file = call.parameters[" file" ]!!
@@ -88,29 +104,18 @@ private fun Route.metadata() {
88104 }
89105}
90106
91- private fun Route.artifact (bindingsCache : Cache <ActionCoords , Result <Map <String , Artifact >>>) {
107+ private fun Route.artifact (
108+ bindingsCache : Cache <ActionCoords , Result <Map <String , Artifact >>>,
109+ refresh : Boolean = false,
110+ ) {
92111 get {
93- val owner = call.parameters[" owner" ]!!
94- val name = call.parameters[" name" ]!!
95- val version = call.parameters[" version" ]!!
96- val actionCoords =
97- ActionCoords (
98- owner = owner,
99- name = name,
100- version = version,
101- )
102- println (" ➡️ Requesting ${actionCoords.prettyPrint} " )
103- val bindingArtifacts =
104- bindingsCache
105- .get(actionCoords) {
106- actionCoords.buildVersionArtifacts()?.let {
107- Result .success(it)
108- } ? : Result .failure(object : Throwable () {})
109- }.getOrNull()
110-
112+ val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
111113 if (bindingArtifacts == null ) {
112114 call.respondText(" Not found" , status = HttpStatusCode .NotFound )
113115 return @get
116+ } else if (refresh && ! deliverOnRefreshRoute) {
117+ call.respondText(text = " OK" )
118+ return @get
114119 }
115120
116121 val file = call.parameters[" file" ]!!
@@ -131,24 +136,8 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
131136 }
132137
133138 head {
134- val owner = call.parameters[" owner" ]!!
135- val name = call.parameters[" name" ]!!
136- val version = call.parameters[" version" ]!!
139+ val bindingArtifacts = call.toBindingArtifacts(bindingsCache, refresh)
137140 val file = call.parameters[" file" ]!!
138- val actionCoords =
139- ActionCoords (
140- owner = owner,
141- name = name,
142- version = version,
143- )
144- val bindingArtifacts =
145- bindingsCache
146- .get(actionCoords) {
147- actionCoords.buildVersionArtifacts()?.let {
148- Result .success(it)
149- } ? : Result .failure(object : Throwable () {})
150- }.getOrNull()
151-
152141 if (bindingArtifacts == null ) {
153142 call.respondText(" Not found" , status = HttpStatusCode .NotFound )
154143 return @head
@@ -160,3 +149,36 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
160149 }
161150 }
162151}
152+
153+ private suspend fun ApplicationCall.toBindingArtifacts (
154+ bindingsCache : Cache <ActionCoords , Result <Map <String , Artifact >>>,
155+ refresh : Boolean ,
156+ ): Map <String , Artifact >? {
157+ val owner = parameters[" owner" ]!!
158+ val name = parameters[" name" ]!!
159+ val version = parameters[" version" ]!!
160+ val actionCoords =
161+ ActionCoords (
162+ owner = owner,
163+ name = name,
164+ version = version,
165+ )
166+ println (" ➡️ Requesting ${actionCoords.prettyPrint} " )
167+ val bindingArtifacts =
168+ if (refresh) {
169+ actionCoords.buildVersionArtifacts().also {
170+ bindingsCache.put(actionCoords, Result .of(it))
171+ }
172+ } else {
173+ bindingsCache
174+ .get(actionCoords) { Result .of(actionCoords.buildVersionArtifacts()) }
175+ .getOrNull()
176+ }
177+ return bindingArtifacts
178+ }
179+
180+ private fun Result.Companion.failure (): Result <Nothing > = failure(object : Throwable () {})
181+
182+ private fun <T > Result.Companion.of (value : T ? ): Result <T > = value?.let { success(it) } ? : failure()
183+
184+ private val deliverOnRefreshRoute = System .getenv(" GWKT_DELIVER_ON_REFRESH" ).toBoolean()
0 commit comments