Skip to content

Instantly share code, notes, and snippets.

@aaripurna
Created June 23, 2022 14:27
Show Gist options
  • Save aaripurna/5d188d075eb95d10359b91a965a7be8f to your computer and use it in GitHub Desktop.
Save aaripurna/5d188d075eb95d10359b91a965a7be8f to your computer and use it in GitHub Desktop.
require "rack"
require "puma"
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Hello World']] }
class ApiKeyValidator
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.has_header?('X-API-KEY') && request.get_header('X-API-KEY') == ENV['API-KEY']
return @app.call(env)
end
[401, {'Content-Type' => 'text/plain'}, ['Unauthorized' ]]
end
end
use ApiKeyValidator
map '/posts' do
run lambda {|env| [200, { 'Content-Type' => 'text/plain'}, ['Post 1', 'Post 2', 'Post 3']]}
end
run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment