-
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Bunny.net cache purging, Origins specification for Cloudflare (#…
- Loading branch information
Showing
8 changed files
with
236 additions
and
22 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...o/tolgee/component/contentDelivery/cachePurging/bunny/BunnyContentDeliveryCachePurging.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,58 @@ | ||
package io.tolgee.component.contentDelivery.cachePurging.bunny | ||
|
||
import io.tolgee.component.contentDelivery.cachePurging.ContentDeliveryCachePurging | ||
import io.tolgee.configuration.tolgee.ContentDeliveryBunnyProperties | ||
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 | ||
import java.net.URLEncoder | ||
|
||
class BunnyContentDeliveryCachePurging( | ||
private val config: ContentDeliveryBunnyProperties, | ||
private val restTemplate: RestTemplate, | ||
) : ContentDeliveryCachePurging { | ||
override fun purgeForPaths( | ||
contentDeliveryConfig: ContentDeliveryConfig, | ||
paths: Set<String>, | ||
) { | ||
executePurgeRequest(contentDeliveryConfig) | ||
} | ||
|
||
val prefix by lazy { | ||
config.urlPrefix?.removeSuffix("/") ?: "" | ||
} | ||
|
||
private fun executePurgeRequest(contentDeliveryConfig: ContentDeliveryConfig) { | ||
val entity: HttpEntity<String> = getHttpEntity() | ||
val encodedPath = URLEncoder.encode("$prefix/${contentDeliveryConfig.slug}/*", Charsets.UTF_8) | ||
|
||
val url = "https://api.bunny.net/purge?url=$encodedPath" | ||
|
||
val response = | ||
restTemplate.exchange( | ||
url, | ||
HttpMethod.GET, | ||
entity, | ||
String::class.java, | ||
) | ||
|
||
if (!response.statusCode.is2xxSuccessful) { | ||
throw IllegalStateException("Purging failed with status code ${response.statusCode}") | ||
} | ||
} | ||
|
||
private fun getHttpEntity(): HttpEntity<String> { | ||
val headers = getHeaders() | ||
return HttpEntity(null, headers) | ||
} | ||
|
||
private fun getHeaders(): HttpHeaders { | ||
val headers = HttpHeaders() | ||
headers.contentType = MediaType.APPLICATION_JSON | ||
headers.set("AccessKey", "${config.apiKey}") | ||
return headers | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...e/component/contentDelivery/cachePurging/bunny/BunnyContentDeliveryCachePurgingFactory.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.bunny | ||
|
||
import io.tolgee.component.contentDelivery.cachePurging.ContentDeliveryCachePurgingFactory | ||
import io.tolgee.configuration.tolgee.ContentDeliveryBunnyProperties | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.client.RestTemplate | ||
|
||
@Component | ||
class BunnyContentDeliveryCachePurgingFactory( | ||
private val restTemplate: RestTemplate, | ||
) : ContentDeliveryCachePurgingFactory { | ||
override fun create(config: Any): BunnyContentDeliveryCachePurging { | ||
return BunnyContentDeliveryCachePurging( | ||
config as ContentDeliveryBunnyProperties, | ||
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
17 changes: 17 additions & 0 deletions
17
...end/data/src/main/kotlin/io/tolgee/configuration/tolgee/ContentDeliveryBunnyProperties.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,17 @@ | ||
package io.tolgee.configuration.tolgee | ||
|
||
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.bunny") | ||
class ContentDeliveryBunnyProperties( | ||
var apiKey: String? = null, | ||
var urlPrefix: String? = null, | ||
) : ContentDeliveryPurgingConfig { | ||
override val enabled: Boolean | ||
get() = !apiKey.isNullOrEmpty() && !urlPrefix.isNullOrEmpty() | ||
|
||
override val contentDeliveryCachePurgingType: ContentDeliveryCachePurgingType | ||
get() = ContentDeliveryCachePurgingType.BUNNY | ||
} |
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
64 changes: 64 additions & 0 deletions
64
.../src/test/kotlin/io/tolgee/unit/cachePurging/BunnyContentStorageConfigCachePurgingTest.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,64 @@ | ||
package io.tolgee.unit.cachePurging | ||
|
||
import io.tolgee.component.contentDelivery.cachePurging.bunny.BunnyContentDeliveryCachePurging | ||
import io.tolgee.configuration.tolgee.ContentDeliveryBunnyProperties | ||
import io.tolgee.model.contentDelivery.ContentDeliveryConfig | ||
import io.tolgee.testing.assert | ||
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 BunnyContentStorageConfigCachePurgingTest() { | ||
@Test | ||
fun `correctly purges`() { | ||
val config = | ||
ContentDeliveryBunnyProperties( | ||
urlPrefix = "fake-url-prefix", | ||
apiKey = "token", | ||
) | ||
val restTemplateMock: RestTemplate = mock() | ||
val purging = BunnyContentDeliveryCachePurging(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 invocation = invocations.single() | ||
assertUrl(invocation) | ||
assertAuthorizationHeader(invocation) | ||
} | ||
|
||
private fun assertAuthorizationHeader(invocation: Invocation) { | ||
val httpEntity = getHttpEntity(invocation) | ||
val headers = httpEntity.headers | ||
headers["AccessKey"].assert.isEqualTo(listOf("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.bunny.net/purge?url=fake-url-prefix%2Ffake-slug%2F*", | ||
) | ||
} | ||
} |
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