Skip to content

Commit aac64c7

Browse files
author
Doris Lam
committed
adding flexo config env vars
1 parent f4d2f34 commit aac64c7

4 files changed

Lines changed: 55 additions & 18 deletions

File tree

src/main/kotlin/org/openmbee/flexo/sysmlv2/Flexo.kt

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import io.ktor.client.request.*
66
import io.ktor.client.statement.*
77
import io.ktor.http.*
88
import io.ktor.server.application.*
9+
import io.ktor.server.config.*
910
import io.ktor.server.response.*
1011
import io.ktor.util.pipeline.*
1112
import org.apache.commons.io.IOUtils
@@ -108,12 +109,13 @@ fun PipelineContext<*, ApplicationCall>.sysmlv2ElementIri(uuid: UUID): String {
108109
return "<urn:sysmlv2:${uuid}>"
109110
}
110111

111-
class FlexoRequestBuilder {
112+
class FlexoRequestBuilder(config: FlexoConfig) {
112113
private var headers = mutableListOf<Pair<String, List<String>>>()
113114
private var path: String = ""
114115
private var queryParams = mutableMapOf<String, String>()
115116
private var body: String = ""
116-
117+
private var flexohost = config.host
118+
private var flexoport = config.port
117119
fun addHeaders(vararg headers: Pair<String, String>) {
118120
this.headers.addAll(headers.map { it.first to listOf(it.second) })
119121
}
@@ -148,8 +150,8 @@ class FlexoRequestBuilder {
148150
return request {
149151
url {
150152
protocol = URLProtocol.HTTP
151-
host = "localhost"
152-
port = 8080
153+
host = flexohost
154+
port = flexoport
153155
path(path)
154156

155157
queryParams.forEach {
@@ -229,17 +231,17 @@ class FlexoResponse(
229231

230232
}
231233

232-
suspend fun flexoRequest(method: HttpMethod, auth: String, setup: FlexoRequestBuilder.() -> Unit): FlexoResponse {
234+
suspend fun PipelineContext<Unit, ApplicationCall>.flexoRequest(method: HttpMethod, setup: FlexoRequestBuilder.() -> Unit): FlexoResponse {
233235
val client = HttpClient() {
234236
install(HttpTimeout)
235237
}
236-
237-
val builder = FlexoRequestBuilder()
238+
val flexoconfig = getFlexoConfigValues(call.application.environment.config)
239+
val builder = FlexoRequestBuilder(flexoconfig)
238240

239241
setup(builder)
240242

241243
val request = builder.build()
242-
request.header(HttpHeaders.Authorization, auth)
244+
request.header(HttpHeaders.Authorization, call.request.headers["Authorization"]!!)
243245
request.method = method
244246
request.timeout {
245247
requestTimeoutMillis = 600000
@@ -251,15 +253,15 @@ suspend fun flexoRequest(method: HttpMethod, auth: String, setup: FlexoRequestBu
251253
}
252254

253255
suspend fun PipelineContext<Unit, ApplicationCall>.flexoRequestGet(setup: FlexoRequestBuilder.() -> Unit): FlexoResponse {
254-
return flexoRequest(HttpMethod.Get, call.request.headers["Authorization"]!!, setup)
256+
return flexoRequest(HttpMethod.Get, setup)
255257
}
256258

257259
suspend fun PipelineContext<Unit, ApplicationCall>.flexoRequestPut(setup: FlexoRequestBuilder.() -> Unit): FlexoResponse {
258-
return flexoRequest(HttpMethod.Put, call.request.headers["Authorization"]!!, setup)
260+
return flexoRequest(HttpMethod.Put, setup)
259261
}
260262

261263
suspend fun PipelineContext<Unit, ApplicationCall>.flexoRequestPost(setup: FlexoRequestBuilder.() -> Unit): FlexoResponse {
262-
return flexoRequest(HttpMethod.Post, call.request.headers["Authorization"]!!, setup)
264+
return flexoRequest(HttpMethod.Post, setup)
263265
}
264266

265267
open class FlexoModelHandler(val model: Model, val prefixes: PrefixMappingImpl) {
@@ -310,3 +312,13 @@ suspend fun PipelineContext<*, ApplicationCall>.forward(flexoResponse: FlexoResp
310312
}.toTypedArray())
311313
}
312314
}
315+
316+
data class FlexoConfig(
317+
val host: String,
318+
val port: Int
319+
)
320+
fun getFlexoConfigValues(config: ApplicationConfig): FlexoConfig {
321+
val host = config.propertyOrNull("flexo.host")?.getString() ?: "localhost"
322+
val port = config.propertyOrNull("flexo.port")?.getString()?.toInt() ?: 8080
323+
return FlexoConfig(host, port)
324+
}

src/main/kotlin/org/openmbee/flexo/sysmlv2/apis/ProjectApi.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ fun FlexoModelHandlerWithFocalNode.projectFromResponse(
5353
name = outgoing[DCTerms.title]?.literal()?: ""
5454
)
5555
}
56+
fun FlexoModelHandler.projectFromResponse(
57+
outgoing: Map<Property, Set<RDFNode>>,
58+
projectUuid: UUID = UUID.fromString(outgoing[MMS.id]?.literal()),
59+
branchUuid: UUID = UUID.fromString(outgoing[MMS.defaultBranchId]?.literal())
60+
): Project {
61+
return Project(
62+
atId = projectUuid,
63+
atType = Project.AtType.Project,
64+
created = OffsetDateTime.parse(
65+
outgoing[MMS.created]?.literal()
66+
?: OffsetDateTime.now().toString()),
67+
defaultBranch = Identified(branchUuid),
68+
description = outgoing[DCTerms.description]?.literal()?: "",
69+
name = outgoing[DCTerms.title]?.literal()?: ""
70+
)
71+
}
5672

5773
fun Route.ProjectApi() {
5874
// delete a project
@@ -164,8 +180,10 @@ fun Route.ProjectApi() {
164180
}
165181

166182
// parse the response model, convert it to JSON, and reply to client
167-
call.respond(flexoResponse.parseLdp {
168-
projectFromResponse()
183+
call.respond(flexoResponse.parseModel {
184+
projectFromResponse(indexOut("$ROOT_CONTEXT/orgs/sysml2/repos/$projectId"),
185+
UUID.fromString(projectId),
186+
branchUuid)
169187
})
170188
}
171189
}

src/main/resources/application.conf.example

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ ktor {
1111
}
1212
}
1313

14-
# Typesafe config allows multiple ways to provide configuration values without hard-coding them here.
15-
# Please see https://github.com/lightbend/config for details.
16-
auth {
17-
oauth {
18-
}
14+
flexo {
15+
host = "localhost"
16+
host = ${?FLEXO_HOST}
17+
port = "8080"
18+
port = ${?FLEXO_PORT}
1919
}

src/test/resources/application.test.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ ktor {
1010
modules = [ org.openmbee.flexo.sysmlv2.AppMainKt.main ]
1111
}
1212
}
13+
flexo {
14+
host = "localhost"
15+
host = ${?FLEXO_HOST}
16+
port = "8080"
17+
port = ${?FLEXO_PORT}
18+
}
19+

0 commit comments

Comments
 (0)