-
-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Enable Cloudflare cache purging
- Loading branch information
Showing
14 changed files
with
233 additions
and
22 deletions.
There are no files selected for viewing
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
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
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
3 changes: 2 additions & 1 deletion
3
...rging/AzureContentDeliveryCachePurging.kt → ...tDoor/AzureContentDeliveryCachePurging.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
3 changes: 2 additions & 1 deletion
3
...zureContentDeliveryCachePurgingFactory.kt → ...zureContentDeliveryCachePurgingFactory.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
2 changes: 1 addition & 1 deletion
2
...y/cachePurging/AzureCredentialProvider.kt → ...azureFrontDoor/AzureCredentialProvider.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
74 changes: 74 additions & 0 deletions
74
...omponent/contentDelivery/cachePurging/cloudflare/CloudflareContentDeliveryCachePurging.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,74 @@ | ||
package io.tolgee.component.contentDelivery.cachePurging.cloudflare | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import io.tolgee.component.contentDelivery.cachePurging.ContentDeliveryCachePurging | ||
import io.tolgee.configuration.tolgee.ContentDeliveryCloudflareProperties | ||
import io.tolgee.model.contentDelivery.ContentDeliveryConfig | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.MediaType | ||
import org.springframework.web.client.RestTemplate | ||
|
||
class CloudflareContentDeliveryCachePurging( | ||
private val config: ContentDeliveryCloudflareProperties, | ||
private val restTemplate: RestTemplate, | ||
) : ContentDeliveryCachePurging { | ||
override fun purgeForPaths( | ||
contentDeliveryConfig: ContentDeliveryConfig, | ||
paths: Set<String>, | ||
) { | ||
val bodies = getChunkedBody(paths, contentDeliveryConfig) | ||
executePurgeRequest(bodies) | ||
} | ||
|
||
private fun getChunkedBody( | ||
paths: Set<String>, | ||
contentDeliveryConfig: ContentDeliveryConfig, | ||
): List<Map<String, List<String>>> { | ||
return paths.map { | ||
"$prefix/${contentDeliveryConfig.slug}/$it" | ||
} | ||
.chunked(config.maxFilesPerRequest) | ||
.map { urls -> | ||
mapOf("files" to urls) | ||
} | ||
} | ||
|
||
val prefix by lazy { | ||
config.urlPrefix?.removeSuffix("/") ?: "" | ||
} | ||
|
||
private fun executePurgeRequest(bodies: List<Map<String, List<String>>>) { | ||
bodies.forEach { body -> | ||
val entity: HttpEntity<String> = getHttpEntity(body) | ||
|
||
val url = "https://api.cloudflare.com/client/v4/zones/${config.zoneId}/purge_cache" | ||
|
||
val response = | ||
restTemplate.exchange( | ||
url, | ||
HttpMethod.POST, | ||
entity, | ||
String::class.java, | ||
) | ||
|
||
if (!response.statusCode.is2xxSuccessful) { | ||
throw IllegalStateException("Purging failed with status code ${response.statusCode}") | ||
} | ||
} | ||
} | ||
|
||
private fun getHttpEntity(body: Map<String, List<String>>): HttpEntity<String> { | ||
val headers = getHeaders() | ||
val jsonBody = jacksonObjectMapper().writeValueAsString(body) | ||
return HttpEntity(jsonBody, headers) | ||
} | ||
|
||
private fun getHeaders(): HttpHeaders { | ||
val headers = HttpHeaders() | ||
headers.contentType = MediaType.APPLICATION_JSON | ||
headers.set("Authorization", "Bearer ${config.apiKey}") | ||
return headers | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...t/contentDelivery/cachePurging/cloudflare/CloudflareContentDeliveryCachePurgingFactory.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,18 @@ | ||
package io.tolgee.component.contentDelivery.cachePurging.cloudflare | ||
|
||
import io.tolgee.component.contentDelivery.cachePurging.ContentDeliveryCachePurgingFactory | ||
import io.tolgee.configuration.tolgee.ContentDeliveryCloudflareProperties | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.client.RestTemplate | ||
|
||
@Component | ||
class CloudflareContentDeliveryCachePurgingFactory( | ||
private val restTemplate: RestTemplate, | ||
) : ContentDeliveryCachePurgingFactory { | ||
override fun create(config: Any): CloudflareContentDeliveryCachePurging { | ||
return CloudflareContentDeliveryCachePurging( | ||
config as ContentDeliveryCloudflareProperties, | ||
restTemplate, | ||
) | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
...ata/src/main/kotlin/io/tolgee/configuration/tolgee/ContentDeliveryCloudflareProperties.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,24 @@ | ||
package io.tolgee.configuration.tolgee | ||
|
||
import io.tolgee.configuration.annotations.DocProperty | ||
import io.tolgee.model.contentDelivery.ContentDeliveryCachePurgingType | ||
import io.tolgee.model.contentDelivery.ContentDeliveryPurgingConfig | ||
import org.springframework.boot.context.properties.ConfigurationProperties | ||
|
||
@ConfigurationProperties(prefix = "tolgee.content-delivery.cache-purging.cloudflare") | ||
class ContentDeliveryCloudflareProperties( | ||
var apiKey: String? = null, | ||
var urlPrefix: String? = null, | ||
var zoneId: String? = null, | ||
@DocProperty( | ||
"Number of paths to purge in one request. " + | ||
"(Cloudflare limit is 30 now, but it might be subject to change)", | ||
) | ||
var maxFilesPerRequest: Int = 30, | ||
) : ContentDeliveryPurgingConfig { | ||
override val enabled: Boolean | ||
get() = !apiKey.isNullOrEmpty() && !urlPrefix.isNullOrEmpty() && !zoneId.isNullOrEmpty() | ||
|
||
override val contentDeliveryCachePurgingType: ContentDeliveryCachePurgingType | ||
get() = ContentDeliveryCachePurgingType.CLOUDFLARE | ||
} |
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
4 changes: 3 additions & 1 deletion
4
...d/data/src/main/kotlin/io/tolgee/model/contentDelivery/ContentDeliveryCachePurgingType.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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package io.tolgee.model.contentDelivery | ||
|
||
import io.tolgee.component.contentDelivery.cachePurging.AzureContentDeliveryCachePurgingFactory | ||
import io.tolgee.component.contentDelivery.cachePurging.ContentDeliveryCachePurgingFactory | ||
import io.tolgee.component.contentDelivery.cachePurging.azureFrontDoor.AzureContentDeliveryCachePurgingFactory | ||
import io.tolgee.component.contentDelivery.cachePurging.cloudflare.CloudflareContentDeliveryCachePurgingFactory | ||
import kotlin.reflect.KClass | ||
|
||
enum class ContentDeliveryCachePurgingType(val factory: KClass<out ContentDeliveryCachePurgingFactory>) { | ||
AZURE_FRONT_DOOR(AzureContentDeliveryCachePurgingFactory::class), | ||
CLOUDFLARE(CloudflareContentDeliveryCachePurgingFactory::class), | ||
} |
4 changes: 2 additions & 2 deletions
4
.../src/test/kotlin/io/tolgee/unit/cachePurging/AzureContentStorageConfigCachePurgingTest.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
90 changes: 90 additions & 0 deletions
90
...test/kotlin/io/tolgee/unit/cachePurging/CloudflareContentStorageConfigCachePurgingTest.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,90 @@ | ||
package io.tolgee.unit.cachePurging | ||
|
||
import io.tolgee.component.contentDelivery.cachePurging.cloudflare.CloudflareContentDeliveryCachePurging | ||
import io.tolgee.configuration.tolgee.ContentDeliveryCloudflareProperties | ||
import io.tolgee.fixtures.node | ||
import io.tolgee.model.contentDelivery.ContentDeliveryConfig | ||
import io.tolgee.testing.assert | ||
import net.javacrumbs.jsonunit.assertj.JsonAssert | ||
import net.javacrumbs.jsonunit.assertj.assertThatJson | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mockito | ||
import org.mockito.invocation.Invocation | ||
import org.mockito.kotlin.any | ||
import org.mockito.kotlin.doAnswer | ||
import org.mockito.kotlin.eq | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
import org.springframework.http.HttpEntity | ||
import org.springframework.http.HttpMethod | ||
import org.springframework.http.HttpStatusCode | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.client.RestTemplate | ||
|
||
class CloudflareContentStorageConfigCachePurgingTest() { | ||
@Test | ||
fun `correctly purges`() { | ||
val config = | ||
ContentDeliveryCloudflareProperties( | ||
zoneId = "fake-zone-id", | ||
urlPrefix = "fake-url-prefix", | ||
maxFilesPerRequest = 10, | ||
apiKey = "token", | ||
) | ||
val restTemplateMock: RestTemplate = mock() | ||
val purging = CloudflareContentDeliveryCachePurging(config, restTemplateMock) | ||
val responseMock: ResponseEntity<*> = Mockito.mock(ResponseEntity::class.java) | ||
whenever(restTemplateMock.exchange(any<String>(), any<HttpMethod>(), any(), eq(String::class.java))).doAnswer { | ||
responseMock as ResponseEntity<String> | ||
} | ||
whenever(responseMock.statusCode).thenReturn(HttpStatusCode.valueOf(200)) | ||
val contentDeliveryConfig = mock<ContentDeliveryConfig>() | ||
whenever(contentDeliveryConfig.slug).thenReturn("fake-slug") | ||
|
||
purging.purgeForPaths( | ||
contentDeliveryConfig = contentDeliveryConfig, | ||
paths = (1..15).map { "fake-path-$it" }.toSet(), | ||
) | ||
|
||
val invocations = Mockito.mockingDetails(restTemplateMock).invocations | ||
val firstInvocation = invocations.first() | ||
assertFiles(firstInvocation) { | ||
isArray | ||
.hasSize(10) | ||
.contains("fake-url-prefix/fake-slug/fake-path-1") | ||
} | ||
assertUrl(firstInvocation) | ||
assertAuthorizationHeader(firstInvocation) | ||
|
||
assertFiles(invocations.toList()[1]) { | ||
isArray.hasSize(5) | ||
} | ||
} | ||
|
||
private fun assertFiles( | ||
invocation: Invocation, | ||
fn: JsonAssert.() -> Unit, | ||
) { | ||
val httpEntity = getHttpEntity(invocation) | ||
assertThatJson(httpEntity.body) { | ||
node("files") { | ||
fn() | ||
} | ||
} | ||
} | ||
|
||
private fun assertAuthorizationHeader(invocation: Invocation) { | ||
val httpEntity = getHttpEntity(invocation) | ||
val headers = httpEntity.headers | ||
headers["Authorization"].assert.isEqualTo(listOf("Bearer token")) | ||
} | ||
|
||
private fun getHttpEntity(invocation: Invocation) = invocation.arguments[2] as HttpEntity<*> | ||
|
||
private fun assertUrl(invocation: Invocation) { | ||
val url = invocation.arguments[0] | ||
url.assert.isEqualTo( | ||
"https://api.cloudflare.com/client/v4/zones/fake-zone-id/purge_cache", | ||
) | ||
} | ||
} |