Skip to content

Commit

Permalink
Add server options to AWS Lambda handler
Browse files Browse the repository at this point in the history
*Why I did it?*
LambdaHandler had the hardcoded AwsServerOptions, so delivering
the custom server options was impossible.

*How I did it:*
LambdaHandler started to accept options by analogous to ZioLambdaHandler

Closes #4064
  • Loading branch information
sergiuszkierat committed Dec 24, 2024
1 parent 034c878 commit a7e3d39
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.io.{InputStream, OutputStream}

/** Used by [[AwsCdkAppTemplate]] for integration tests
*/
object CdkTestLambdaHandler extends LambdaHandler[IO, AwsRequestV1] {
object CdkTestLambdaHandler extends LambdaHandler[IO, AwsRequestV1](AwsCatsEffectServerOptions.default[IO]) {
override protected def getAllEndpoints: List[ServerEndpoint[Any, IO]] = allEndpoints.toList

override def handleRequest(input: InputStream, output: OutputStream, context: Context): Unit = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package sttp.tapir.serverless.aws.cdk

import cats.effect.IO
import com.amazonaws.services.lambda.runtime.api.client.api._
import io.circe.generic.auto._
import io.circe.parser.decode
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import sttp.tapir.serverless.aws.cdk.test.IOLambdaHandlerV1
import sttp.tapir.serverless.aws.lambda.AwsResponse
import sttp.tapir.serverless.aws.lambda.{AwsCatsEffectServerOptions, AwsResponse}

import java.io.{ByteArrayInputStream, ByteArrayOutputStream}

Expand Down Expand Up @@ -81,10 +82,10 @@ class IOLambdaHandlerV1Test extends AnyFunSuite with Matchers {
new LambdaClientContext()
)

test("lambda handler") {
test("lambda handler without encoding") {
val output = new ByteArrayOutputStream()

val handler = new IOLambdaHandlerV1
val handler = new IOLambdaHandlerV1(AwsCatsEffectServerOptions.noEncoding[IO])
handler.handleRequest(new ByteArrayInputStream(input.getBytes), output, context)

val expected = AwsResponse(
Expand All @@ -96,4 +97,20 @@ class IOLambdaHandlerV1Test extends AnyFunSuite with Matchers {

decode[AwsResponse](output.toString()) shouldBe Right(expected)
}

test("lambda handler with default encoding") {
val output = new ByteArrayOutputStream()

val handler = new IOLambdaHandlerV1(AwsCatsEffectServerOptions.default[IO])
handler.handleRequest(new ByteArrayInputStream(input.getBytes), output, context)

val expected = AwsResponse(
isBase64Encoded = true,
200,
Map("Content-Length" -> "9", "Content-Type" -> "text/plain; charset=UTF-8"),
"SGkhIEp1bGll"
)

decode[AwsResponse](output.toString()) shouldBe Right(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import cats.effect.unsafe.implicits.global
import com.amazonaws.services.lambda.runtime.Context
import io.circe.generic.auto._
import sttp.tapir.server.ServerEndpoint
import sttp.tapir.serverless.aws.lambda.{AwsRequestV1, LambdaHandler}
import sttp.tapir.serverless.aws.lambda.{AwsRequestV1, AwsServerOptions, LambdaHandler}

import java.io.{InputStream, OutputStream}

class IOLambdaHandlerV1 extends LambdaHandler[IO, AwsRequestV1] {
class IOLambdaHandlerV1(options: AwsServerOptions[IO]) extends LambdaHandler[IO, AwsRequestV1](options) {

override protected def getAllEndpoints: List[ServerEndpoint[Any, IO]] = TestEndpoints.all[IO].toList

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import java.nio.charset.StandardCharsets
* @tparam R
* AWS API Gateway request type [[AwsRequestV1]] or [[AwsRequest]]. At the moment mapping is required as there is no support for
* generating API Gateway V2 definitions with AWS CDK v2.
* @param options
* Server options of type AwsServerOptions.
*/
abstract class LambdaHandler[F[_]: Sync, R: Decoder] extends RequestStreamHandler {
abstract class LambdaHandler[F[_]: Sync, R: Decoder](options: AwsServerOptions[F]) extends RequestStreamHandler {

protected def getAllEndpoints: List[ServerEndpoint[Any, F]]

protected def process(input: InputStream, output: OutputStream): F[Unit] = {
val server: AwsCatsEffectServerInterpreter[F] =
AwsCatsEffectServerInterpreter(AwsCatsEffectServerOptions.noEncoding[F])
val server: AwsCatsEffectServerInterpreter[F] = AwsCatsEffectServerInterpreter(options)

for {
allBytes <- Sync[F].blocking(input.readAllBytes())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.nio.charset.StandardCharsets
*
* @tparam Env
* The Environment type of the handler .
* @tparam options
* @param options
* Server options of type AwsServerOptions.
*/
abstract class ZioLambdaHandler[Env: RIOMonadError](options: AwsServerOptions[RIO[Env, *]]) {
Expand Down

0 comments on commit a7e3d39

Please sign in to comment.