-
Notifications
You must be signed in to change notification settings - Fork 427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/serverless zio #2975
Merged
kciesielski
merged 6 commits into
softwaremill:master
from
TigranOhanyan:feature/serverless-zio
Jun 26, 2023
Merged
Feature/serverless zio #2975
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7f51d28
feat: add serverless support for Zio
TigranOhanyan a6c03b5
fix: avoid using zio as a standalone package
TigranOhanyan 71bae56
fix: correct main class for LambdaSamTemplate
TigranOhanyan c919398
Merge branch 'softwaremill:master' into feature/serverless-zio
TigranOhanyan f0b57c0
fix: put a ')' t the end of logging
TigranOhanyan dec2e0b
fix: yet another ')' at the end of the logging
TigranOhanyan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions
23
...io-tests/src/main/scala/sttp/tapir/serverless/aws/ziolambda/tests/LambdaSamTemplate.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package sttp.tapir.serverless.aws.ziolambda.tests | ||
|
||
import sttp.tapir.serverless.aws.sam.{AwsSamInterpreter, AwsSamOptions, CodeSource} | ||
|
||
import java.nio.charset.StandardCharsets.UTF_8 | ||
import java.nio.file.{Files, Paths} | ||
|
||
object LambdaSamTemplate extends App { | ||
|
||
val jarPath = Paths.get("serverless/aws/lambda-zio-tests/target/jvm-2.13/tapir-aws-lambda-zio-tests.jar").toAbsolutePath.toString | ||
|
||
val samOptions: AwsSamOptions = AwsSamOptions( | ||
"Tests", | ||
source = CodeSource( | ||
"java11", | ||
jarPath, | ||
"sttp.tapir.serverless.aws.ziolambda.tests.ZioLambdaHandlerImpl::handleRequest" | ||
), | ||
memorySize = 1024 | ||
) | ||
val yaml = AwsSamInterpreter(samOptions).toSamTemplate(allEndpoints.map(_.endpoint).toList).toYaml | ||
Files.write(Paths.get("aws-lambda-zio-template.yaml"), yaml.getBytes(UTF_8)) | ||
} |
22 changes: 22 additions & 0 deletions
22
...tests/src/main/scala/sttp/tapir/serverless/aws/ziolambda/tests/ZioLambdaHandlerImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package sttp.tapir.serverless.aws.ziolambda.tests | ||
|
||
import com.amazonaws.services.lambda.runtime.{Context, RequestStreamHandler} | ||
import io.circe.generic.auto._ | ||
import sttp.tapir.serverless.aws.lambda.AwsRequest | ||
import sttp.tapir.serverless.aws.ziolambda.ZioLambdaHandler | ||
import sttp.tapir.ztapir.RIOMonadError | ||
import zio.{Runtime, Unsafe} | ||
|
||
import java.io.{InputStream, OutputStream} | ||
|
||
class ZioLambdaHandlerImpl extends RequestStreamHandler { | ||
private implicit val m = new RIOMonadError[Any] | ||
private val handler = ZioLambdaHandler.default[Any](allEndpoints.toList) | ||
|
||
override def handleRequest(input: InputStream, output: OutputStream, context: Context): Unit = { | ||
val runtime = Runtime.default | ||
Unsafe.unsafe { implicit unsafe => | ||
runtime.unsafe.run(handler.process[AwsRequest](input, output)).getOrThrowFiberFailure() | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...s/lambda-zio-tests/src/main/scala/sttp/tapir/serverless/aws/ziolambda/tests/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package sttp.tapir.serverless.aws.ziolambda | ||
|
||
import sttp.tapir.{endpoint, stringToPath} | ||
import sttp.tapir.tests.Basic._ | ||
import sttp.tapir.tests.Mapping.in_4query_out_4header_extended | ||
import sttp.tapir.tests.TestUtil.inputStreamToByteArray | ||
import sttp.tapir.ztapir.ZTapir | ||
import zio.ZIO | ||
|
||
import java.io.{ByteArrayInputStream, InputStream} | ||
|
||
package object tests extends ZTapir { | ||
|
||
type ZioEndpoint = ZServerEndpoint[Any, Any] | ||
|
||
// this endpoint is used to wait until sam local starts up before running actual tests | ||
val health_endpoint: ZioEndpoint = | ||
endpoint.get.in("health").zServerLogic(_ => ZIO.unit) | ||
|
||
val in_path_path_out_string_endpoint: ZioEndpoint = | ||
in_path_path_out_string.zServerLogic { case (fruit: String, amount: Int) => | ||
ZIO.attempt(s"$fruit $amount").mapError(_ => ()) | ||
} | ||
|
||
val in_string_out_string_endpoint: ZioEndpoint = | ||
in_string_out_string.in("string").zServerLogic(v => ZIO.attempt(v).mapError(_ => ())) | ||
|
||
val in_json_out_json_endpoint: ZioEndpoint = | ||
in_json_out_json.in("json").zServerLogic(v => ZIO.attempt(v).mapError(_ => ())) | ||
|
||
val in_headers_out_headers_endpoint: ZioEndpoint = | ||
in_headers_out_headers.zServerLogic(v => ZIO.attempt(v).mapError(_ => ())) | ||
|
||
val in_input_stream_out_input_stream_endpoint: ZioEndpoint = | ||
in_input_stream_out_input_stream.in("is").zServerLogic { is => | ||
ZIO.attempt(new ByteArrayInputStream(inputStreamToByteArray(is)): InputStream).orDie | ||
} | ||
|
||
val in_4query_out_4header_extended_endpoint: ZioEndpoint = | ||
in_4query_out_4header_extended.in("echo" / "query").zServerLogic(v => ZIO.attempt(v).mapError(_ => ())) | ||
|
||
val allEndpoints: Set[ZioEndpoint] = Set( | ||
health_endpoint, | ||
in_path_path_out_string_endpoint, | ||
in_string_out_string_endpoint, | ||
in_json_out_json_endpoint, | ||
in_headers_out_headers_endpoint, | ||
in_input_stream_out_input_stream_endpoint, | ||
in_4query_out_4header_extended_endpoint | ||
) | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: This change isn't needed, right? Or maybe I'm missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is required, because
sttp.tapir.serverless.aws.ziolambda.AwsZioServerInterpreter
depends onsttp.tapir.serverless.aws.lambda.AwsServerInterpreter
and for these two the closest common package issttp.tapir.serverless.aws
. That is why it should be private to[aws]
to make the code compile.