Skip to content

Commit

Permalink
Add Rack handler
Browse files Browse the repository at this point in the history
  • Loading branch information
elct9620 committed Jan 1, 2024
1 parent 4ed3f78 commit 8fb8bcf
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 7 deletions.
38 changes: 31 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
name: Ruby
name: CI

on:
push:
paths:
- '.github/workflows/ci.yml'
- 'lib/**'
- '*.gemspec'
- 'spec/**'
- 'Rakefile'
- 'Gemfile'
- '.rubocop.yml'
pull_request:
branches:
- main

pull_request:
schedule:
- cron: '30 4 * * *'
create:

jobs:
build:
rubocop:
runs-on: ubuntu-latest
name: Rubocop
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: '3.2'
bundler-cache: true
- name: Run linter
run: bundle exec rake rubocop

tests:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.3.0'

- '3.2'
- '3.1'
- '3.0'
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
- name: Run all tests
run: bundle exec rake
35 changes: 35 additions & 0 deletions lib/hanami/lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,41 @@ module Hanami
# @since 0.1.0
# @api private
module Lambda
@_mutex = Mutex.new

# Returns the Hanami::Lambda application.
#
# @return [Hanami::Lambda::Application] the application
# @raise [Hanami::AppLoadError] if the application isn't configured
#
# @api public
# @since 0.1.0
def app
@_mutex.synchronize do
unless defined?(@_app)
raise Hanami::AppLoadError,
"Hanami::Lambda.app is not yet configured. "
end

@_app
end
end

# @api private
# @since 0.1.0
def app=(klass)
@_mutex.synchronize do
raise AppLoadError, "Hanami::Lambda.app is already configured." if instance_variable_defined?(:@_app)

@_app = klass unless klass.name.nil?
end
end

def call(event:, context:)
Hanami.boot
app.call(event: event, context: context)
end

# @since 0.1.0
# @api private
def self.gem_loader
Expand Down
37 changes: 37 additions & 0 deletions lib/hanami/lambda/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

module Hanami
module Lambda
# The application to configure for AWS Lambda.
#
# @since 0.1.0
class Application
# @api private
def self.inherited(subclass)
super

Hanami::Lambda.app = subclass
subclass.extend(ClassMethods)
end

module ClassMethods
# Dispatch event to the handler
#
# @api private
# @since 0.1.0
def call(event:, context:)
handler = lookup(event: event, context: context)
handler.call
end

# Lookup the handler for the given event
#
# @api private
# @since 0.1.0
def lookup(event:, context:)
Rack.new(Hanami.app, event: event, context: context)
end
end
end
end
end
49 changes: 49 additions & 0 deletions lib/hanami/lambda/rack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

module Hanami
module Lambda
# Rack interface for AWS Lambda.
#
# @api private
# @since 0.1.0
class Rack
attr_reader :app, :event, :context

# @api private
def initialize(app, event:, context:)
@app = app
@event = event
@context = context
end

# Handle the request
#
# @return [Hash] the response
#
# @since 0.1.0
def call
status_code, headers, body = app.call(env)

{
statusCode: status_code,
headers: headers,
body: body.join
}
end

# Build the Rack environment
#
# @return [Hash] the Rack environment
#
# @since 0.1.0
def env
{
::Rack::REQUEST_METHOD => event["httpMethod"],
::Rack::PATH_INFO => event["path"] || "",
::Rack::VERSION => ::Rack::VERSION,
::Rack::RACK_INPUT => StringIO.new(event["body"] || "")
}
end
end
end
end

0 comments on commit 8fb8bcf

Please sign in to comment.