Skip to content

Instantly share code, notes, and snippets.

@juancho088
Created June 10, 2018 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juancho088/1b8a308cda44def9155e586794b155d2 to your computer and use it in GitHub Desktop.
Save juancho088/1b8a308cda44def9155e586794b155d2 to your computer and use it in GitHub Desktop.
New Handler Implementation for a Kotlin Serverless
package com.myblockbuster
import com.amazonaws.services.lambda.runtime.Context
import com.amazonaws.services.lambda.runtime.RequestHandler
import com.myblockbuster.ApiGatewayResponse.Companion.LOG
import com.myblockbuster.core.*
import com.myblockbuster.core.dispatchers.RequestDispatcher
open class Handler: RequestHandler<Map<String, Any>, ApiGatewayResponse> {
var requestDispatcher: RequestDispatcher = RequestDispatcher()
override fun handleRequest(input:Map<String, Any>, context: Context): ApiGatewayResponse {
var status = 500
var body: Any? = EmptyModel()
try {
body = requestDispatcher.locate(ApiGatewayRequest(input, context)) ?: EmptyModel()
status = when (body is EmptyModel) {
true -> 204
false -> 200
}
}
catch (e: MyException) {
LOG.error(e.message, e)
status = e.code
body = ErrorModel(e.message)
}
catch (e: Throwable) {
LOG.error(e.message, e)
status = 500
body = ErrorModel("Internal server error")
}
finally {
return ApiGatewayResponse.build {
statusCode = status
if (body is Model)
objectBody = body as Model?
else
listBody = body as List<Any>
headers = mapOf("X-Powered-By" to "AWS Lambda & Serverless")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment