Skip to content

Commit

Permalink
♻️ Simplify Main by using the new overrideEndpoint parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
obecker committed Feb 18, 2024
1 parent c60780e commit 7d1c22c
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions backend/src/main/kotlin/de/obqo/causalist/app/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,11 @@ import org.http4k.connect.amazon.dynamodb.model.TableName
import org.http4k.connect.amazon.s3.Http
import org.http4k.connect.amazon.s3.S3Bucket
import org.http4k.connect.amazon.s3.model.BucketName
import org.http4k.core.Filter
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.NoOp
import org.http4k.core.Request
import org.http4k.core.Status
import org.http4k.core.Uri
import org.http4k.core.then
import org.http4k.filter.ClientFilters.SetBaseUriFrom
import org.http4k.lens.uri
import org.http4k.lens.value
import org.http4k.server.SunHttp
import org.http4k.server.asServer
Expand All @@ -57,37 +53,24 @@ fun main() {
println("Read environment from $envResource")
val environment = Environment.fromResource(envResource)

val dynamoDbUriFilter = environment["DYNAMODB_URI"]?.let { SetBaseUriFrom(Uri.of(it)) } ?: Filter.NoOp
val dynamoHttp = dynamoDbUriFilter.then(httpClient)

val s3UriFilter = environment["S3_URI"]?.let { SetBaseUriFrom(Uri.of(it)) } ?: Filter.NoOp
val s3Http = s3UriFilter.then(httpClient)

val api = buildApi(
environment = environment,
dynamoHttp = dynamoHttp,
s3Http = s3Http
)
val api = buildApi(environment)

// smoke test that the DB is available
val response = api(Request(method = Method.POST, "/api/login").body("""{"username":"foo","password":""}"""))
check(response.status == Status.FORBIDDEN) { response }

api.asServer(SunHttp(9000)).start()
println("Server started${env?.let { " with environment $it" }.orEmpty()}")
api.asServer(SunHttp(9000)).start().also {
println("Server started${env?.let { " with environment $env" }.orEmpty()}")
}.block()
}

// entrypoint for the AWS Lambda Runtime
@Suppress("Unused")
class ApiLambdaHandler : ApiGatewayV2LambdaFunction(AppLoader { env ->
buildApi(Environment.from(env), httpClient, httpClient)
buildApi(Environment.from(env))
})

private fun buildApi(
environment: Environment,
dynamoHttp: HttpHandler,
s3Http: HttpHandler
): HttpHandler {
private fun buildApi(environment: Environment): HttpHandler {
Config.init(environment)

val usersTableKey = EnvironmentKey.value(TableName).required("CAUSALIST_USERS_TABLE")
Expand All @@ -101,8 +84,9 @@ private fun buildApi(

val dynamoDb = DynamoDb.Http(
env = environment,
http = dynamoHttp,
credentialsProvider = credentialsProvider
http = httpClient,
credentialsProvider = credentialsProvider,
overrideEndpoint = environment[EnvironmentKey.uri().optional("DYNAMODB_URI")]
)

val userRepository = dynamoUserRepository(dynamoDb, usersTableKey(environment))
Expand All @@ -111,12 +95,14 @@ private fun buildApi(
val authentication = authentication(userService)

val bucketName = caseDocumentsBucketNameKey(environment)
val overriddenBucketEndpoint = environment[EnvironmentKey.uri().optional("S3_URI")]
val s3Bucket = S3Bucket.Http(
bucketName = bucketName,
bucketRegion = AWS_REGION(environment),
env = environment,
http = s3Http,
credentialsProvider = credentialsProvider
credentialsProvider = credentialsProvider,
http = httpClient,
overrideEndpoint = overriddenBucketEndpoint,
forcePathStyle = overriddenBucketEndpoint != null,
)
val s3BucketWrapper = S3BucketWrapper(bucketName, s3Bucket)

Expand Down

0 comments on commit 7d1c22c

Please sign in to comment.