Last active
November 8, 2024 13:54
-
-
Save marcelorxaviers/874ec5f3a950ac40b430a01f8946777e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# frozen_string_literal: true | |
# ... | |
require_relative "../lib/middleware/marcelo" | |
# ... | |
module CoolName | |
class Application < Rails::Application | |
#... | |
# Before anything you get me | |
# config.middleware.insert_before 0, Marcelo | |
# Appends it to the end of the middleware chain | |
config.middleware.use Marcelo | |
#... | |
end | |
end |
This file contains hidden or 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
# frozen_string_literal: true | |
# I am the man in the middle(ware) | |
# | |
# Reading material: | |
# - https://younes.codes/posts/what-is-rack | |
# - https://guides.rubyonrails.org/rails_on_rack.html | |
class Marcelo | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
return @app.call(env) unless "money".in?(env["REQUEST_PATH"].to_s.downcase) | |
[ | |
Rack::Utils::SYMBOL_TO_STATUS_CODE[:misdirected_request], # Status code | |
{ denied: "you'll see no money here" }, # Headers | |
["Go back where you came from, mercenary."] # Body | |
] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
By doing the following:
The result should be: