-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(backend): reduce the number of mandatory env to set.
- Loading branch information
1 parent
d03ed06
commit 0186a31
Showing
6 changed files
with
49 additions
and
8 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
45 changes: 42 additions & 3 deletions
45
backend/src/main/java/com/paligot/confily/backend/internals/SystemEnv.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,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() != "" |
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