libeufin

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

commit afacf8c17eea73c96a7e3f04c1c80356d5742168
parent 0a01af87e2d27e6cb2380b06db6382dbf66dc33b
Author: ms <ms@taler.net>
Date:   Mon, 11 Oct 2021 09:29:55 +0200

Ask env for base URL.

This allows to build URLs when the services
are running behind extra "/location" component(s)
of a reverse proxy.

Diffstat:
Mnexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt | 10++++++++--
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 41++++++++++++++++++++++-------------------
2 files changed, 30 insertions(+), 21 deletions(-)

diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt @@ -54,6 +54,12 @@ import tech.libeufin.util.* import java.net.BindException import java.net.URLEncoder import kotlin.system.exitProcess +import java.net.URL + +private val baseUrl = URL( + getValueFromEnv("LIBEUFIN_NEXUS_BASE_URL") ?: throw Exception( + "env LIBEUFIN_NEXUS_BASE_URL is not defined") +) /** * Return facade state depending on the type. @@ -895,7 +901,7 @@ val nexusApp: Application.() -> Unit = { type = f.type, baseUrl = call.url { parameters.clear() - encodedPath = "" + encodedPath = baseUrl.path pathComponents("facades", f.facadeName, f.type) encodedPath += "/" }, @@ -922,7 +928,7 @@ val nexusApp: Application.() -> Unit = { type = it.type, baseUrl = call.url { parameters.clear() - encodedPath = "" + encodedPath = baseUrl.path pathComponents("facades", it.facadeName, it.type) encodedPath += "/" }, diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -63,16 +63,12 @@ import com.github.ajalt.clikt.parameters.options.* import com.github.ajalt.clikt.parameters.types.int import execThrowableOrTerminate import io.ktor.application.ApplicationCall -import io.ktor.application.ApplicationCallPipeline import io.ktor.application.call import io.ktor.application.install -import io.ktor.features.CallLogging -import io.ktor.features.ContentNegotiation import org.jetbrains.exposed.sql.statements.api.ExposedBlob import com.fasterxml.jackson.core.util.DefaultIndenter import com.fasterxml.jackson.core.util.DefaultPrettyPrinter import com.fasterxml.jackson.module.kotlin.KotlinModule -import com.fasterxml.jackson.databind.SerializationFeature import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction import io.ktor.features.StatusPages import io.ktor.response.respond @@ -84,19 +80,23 @@ import io.ktor.routing.* import io.ktor.server.netty.* import io.ktor.util.date.* import io.ktor.application.* +import io.ktor.util.* import kotlinx.coroutines.newSingleThreadContext import startServer import tech.libeufin.util.* import validatePlainAmount import java.net.BindException -import java.util.* +import java.net.URL import kotlin.system.exitProcess private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox") -private val hostName: String? = getValueFromEnv("LIBEUFIN_SANDBOX_HOSTNAME") private val currencyEnv: String? = getValueFromEnv("LIBEUFIN_SANDBOX_CURRENCY") private val envName: String? = getValueFromEnv("TALER_ENV_NAME") const val SANDBOX_DB_ENV_VAR_NAME = "LIBEUFIN_SANDBOX_DB_CONNECTION" +private val baseUrl = URL( + getValueFromEnv("LIBEUFIN_SANDBOX_BASE_URL") ?: throw Exception( + "env LIBEUFIN_SANDBOX_BASE_URL is not defined") +) data class SandboxError( val statusCode: HttpStatusCode, @@ -890,23 +890,19 @@ val sandboxApp: Application.() -> Unit = { get("/taler") { requireSuperuser(call.request) SandboxAssert( - hostName != null, - "Own hostname not found. Logs should have warned" - ) - SandboxAssert( currencyEnv != null, "Currency not found. Logs should have warned" ) // check that the three canonical accounts exist val wo = transaction { - val exchange = tech.libeufin.sandbox.BankAccountEntity.find { - tech.libeufin.sandbox.BankAccountsTable.label eq "sandbox-account-exchange" + val exchange = BankAccountEntity.find { + BankAccountsTable.label eq "sandbox-account-exchange" }.firstOrNull() - val customer = tech.libeufin.sandbox.BankAccountEntity.find { - tech.libeufin.sandbox.BankAccountsTable.label eq "sandbox-account-customer" + val customer = BankAccountEntity.find { + BankAccountsTable.label eq "sandbox-account-customer" }.firstOrNull() - val merchant = tech.libeufin.sandbox.BankAccountEntity.find { - tech.libeufin.sandbox.BankAccountsTable.label eq "sandbox-account-merchant" + val merchant = BankAccountEntity.find { + BankAccountsTable.label eq "sandbox-account-merchant" }.firstOrNull() SandboxAssert(exchange != null, "exchange has no bank account") @@ -914,15 +910,22 @@ val sandboxApp: Application.() -> Unit = { SandboxAssert(merchant != null, "merchant has no bank account") // At this point, the three actors exist and a new withdraw operation can be created. - tech.libeufin.sandbox.TalerWithdrawalEntity.new { + TalerWithdrawalEntity.new { // wopid is autogenerated, and momentarily the only column - } } /** * Future versions will include the QR code in this response. */ - call.respondText("taler://withdraw/${hostName}/api/${wo.wopid}") + val ret = call.url { + protocol = URLProtocol( + "taler".plus(if (baseUrl.protocol.lowercase() == "http") "+http" else ""), + -1 + ) + pathComponents(baseUrl.path, "api", wo.wopid.toString()) + encodedPath += "/" + } + call.respondText(ret) return@get } get("/api/config") {