Created
June 23, 2022 14:27
-
-
Save aaripurna/5d188d075eb95d10359b91a965a7be8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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