require 'webrick' require 'net/http' require 'base64' require 'json' require 'httparty' require_relative '__NOW_HANDLER_FILENAME' SupportedHTTPMethods = { 'GET' => Net::HTTP::Get, 'POST' => Net::HTTP::Post, 'PATCH' => Net::HTTP::Patch, 'PUT' => Net::HTTP::Put, 'DELETE' => Net::HTTP::Delete, 'HEAD' => Net::HTTP::Head, 'OPTIONS' => Net::HTTP::Options, 'MOVE' => Net::HTTP::Move, 'COPY' => Net::HTTP::Copy, 'MKCOL' => Net::HTTP::Mkcol, 'LOCK' => Net::HTTP::Lock, 'UNLOCK' => Net::HTTP::Unlock, } def now__handler(event:, context:) if not Object.const_defined?('Handler') return { :statusCode => 500, :body => 'Handler not defined in lambda' } end payload = JSON.parse(event['body']) path = payload['path'] headers = payload['headers'] httpMethod = payload['method'] encoding = payload['encoding'] body = payload['body'] if not SupportedHTTPMethods.key?(httpMethod) return { :statusCode => 405, :body => 'Method Not Allowed' } end if (not body.nil? and not body.empty?) and (not encoding.nil? and encoding == 'base64') body = Base64.decode64(body) end server = WEBrick::HTTPServer.new :Port => 3000 if Handler.is_a?(Proc) server.mount_proc '/', Handler else server.mount '/', Handler end Thread.new(server) do |server| server.start end req = HTTParty::Request.new( SupportedHTTPMethods[httpMethod], 'http://localhost:3000' + path, { headers: headers, body: body, } ) res = req.perform return_hashmap = { :statusCode => res.code, :headers => res.headers } if not res.nil? return_hashmap['body'] = res.body end return_hashmap end