Sha256: 3210179346896418f17fb96bc3afb6e41b918fe4e8dd20f108aaea4c29e9d644

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

if ENV["LAMBDA_TASK_ROOT"].present?

  ENV['BUNDLE_GEMFILE'] ||= "#{ENV["LAMBDA_TASK_ROOT"]}/Gemfile.serverless"
  ENV['BUNDLE_WITHOUT'] ||= "development:test"
  
  ENV["RAILS_ENV"] ||= "production"

  require "bundler"

  Bundler.setup(:default, ENV["RAILS_ENV"])

  require "active_record"

  Bundler.require(:default, ENV["RAILS_ENV"])

  ActiveSupport::Dependencies.autoload_paths += Dir[ "#{ENV["LAMBDA_TASK_ROOT"]}/app/**/" ]

  Dir["#{ENV["LAMBDA_TASK_ROOT"]}/config/serverless_initializers/*.rb"].each { |file| require file }
  I18n.load_path << Dir["#{ENV["LAMBDA_TASK_ROOT"]}/config/locales/*.yml"]

  ActiveRecord::Base.configurations = {}
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ENV["RAILS_ENV"]])
end

class DefaultApiEntry
  def self.call(env)
    status  = 200
    headers = { "Content-Type" => "text/html" }
    body    = ['Hello World.']

    [status, headers, body]
  end
end

module ServerlessHub
  module Handler
    $app ||= Rack::Builder.new do
      app = begin
              Kernel.const_get(ENV.fetch('API_ENTRY_CLASS_NAME', 'ApiEntry'))
            rescue
              DefaultApiEntry
            end

      use AuthorizerTokenDecoder
      use Authorizer
      run app
    end.to_app
    
    def self.call(event: {}, context: {})
      return "Warm Up" if event["source"] == "serverless-plugin-warmup"
      
      Lamby.handler $app, event, context, rack: :api
    rescue Exception => msg
      p "errors: #{msg}"
      response = {
        "statusCode" => 500,
        "body" => ENV["RAILS_ENV"] == "production" ? "Something wrong happened" : msg,
      }
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
serverless_hub-1.1.1 lib/serverless_hub/handler.rb
m_serverless_hub-0.0.1 lib/serverless_hub/handler.rb
serverless_hub-1.0.10 lib/serverless_hub/handler.rb