Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement middleware and version commands in the rage/cli.rb file. #99

Merged
merged 2 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/rage/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "thor"
require "rack"
require "rage/version"

module Rage
class CLI < Thor
Expand Down Expand Up @@ -112,6 +113,29 @@ def console
IRB.start
end

desc "middleware", "List Rack middleware stack enabled for the application"
def middleware
app = ::Rack::Builder.parse_file(options[:config] || "config.ru")
app = app[0] if app.is_a?(Array)
middlewares = []

while app
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to use Rage.config.middleware.middlewares instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rsamoilov - Thank you for your comment.
What do you think about the below approach?

def middleware
  environment

  Rage.config.middleware.middlewares.each do |middleware|
    say "use #{middleware.first.inspect}"
  end
end

This method gives me the following output in the rage test app

➜  rage-test-app git:(master) ✗ rage middleware
use Rage::FiberWrapper
use Rage::Reloader
use Rage::Cors

Is it ok to call environment here or is there any better way to initialise the Rage::Configuration so that Rage.config is available in the middleware method?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, calling environment should be ok

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. Thank you.

if app.instance_variable_defined?(:@app)
middlewares << app.class
app = app.instance_variable_get(:@app)
else
break
end
end

middlewares.each { |middleware| puts middleware }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make it look a bit closer to the output of rails middleware?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rsamoilov - Thank you for the comment. I think the line will produce a similar output. What do you think?

say "use #{middleware.first.inspect}"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Probably calling name would be a bit more clear.

say "use #{middleware.first.name}"

end

desc "version", "Return the current version of the framework"
def version
puts Rage::VERSION
end

private

def environment
Expand Down
46 changes: 46 additions & 0 deletions spec/rage/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require "rage/cli"

RSpec.describe Rage::CLI do
subject(:rage_cli) { described_class.new }

describe "#middleware" do
let(:config_ru) { "spec/rspec/config.ru" }

before do
allow(rage_cli).to receive(:options).and_return(config: config_ru)
allow(Rack::Builder).to receive(:parse_file).with(config_ru).and_return([app])
end

context "when middleware stack is present" do
let(:app) do
Rack::Builder.app do
use Rage::FiberWrapper
use Rage::Reloader
run ->(env) { [200, { "Content-Type" => "text/plain" }, ["OK"]] }
end
end

it "lists the middleware stack" do
expect { rage_cli.middleware }.to output(/Rage::FiberWrapper\nRage::Reloader/).to_stdout
end
end

context "when middleware stack is empty" do
let(:app) { ->(env) { [200, { "Content-Type" => "text/plain" }, ["OK"]] } }

it "does not list any middleware" do
expect { rage_cli.middleware }.to output("").to_stdout
end
end
end

describe "#version" do
before do
stub_const("Rage::VERSION", "1.0.0")
end

it "returns the current version of the framework" do
expect { rage_cli.version }.to output("1.0.0\n").to_stdout
end
end
end
Loading