Last active
June 23, 2022 13:57
-
-
Save aaripurna/6ac923e52bba57f5390f13c9e97002d4 to your computer and use it in GitHub Desktop.
Rack Middleware
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
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment