libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit c8d3cdeeb92ed054e77c3c549f17e99fa5f288aa
parent 90d2a60ddca12cfb819c40564ef2f3d17344d047
Author: MS <ms@taler.net>
Date:   Wed, 27 Jan 2021 13:36:15 +0100

db connection string only from env

Diffstat:
Mnexus/src/main/kotlin/tech/libeufin/nexus/Main.kt | 16+++++-----------
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 16+++-------------
Mutil/src/main/kotlin/Config.kt | 10++++++++++
Mutil/src/main/kotlin/Errors.kt | 2+-
4 files changed, 19 insertions(+), 25 deletions(-)

diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/Main.kt @@ -35,15 +35,11 @@ import execThrowableOrTerminate import com.github.ajalt.clikt.core.* import com.github.ajalt.clikt.parameters.options.versionOption import tech.libeufin.nexus.iso20022.parseCamtMessage -import tech.libeufin.util.XMLUtil -import tech.libeufin.util.getVersion -import tech.libeufin.util.setLogLevel +import tech.libeufin.util.* import java.io.File val logger: Logger = LoggerFactory.getLogger("tech.libeufin.nexus") - -val LIBEUFIN_NEXUS_DB_CONNECTION = System.getenv( - "LIBEUFIN_NEXUS_DB_CONNECTION") ?: "jdbc:sqlite:/tmp/libeufin-nexus.sqlite3" +val NEXUS_DB_ENV_VAR_NAME = "LIBEUFIN_NEXUS_DB_CONNECTION" class NexusCommand : CliktCommand() { init { @@ -58,13 +54,12 @@ class Serve : CliktCommand("Run nexus HTTP server") { helpFormatter = CliktHelpFormatter(showDefaultValues = true) } } - private val dbConnString by option().default(LIBEUFIN_NEXUS_DB_CONNECTION) private val host by option().default("127.0.0.1") private val port by option().int().default(5001) private val logLevel by option() override fun run() { setLogLevel(logLevel) - serverMain(dbConnString, host, port) + serverMain(getDbConnFromEnv(NEXUS_DB_ENV_VAR_NAME), host, port) } } @@ -85,8 +80,8 @@ class ResetTables : CliktCommand("Drop all the tables from the database") { helpFormatter = CliktHelpFormatter(showDefaultValues = true) } } - private val dbConnString by option().default(LIBEUFIN_NEXUS_DB_CONNECTION) override fun run() { + val dbConnString = getDbConnFromEnv(NEXUS_DB_ENV_VAR_NAME) execThrowableOrTerminate { dbDropTables(dbConnString) dbCreateTables(dbConnString) @@ -95,12 +90,11 @@ class ResetTables : CliktCommand("Drop all the tables from the database") { } class Superuser : CliktCommand("Add superuser or change pw") { - private val dbConnString by option().default(LIBEUFIN_NEXUS_DB_CONNECTION) private val username by argument() private val password by option().prompt(requireConfirmation = true, hideInput = true) override fun run() { execThrowableOrTerminate { - dbCreateTables(dbConnString) + dbCreateTables(getDbConnFromEnv(NEXUS_DB_ENV_VAR_NAME)) } transaction { val hashedPw = hashpw(password) diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -82,15 +82,7 @@ import tech.libeufin.util.ebics_h004.EbicsTypes import java.util.* import kotlin.random.Random -val LIBEUFIN_SANDBOX_DB_CONNECTION = System.getenv( - "LIBEUFIN_SANDBOX_DB_CONNECTION") ?: "jdbc:sqlite:/tmp/libeufin-sandbox.sqlite3" - -class CustomerNotFound(id: String?) : Exception("Customer ${id} not found") -class BadInputData(inputData: String?) : Exception("Customer provided invalid input data: ${inputData}") -class UnacceptableFractional(badNumber: BigDecimal) : Exception( - "Unacceptable fractional part ${badNumber}" -) - +val SANDBOX_DB_ENV_VAR_NAME = "LIBEUFIN_SANDBOX_DB_CONNECTION" private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox") data class SandboxError(val statusCode: HttpStatusCode, val reason: String) : Exception() @@ -103,9 +95,8 @@ class ResetTables : CliktCommand("Drop all the tables from the database") { helpFormatter = CliktHelpFormatter(showDefaultValues = true) } } - - private val dbConnString by option().default(LIBEUFIN_SANDBOX_DB_CONNECTION) override fun run() { + val dbConnString = getDbConnFromEnv(SANDBOX_DB_ENV_VAR_NAME) execThrowableOrTerminate { dbDropTables(dbConnString) dbCreateTables(dbConnString) @@ -120,12 +111,11 @@ class Serve : CliktCommand("Run sandbox HTTP server") { } } - private val dbConnString by option().default(LIBEUFIN_SANDBOX_DB_CONNECTION) private val logLevel by option() private val port by option().int().default(5000) override fun run() { setLogLevel(logLevel) - serverMain(dbConnString, port) + serverMain(getDbConnFromEnv(SANDBOX_DB_ENV_VAR_NAME), port) } } diff --git a/util/src/main/kotlin/Config.kt b/util/src/main/kotlin/Config.kt @@ -5,6 +5,7 @@ import ch.qos.logback.classic.LoggerContext import ch.qos.logback.classic.util.ContextInitializer import ch.qos.logback.core.util.Loader import org.slf4j.LoggerFactory +import kotlin.system.exitProcess fun getVersion(): String { return Loader.getResource( @@ -46,4 +47,13 @@ fun setLogLevel(logLevel: String?) { } } } +} + +fun getDbConnFromEnv(varName: String): String { + val dbConnStr = System.getenv(varName) + if (dbConnStr.isNullOrBlank() or dbConnStr.isNullOrEmpty()) { + println("DB connection string not found/valid in the env variable $varName.") + exitProcess(1) + } + return dbConnStr } \ No newline at end of file diff --git a/util/src/main/kotlin/Errors.kt b/util/src/main/kotlin/Errors.kt @@ -32,7 +32,7 @@ fun execThrowableOrTerminate(func: () -> Unit) { try { func() } catch (e: Exception) { - e.printStackTrace() + println(e.message) exitProcess(1) } } \ No newline at end of file