Created
March 28, 2018 15:44
-
-
Save amoj/5e643afae1ab96497e3fa0521b7febbc to your computer and use it in GitHub Desktop.
A sample Kotlin app using Spark framework in AWS Lambda.
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
package com.example.lambda | |
import com.amazonaws.serverless.exceptions.ContainerInitializationException | |
import com.amazonaws.serverless.proxy.model.AwsProxyRequest | |
import com.amazonaws.serverless.proxy.model.AwsProxyResponse | |
import com.amazonaws.serverless.proxy.spark.SparkLambdaContainerHandler | |
import com.amazonaws.services.lambda.runtime.Context | |
import com.amazonaws.services.lambda.runtime.RequestHandler | |
import spark.Request | |
import spark.Response | |
import spark.Spark.* | |
class Handler @Throws(ContainerInitializationException::class) | |
constructor() : RequestHandler<AwsProxyRequest, AwsProxyResponse> { | |
private val handler = SparkLambdaContainerHandler.getAwsProxyHandler() | |
private var initialized = false | |
override fun handleRequest(request: AwsProxyRequest, context: Context?): AwsProxyResponse { | |
if(!initialized){ | |
initRoutes() | |
initialized = true | |
} | |
return handler.proxy(request, context) | |
} | |
companion object { | |
@kotlin.jvm.JvmStatic | |
fun main(args: Array<String>){ | |
initRoutes() | |
} | |
private fun initRoutes(){ | |
initExceptionHandler { e -> | |
println("Spark initialization failed") | |
System.exit(100) | |
} | |
path("/greetings"){ | |
get(""){_, _ -> "Hello World"} | |
get("/:name"){req, res -> greetName( | |
req, | |
res | |
)} | |
} | |
} | |
private fun greetName(req: Request, res: Response): String { | |
val name = req.params("name") ?: "" | |
return "Hello $name" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment