Skip to content

Commit

Permalink
updated package
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Coser committed Apr 8, 2020
1 parent de7ac9d commit 4a06933
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 74 deletions.
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.3.70'
id 'org.jetbrains.kotlin.jvm' version '1.3.71'
id 'java-library'
}

Expand All @@ -14,11 +14,11 @@ dependencies {

compile 'com.amazonaws:aws-lambda-java-core:1.2.0'
compile 'com.google.code.gson:gson:2.8.6'
compile 'com.github.DaikonWeb:daikon-core:0.0.2'
compile 'com.github.DaikonWeb:daikon-core:1.1.0'

testImplementation "org.junit.jupiter:junit-jupiter-api:5.5.2"
testImplementation "org.junit.jupiter:junit-jupiter-engine:5.5.2"
testImplementation "net.wuerl.kotlin:assertj-core-kotlin:0.2.1"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.2'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.5.2'
testImplementation 'net.wuerl.kotlin:assertj-core-kotlin:0.2.1'
testImplementation 'io.mockk:mockk:1.9.3.kotlin12'
testRuntimeOnly 'net.bytebuddy:byte-buddy:1.10.6'
}
Expand Down
103 changes: 34 additions & 69 deletions src/main/kotlin/daikon/lambda/HttpHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ import java.io.OutputStream
abstract class HttpHandler: RequestStreamHandler {
private val beforeActions = Routing()
private val routes = Routing()
private val exceptions = Exceptions()
private val afterActions = Routing()
private val basePath = mutableListOf("")

abstract fun routing()

override fun handleRequest(input: InputStream, output: OutputStream, context: com.amazonaws.services.lambda.runtime.Context) {
try {
output.json(execute(input.asString().toLambdaRequest()))
} catch (t: Throwable) {
println("${t.message}: ${t.stackTrace}")
output.json(LambdaResponse().apply { status(HttpStatus.INTERNAL_SERVER_ERROR_500) }.asMap())
}
output.json(execute(input.asString().toLambdaRequest()))
}

private fun InputStream.asString() = bufferedReader().use { it.readText() }
Expand All @@ -33,93 +29,62 @@ abstract class HttpHandler: RequestStreamHandler {
private fun execute(request: Request): Map<String, Any> {
val response = LambdaResponse()
routing()
RoutingHandler(beforeActions, routes, afterActions, NullContext()).execute(request, response)
RoutingHandler(beforeActions, routes, afterActions, NullContext(), exceptions).execute(request, response)
return response.asMap()
}

fun get(path: String, action: (Request, Response) -> Unit) {
get(path, DummyRouteAction(action))
}
fun exception(exception: Class<out Throwable>, action: (Request, Response, Context, Throwable) -> Unit)
= exception(exception, ContextExceptionAction(action))

fun get(path: String, action: (Request, Response, Context) -> Unit) {
get(path, ContextRouteAction(action))
}
fun exception(exception: Class<out Throwable>, action: (Request, Response, Throwable) -> Unit)
= exception(exception, DummyExceptionAction(action))

fun get(path: String, action: RouteAction) {
add(Method.GET, path, action)
fun exception(exception: Class<out Throwable>, action: ExceptionAction): HttpHandler {
exceptions.add(ExceptionRoute(exception, action))
return this
}

fun post(path: String, action: (Request, Response) -> Unit) {
post(path, DummyRouteAction(action))
}
fun get(path: String, action: (Request, Response) -> Unit) = get(path, DummyRouteAction(action))

fun post(path: String, action: (Request, Response, Context) -> Unit) {
post(path, ContextRouteAction(action))
}
fun get(path: String, action: (Request, Response, Context) -> Unit) = get(path, ContextRouteAction(action))

fun post(path: String, action: RouteAction) {
add(Method.POST, path, action)
}
fun get(path: String, action: RouteAction) = add(Method.GET, path, action)

fun put(path: String, action: (Request, Response) -> Unit) {
put(path, DummyRouteAction(action))
}
fun post(path: String, action: (Request, Response) -> Unit) = post(path, DummyRouteAction(action))

fun put(path: String, action: (Request, Response, Context) -> Unit) {
put(path, ContextRouteAction(action))
}
fun post(path: String, action: (Request, Response, Context) -> Unit) = post(path, ContextRouteAction(action))

fun put(path: String, action: RouteAction) {
add(Method.PUT, path, action)
}
fun post(path: String, action: RouteAction) = add(Method.POST, path, action)

fun delete(path: String, action: (Request, Response) -> Unit) {
delete(path, DummyRouteAction(action))
}
fun put(path: String, action: (Request, Response) -> Unit) = put(path, DummyRouteAction(action))

fun delete(path: String, action: (Request, Response, Context) -> Unit) {
delete(path, ContextRouteAction(action))
}
fun put(path: String, action: (Request, Response, Context) -> Unit) = put(path, ContextRouteAction(action))

fun delete(path: String, action: RouteAction) {
add(Method.DELETE, path, action)
}
fun put(path: String, action: RouteAction) = add(Method.PUT, path, action)

fun options(path: String, action: (Request, Response) -> Unit) {
options(path, DummyRouteAction(action))
}
fun delete(path: String, action: (Request, Response) -> Unit) = delete(path, DummyRouteAction(action))

fun options(path: String, action: (Request, Response, Context) -> Unit) {
options(path, ContextRouteAction(action))
}
fun delete(path: String, action: (Request, Response, Context) -> Unit) = delete(path, ContextRouteAction(action))

fun options(path: String, action: RouteAction) {
add(Method.OPTIONS, path, action)
}
fun delete(path: String, action: RouteAction) = add(Method.DELETE, path, action)

fun head(path: String, action: (Request, Response) -> Unit) {
head(path, DummyRouteAction(action))
}
fun options(path: String, action: (Request, Response) -> Unit) = options(path, DummyRouteAction(action))

fun head(path: String, action: (Request, Response, Context) -> Unit) {
head(path, ContextRouteAction(action))
}
fun options(path: String, action: (Request, Response, Context) -> Unit) = options(path, ContextRouteAction(action))

fun head(path: String, action: RouteAction) {
add(Method.HEAD, path, action)
}
fun options(path: String, action: RouteAction) = add(Method.OPTIONS, path, action)

fun any(path: String, action: (Request, Response) -> Unit) {
any(path, DummyRouteAction(action))
}
fun head(path: String, action: (Request, Response) -> Unit) = head(path, DummyRouteAction(action))

fun any(path: String, action: (Request, Response, Context) -> Unit) {
any(path, ContextRouteAction(action))
}
fun head(path: String, action: (Request, Response, Context) -> Unit) = head(path, ContextRouteAction(action))

fun any(path: String, action: RouteAction) {
add(Method.ANY, path, action)
}
fun head(path: String, action: RouteAction) = add(Method.HEAD, path, action)

fun any(path: String, action: (Request, Response) -> Unit) = any(path, DummyRouteAction(action))

fun any(path: String, action: (Request, Response, Context) -> Unit) = any(path, ContextRouteAction(action))

fun any(path: String, action: RouteAction) = add(Method.ANY, path, action)

fun before(path: String = "/*", action: (Request, Response) -> Unit) {
beforeActions.add(
Expand Down
3 changes: 3 additions & 0 deletions src/test/kotlin/daikon/lambda/RequestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class RequestTest {
val input = apiGatewayEvent(method = "POST", path = "/")

runHandler(input, output) {
exception(Throwable::class.java) { _, res, _ -> res.status(INTERNAL_SERVER_ERROR_500) }
post("/") { req, res -> res.write("hello ${req.header("name")}") }
}

Expand Down Expand Up @@ -137,6 +138,7 @@ class RequestTest {
val input = apiGatewayEvent(method = "GET", path = "/")

runHandler(input, output) {
exception(Throwable::class.java) { _, res, _ -> res.status(INTERNAL_SERVER_ERROR_500) }
get("/") { req, res -> res.write(req.param(":baz")) }
}

Expand Down Expand Up @@ -213,6 +215,7 @@ class RequestTest {
val input = apiGatewayEvent(method = "GET", path = "/")

runHandler(input, output) {
exception(Throwable::class.java) { _, res, _ -> res.status(INTERNAL_SERVER_ERROR_500) }
get("/") { req, res ->
val attribute = req.attribute<String>("any")
res.write("Hello $attribute")
Expand Down

0 comments on commit 4a06933

Please sign in to comment.