Skip to content

Commit 1371526

Browse files
committed
Added akka-http example
1 parent 74c44d6 commit 1371526

File tree

4 files changed

+66
-24
lines changed

4 files changed

+66
-24
lines changed

build.sbt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lazy val root = project
88
.in(file("."))
99
.settings(commonSettings)
1010
.settings(noPublishSettings)
11-
.aggregate(common, tests, http4s, akka, exampleHttp4s)
11+
.aggregate(common, tests, http4s, akka, exampleHttp4s, exampleAkka)
1212

1313
lazy val CirceVersion = "0.9.0"
1414
lazy val ScalaTestVersion = "3.0.4"
@@ -60,7 +60,6 @@ lazy val http4s = project
6060
.dependsOn(common)
6161
.dependsOn(tests % "test")
6262

63-
6463
lazy val akka = project
6564
.in(file("akka-http-lambda"))
6665
.settings(publishSettings)
@@ -88,14 +87,25 @@ lazy val exampleHttp4s = project
8887
moduleName := "example-http4s",
8988
assemblyJarName in assembly := "example-http4s.jar",
9089
libraryDependencies ++= Seq(
91-
"org.http4s" %% "http4s-circe" % Http4sVersion,
92-
"io.circe" %% "circe-parser" % CirceVersion,
93-
"io.circe" %% "circe-generic" % CirceVersion,
9490
"org.http4s" %% "http4s-dsl" % Http4sVersion
9591
)
9692
)
9793
.dependsOn(http4s)
9894

95+
lazy val exampleAkka = project
96+
.in(file("example-akka-http"))
97+
.settings(noPublishSettings)
98+
.settings(commonSettings)
99+
.settings(
100+
moduleName := "example-akka-http",
101+
assemblyJarName in assembly := "example-akka-http.jar",
102+
libraryDependencies ++= Seq(
103+
"com.typesafe.akka" %% "akka-http" % "10.1.1",
104+
"com.typesafe.akka" %% "akka-stream" % "2.5.11"
105+
)
106+
)
107+
.dependsOn(akka)
108+
99109
lazy val noPublishSettings = Seq(
100110
publish := {},
101111
publishLocal := {},

example-akka-http/serverless.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
service: example-akka-http
2+
3+
provider:
4+
name: aws
5+
runtime: java8
6+
region: us-west-2
7+
stage: dev
8+
9+
package:
10+
artifact: target/scala-2.12/example-akka-http.jar
11+
12+
functions:
13+
api:
14+
handler: io.github.howardjohn.lambda.akka.example.Route$EntryPoint::handle
15+
events:
16+
- http:
17+
path: "{proxy+}"
18+
method: any
19+
cors: true
20+
# Uncomment below to keep the application warm
21+
# - schedule:
22+
# rate: rate(4 minutes)
23+
# input:
24+
# httpMethod: GET
25+
# path: /hello/keepWarm
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.howardjohn.lambda.akka.example
2+
3+
import akka.actor.ActorSystem
4+
import akka.http.scaladsl.server.Route
5+
import akka.http.scaladsl.server.Directives._
6+
import akka.stream.ActorMaterializer
7+
import io.github.howardjohn.lambda.akka.AkkaHttpLambdaHandler
8+
9+
object Route {
10+
// Set up the route
11+
val route: Route =
12+
path("hello" / Segment) { name: String =>
13+
get {
14+
complete(s"Hello, $name!")
15+
}
16+
}
17+
18+
// Set up dependencies
19+
implicit val system: ActorSystem = ActorSystem("example")
20+
implicit val materializer: ActorMaterializer = ActorMaterializer()
21+
implicit val ec = scala.concurrent.ExecutionContext.Implicits.global
22+
23+
// Define the entry point for Lambda
24+
class EntryPoint extends AkkaHttpLambdaHandler(route)
25+
}

example-http4s/src/main/scala/io/github/howardjohn/lambda/http4s/example/Route.scala

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,14 @@ package io.github.howardjohn.lambda.http4s.example
33
import cats.effect.IO
44
import io.github.howardjohn.lambda.http4s.Http4sLambdaHandler
55
import org.http4s.HttpService
6-
import org.http4s.circe.jsonOf
7-
import io.circe.generic.auto._
86
import org.http4s.dsl.io._
97

108
object Route {
11-
implicit val inputDecoder = jsonOf[IO, Input]
12-
139
// Set up the route
1410
val service: HttpService[IO] = HttpService[IO] {
15-
case GET -> Root / "hello" / name :? TimesQueryMatcher(times) =>
16-
Ok(s"Hello, $name." * times.getOrElse(1))
17-
case req @ POST -> Root / "message" =>
18-
for {
19-
inp <- req.as[Input]
20-
resp <- Ok(inp.messages.map(msg => s"$msg, ${inp.name}!").mkString(" "))
21-
} yield resp
11+
case GET -> Root / "hello" / name => Ok(s"Hello, $name!")
2212
}
2313

2414
// Define the entry point for Lambda
25-
// Referenced as ` io.github.howardjohn.http4s.lambda.example$Route::handler` in Lambda
2615
class EntryPoint extends Http4sLambdaHandler(service)
27-
28-
case class Input(
29-
name: String,
30-
messages: Seq[String]
31-
)
32-
33-
object TimesQueryMatcher extends OptionalQueryParamDecoderMatcher[Int]("times")
3416
}

0 commit comments

Comments
 (0)