Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiments with adding support for a variety of different handler types #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dyslexic-degenerate
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant