Skip to content

Commit

Permalink
refactor(backend): reduce the number of mandatory env to set.
Browse files Browse the repository at this point in the history
  • Loading branch information
GerardPaligot committed Sep 8, 2024
1 parent d03ed06 commit 0186a31
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 8 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ gcloud run deploy confily \
--platform managed \
--port 8080 \
--region europe-west1 \
--set-env-vars=IS_CLOUD=true \
--set-env-vars=BASE_URL_CONFERENCE_HALL=conference-hall.io \
--set-env-vars=PROJECT_ID=$PROJECT_ID \
--allow-unauthenticated
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.paligot.confily.backend.events

import com.paligot.confily.backend.internals.SystemEnv
import java.text.DecimalFormat
import java.time.LocalDateTime

Expand Down Expand Up @@ -83,7 +84,7 @@ fun EventDb.openFeedbackUrl(): String? {
val formatMonth = formatter.format(startDate.monthValue)
val formatDay = formatter.format(startDate.dayOfMonth)
val dateFormatted = "${startDate.year}-$formatMonth-$formatDay"
"https://openfeedback.io/${this.openFeedbackId}/$dateFormatted"
"https://${SystemEnv.openFeedbackUrl}/${this.openFeedbackId}/$dateFormatted"
} catch (ignored: Throwable) {
null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
package com.paligot.confily.backend.internals

object SystemEnv {
val projectName = System.getenv("PROJECT_NAME") ?: "confily"
val isCloud = System.getenv("IS_CLOUD") == "true"
val gcpProjectId = System.getenv("PROJECT_ID")
val isCloud: Boolean = getEnv("IS_CLOUD") ?: true
val projectName: String = getEnv("PROJECT_NAME") ?: "confily"
val gcpProjectId: String = getEnv("PROJECT_ID")
?: throw IllegalStateException("PROJECT_ID is required")
val conferenceHallUrl: String = getEnv("BASE_URL_CONFERENCE_HALL")
?: "conference-hall.io"
val openPlannerUrl: String = getEnv("BASE_URL_OPEN_PLANNER")
?: "storage.googleapis.com/conferencecenterr.appspot.com"
val openFeedbackUrl: String = getEnv("BASE_URL_OPEN_FEEDBACK")
?: "openfeedback.io"
}

@Suppress("ReturnCount")
private inline fun <reified T> getEnv(name: String): T? {
if (hasEnv(name).not()) {
return null
}
val env = System.getenv(name)
if (T::class == Int::class && env.toDoubleOrNull() != null) {
return env.toInt() as T
} else if (T::class == Long::class && env.toDoubleOrNull() != null) {
return env.toLong() as T
} else if (T::class == Double::class && env.toDoubleOrNull() != null) {
return env.toDouble() as T
} else if (T::class == Boolean::class && env == "true" || env == "false") {
return env.toBoolean() as T
} else if (T::class == String::class) {
return env as T
} else if (T::class == List::class) {
return env.split(",").map { it.trim() } as T
} else if (T::class == Map::class) {
return env.split(",").map {
val (key, value) = it.split("=")
key to value
}.toMap() as T
} else if (T::class == Set::class) {
return env.split(",").map { it.trim() }.toSet() as T
}
throw IllegalArgumentException("Type not supported")
}

private fun hasEnv(name: String): Boolean =
System.getenv(name) != null && System.getenv(name).trim() != ""
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class SecretManagerImpl(
private val projectId: String,
private val client: SecretManagerServiceClient
) : Secret {
@Suppress("SwallowedException")
override fun get(id: String): String? {
return try {
val data = client
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.paligot.confily.backend.third.parties.conferencehall

import com.paligot.confily.backend.internals.SystemEnv
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.java.Java
Expand All @@ -14,7 +15,7 @@ import kotlinx.serialization.json.Json

class ConferenceHallApi(
private val client: HttpClient,
private val baseUrl: String = "https://${System.getenv("BASE_URL_CONFERENCE_HALL")}/api"
private val baseUrl: String = "https://${SystemEnv.conferenceHallUrl}/api"
) {
suspend fun fetchEventConfirmed(eventId: String, apiKey: String) =
client.get("$baseUrl/v1/event/$eventId?key=$apiKey&state=confirmed").body<Event>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.paligot.confily.backend.third.parties.openplanner

import com.paligot.confily.backend.internals.SystemEnv
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.java.Java
Expand All @@ -15,7 +16,7 @@ import kotlinx.serialization.json.Json

class OpenPlannerApi(
private val client: HttpClient,
private val baseUrl: String = "https://storage.googleapis.com/conferencecenterr.appspot.com"
private val baseUrl: String = "https://${SystemEnv.openPlannerUrl}"
) {
suspend fun fetchPrivateJson(eventId: String, privateId: String): OpenPlanner =
client.get("$baseUrl/events/$eventId/$privateId.json?t=${Clock.System.now().epochSeconds}")
Expand Down

0 comments on commit 0186a31

Please sign in to comment.