Skip to content

Commit

Permalink
fix: Content delivery configured check & docs (#2481)
Browse files Browse the repository at this point in the history
- Checks storage is configured to show CD options in the UI
- Noted that CD storage needs to be configured in the docs
  • Loading branch information
JanCizmar authored Nov 10, 2024
1 parent b059045 commit 5a94758
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.tolgee.api.v2.controllers

import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import io.tolgee.component.contentDelivery.ContentDeliveryFileStorageProvider
import io.tolgee.component.publicBillingConfProvider.PublicBillingConfProvider
import io.tolgee.configuration.PublicConfigurationDTO
import io.tolgee.configuration.tolgee.TolgeeProperties
Expand All @@ -25,6 +26,7 @@ class ConfigurationController
private val applicationContext: ApplicationContext,
private val publicBillingConfProvider: PublicBillingConfProvider,
private val versionProvider: VersionProvider,
private val cdFileStorageProvider: ContentDeliveryFileStorageProvider,
) : IController {
@GetMapping(value = ["configuration"])
@Operation(summary = "Get server configuration")
Expand All @@ -39,6 +41,7 @@ class ConfigurationController
machineTranslationServices = machineTranslationServices,
billing = publicBillingConfProvider(),
versionProvider.version,
contentDeliveryEnabled = cdFileStorageProvider.isServerContentDeliveryConfigured(),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import io.tolgee.dtos.response.PublicBillingConfigurationDTO

@Suppress("MemberVisibilityCanBePrivate", "unused")
class PublicConfigurationDTO(
@Schema(hidden = true) properties: TolgeeProperties,
@Schema(hidden = true)
val properties: TolgeeProperties,
val machineTranslationServices: MtServicesDTO,
val billing: PublicBillingConfigurationDTO,
val version: String,
contentDeliveryEnabled: Boolean,
) {
val authentication: Boolean = properties.authentication.enabled
var authMethods: AuthMethodsDTO? = null
Expand All @@ -32,7 +34,8 @@ class PublicConfigurationDTO(
val ga4Tag = properties.ga4Tag
val postHogApiKey: String? = properties.postHog.apiKey
val postHogHost: String? = properties.postHog.host
val contentDeliveryConfigured: Boolean = properties.contentDelivery.publicUrlPrefix != null
val contentDeliveryConfigured: Boolean = contentDeliveryEnabled

val userSourceField: Boolean = properties.userSourceField
val plausible: PlausibleDto =
PlausibleDto(
Expand Down Expand Up @@ -101,4 +104,9 @@ class PublicConfigurationDTO(
passwordResettable = properties.authentication.nativeEnabled
allowRegistrations = properties.authentication.registrationsAllowed
}

private fun isContentDeliveryEnabled(): Boolean {
return properties.contentDelivery.publicUrlPrefix != null &&
properties.contentDelivery.storage.s3.enabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ContentDeliveryFileStorageProvider(
(tolgeeProperties.contentDelivery.storage.s3.enabled) xor (tolgeeProperties.contentDelivery.storage.azure.enabled)

if (!isSingleSet) {
throw RuntimeException("Exactly one content storage must be set")
throw RuntimeException("You have to configure exactly one content storage via configuration properties")
}

if (tolgeeProperties.contentDelivery.storage.s3.enabled) {
Expand All @@ -60,6 +60,19 @@ class ContentDeliveryFileStorageProvider(
throw RuntimeException("No Content Storage is set")
}

fun isServerContentDeliveryConfigured(): Boolean {
if (tolgeeProperties.contentDelivery.publicUrlPrefix.isNullOrEmpty()) {
return false
}

return try {
getDefaultStorageProperties()
true
} catch (e: Throwable) {
false
}
}

private fun bypassForTesting(): FileStorage? {
if (tolgeeProperties.internal.e3eContentStorageBypassOk == null) {
return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import org.springframework.boot.context.properties.ConfigurationProperties
displayName = "Content Delivery",
description =
"These properties are used to configure " +
"default server content delivery storage.",
"default server content delivery storage." +
"\n \n" +
"To get content delivery working, " +
"you have to configure the Storage (e.g. S3 or Blob).",
)
class ContentDeliveryProperties {
@DocProperty(
Expand All @@ -22,6 +25,7 @@ class ContentDeliveryProperties {
var storage: ContentStorageProperties = ContentStorageProperties()

@DocProperty(
displayName = "Cache purging",
description =
"Several services can be used as cache. Tolgee is able to purge the cache when " +
"new files are published when this configuration is set.",
Expand Down

0 comments on commit 5a94758

Please sign in to comment.