This repository was archived by the owner on Sep 21, 2023. It is now read-only.
forked from howardjohn/scala-server-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to be generic to the server type
- Loading branch information
1 parent
e62325b
commit ec4de8b
Showing
14 changed files
with
204 additions
and
235 deletions.
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
41 changes: 41 additions & 0 deletions
41
common/src/main/scala/io/github/howardjohn/lambda/Encoding.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,41 @@ | ||
package io.github.howardjohn.lambda | ||
|
||
import io.circe | ||
import io.circe.generic.auto._ | ||
import io.circe.parser.decode | ||
import io.circe.syntax._ | ||
|
||
object Encoding { | ||
case class ProxyRequest( | ||
httpMethod: String, | ||
path: String, | ||
headers: Option[Map[String, String]], | ||
body: Option[String], | ||
queryStringParameters: Option[Map[String, String]] | ||
) | ||
|
||
case class ProxyResponse( | ||
statusCode: Int, | ||
headers: Map[String, String], | ||
body: String | ||
) | ||
|
||
def parseRequest(rawInput: String): Either[circe.Error, ProxyRequest] = decode[ProxyRequest](rawInput) | ||
|
||
def encodeResponse(response: ProxyResponse): String = | ||
response.asJson.noSpaces | ||
|
||
def reconstructPath(request: ProxyRequest): String = { | ||
val requestString = request.queryStringParameters | ||
.map { | ||
_.map { | ||
case (k, v) => s"$k=$v" | ||
}.mkString("&") | ||
} | ||
.map { qs => | ||
if (qs.isEmpty) "" else "?" + qs | ||
} | ||
.getOrElse("") | ||
request.path + requestString | ||
} | ||
} |
14 changes: 4 additions & 10 deletions
14
...owardjohn/http4s/lambda/IOStreamOps.scala → ...ithub/howardjohn/lambda/IOStreamOps.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 |
---|---|---|
@@ -1,29 +1,23 @@ | ||
package io.github.howardjohn.http4s.lambda | ||
package io.github.howardjohn.lambda | ||
|
||
import java.io.{InputStream, OutputStream} | ||
import java.nio.charset.StandardCharsets | ||
|
||
import cats.effect.IO | ||
|
||
import scala.io.Source | ||
|
||
object IOStreamOps { | ||
object StreamOps { | ||
implicit class InputStreamOps(val is: InputStream) extends AnyVal { | ||
|
||
def consume(): IO[String] = IO { | ||
def consume(): String = { | ||
val contents = Source.fromInputStream(is).mkString | ||
is.close() | ||
contents | ||
} | ||
|
||
} | ||
|
||
implicit class OutputStreamOps(val os: OutputStream) extends AnyVal { | ||
|
||
def writeAndClose(contents: String): IO[Unit] = IO { | ||
def writeAndClose(contents: String): Unit = { | ||
os.write(contents.getBytes(StandardCharsets.UTF_8)) | ||
os.close() | ||
} | ||
|
||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
common/src/main/scala/io/github/howardjohn/lambda/LambdaHandler.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,21 @@ | ||
package io.github.howardjohn.lambda | ||
|
||
import java.io.{InputStream, OutputStream} | ||
|
||
import io.github.howardjohn.lambda.Encoding._ | ||
import io.github.howardjohn.lambda.StreamOps._ | ||
|
||
trait LambdaHandler { | ||
def handleRequest(request: ProxyRequest): ProxyResponse | ||
|
||
def handle(is: InputStream, os: OutputStream): Unit = { | ||
val rawInput = is.consume() | ||
val request = parseRequest(rawInput).fold( | ||
e => throw e, | ||
identity | ||
) | ||
val rawResponse = handleRequest(request) | ||
val response = encodeResponse(rawResponse) | ||
os.writeAndClose(response) | ||
} | ||
} |
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,25 @@ | ||
service: example-http4s | ||
|
||
provider: | ||
name: aws | ||
runtime: java8 | ||
region: us-west-2 | ||
stage: dev | ||
|
||
package: | ||
artifact: target/scala-2.12/example-http4s.jar | ||
|
||
functions: | ||
api: | ||
handler: io.github.howardjohn.lambda.http4s.example.Route$EntryPoint::handle | ||
events: | ||
- http: | ||
path: "{proxy+}" | ||
method: any | ||
cors: true | ||
# Uncomment below to keep the application warm | ||
# - schedule: | ||
# rate: rate(4 minutes) | ||
# input: | ||
# httpMethod: GET | ||
# path: /hello/keepWarm |
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 was deleted.
Oops, something went wrong.
69 changes: 0 additions & 69 deletions
69
http4s-lambda/src/main/scala/io/github/howardjohn/http4s/lambda/Encoding.scala
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
http4s-lambda/src/main/scala/io/github/howardjohn/http4s/lambda/LambdaHandler.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.