Experiments with adding support for a variety of different handler types
Hey @spinscale,
I hope I'm not inundating you with too many pull requests!
I figured I'd try and learn some crystal and experiment with ways to make the handlers more generic and maintain a nice rubyish feel.
There is a lot of hacking here so I hope it isn't too distracting.
What I'm trying to accomplish is to allow for handlers that provide some convience and safety at the same time for different aws lambda use cases.
Some different scenarios I've come up with in this pull request:
runtime = Lambda::Runtime.new
# a default handler that provides a JSON:Any -> JSON:Any
runtime.register_handler do |json|
Lambda::Builder::HTTPResponse.new(200, "Hello from Crystal")
end
# a named handler that provides a HTTPRequest -> HTTPResponse
runtime.register_handler("my_handler", Lambda::Builder::HTTPRequest, Lambda::Builder::HTTPResponse) do |request|
Lambda::Builder::HTTPResponse.new(200, "Hello from Crystal")
end
# a default handler that provides a APIGatewayV2HTTPRequest -> APIGatewayV2HTTPResponse
runtime.register_handler(Lambda::Events::APIGatewayV2HTTPRequest, Lambda::Events::APIGatewayV2HTTPResponse) do |request|
Lambda::Events::APIGatewayV2HTTPResponse.new(200, "yolo")
end
This is accomplished by adding these additional register_handler definitions to the Runtime:
alias Handler = (String -> String) |
(JSON::Any -> JSON::Any) |
(Lambda::Builder::HTTPRequest -> Lambda::Builder::HTTPResponse) |
(Lambda::Events::APIGatewayV2HTTPRequest -> Lambda::Events::APIGatewayV2HTTPResponse)
def register_handler(name : String, input : T.class, output : U.class, &block : T -> U) forall T, U
handler = Proc(T, U).new &block
raise "unsupported handler '#{typeof(handler)}'' must be typeof '#{typeof(Handler)}'" unless handler.is_a?(Handler)
self.handlers[name] = handler
end
def register_handler(input : T.class, output : U.class, &block : T -> U) forall T, U
register_handler("default", input, output, &block)
end
def register_handler(name : String, &block : JSON::Any -> JSON::Any)
register_handler(name, JSON::Any, JSON::Any, &block)
end
def register_handler(&block : JSON::Any -> JSON::Any)
register_handler("default", JSON::Any, JSON::Any, &block)
end
As well as additional logic to handle the specific types:
def execute_handler(handler : Handler, body)
case handler
in Proc(String, String)
handler.call(body)
in Proc(Lambda::Builder::HTTPRequest, Lambda::Builder::HTTPResponse)
req = Lambda::Builder::HTTPRequest.new(JSON.parse(body))
handler.call(req).as_json.to_json
in Proc(Lambda::Events::APIGatewayV2HTTPRequest, Lambda::Events::APIGatewayV2HTTPResponse)
request = Lambda::Events::APIGatewayV2HTTPRequest.from_json(body)
handler.call(request).to_json
in Proc(JSON::Any, JSON::Any)
handler.call(JSON.parse(body)).to_json
end
end
I'm still rather new to crystal so doing a bunch of fighting with the type system still. If you have any better ideas/approaches please let me know.
If you think this is a decent approach I'll clean it up, get tests in and add the additional lambda events supported by aws.