@@ -11,6 +11,7 @@ import io.github.typesafegithub.workflows.mavenbinding.buildVersionArtifacts
11
11
import io.github.typesafegithub.workflows.shared.internal.getGithubToken
12
12
import io.ktor.http.ContentType
13
13
import io.ktor.http.HttpStatusCode
14
+ import io.ktor.server.application.ApplicationCall
14
15
import io.ktor.server.application.call
15
16
import io.ktor.server.application.install
16
17
import io.ktor.server.engine.embeddedServer
@@ -58,15 +59,30 @@ fun main() {
58
59
metadata()
59
60
}
60
61
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
+
61
72
get(" /status" ) {
62
73
call.respondText(" OK" )
63
74
}
64
75
}
65
76
}.start(wait = true )
66
77
}
67
78
68
- private fun Route.metadata () {
79
+ private fun Route.metadata (refresh : Boolean = false ) {
69
80
get {
81
+ if (refresh && ! deliverOnRefreshRoute) {
82
+ call.respondText(text = " Not found" , status = HttpStatusCode .NotFound )
83
+ return @get
84
+ }
85
+
70
86
val owner = call.parameters[" owner" ]!!
71
87
val name = call.parameters[" name" ]!!
72
88
val file = call.parameters[" file" ]!!
@@ -88,29 +104,18 @@ private fun Route.metadata() {
88
104
}
89
105
}
90
106
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
+ ) {
92
111
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)
111
113
if (bindingArtifacts == null ) {
112
114
call.respondText(" Not found" , status = HttpStatusCode .NotFound )
113
115
return @get
116
+ } else if (refresh && ! deliverOnRefreshRoute) {
117
+ call.respondText(text = " OK" )
118
+ return @get
114
119
}
115
120
116
121
val file = call.parameters[" file" ]!!
@@ -131,24 +136,8 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
131
136
}
132
137
133
138
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)
137
140
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
-
152
141
if (bindingArtifacts == null ) {
153
142
call.respondText(" Not found" , status = HttpStatusCode .NotFound )
154
143
return @head
@@ -160,3 +149,36 @@ private fun Route.artifact(bindingsCache: Cache<ActionCoords, Result<Map<String,
160
149
}
161
150
}
162
151
}
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