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

Fix issue setting environment from command line option #1547

Merged
merged 1 commit into from Aug 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 0 additions & 52 deletions lib/sinatra/base.rb
Expand Up @@ -1831,58 +1831,6 @@ class << self
content_type 'text/html'
'<h1>Internal Server Error</h1>'
end

configure :development do
get '/__sinatra__/:image.png' do
filename = File.dirname(__FILE__) + "/images/#{params[:image].to_i}.png"
content_type :png
send_file filename
end

error NotFound do
content_type 'text/html'

if self.class == Sinatra::Application
code = <<-RUBY.gsub(/^ {12}/, '')
#{request.request_method.downcase} '#{request.path_info}' do
"Hello World"
end
RUBY
else
code = <<-RUBY.gsub(/^ {12}/, '')
class #{self.class}
#{request.request_method.downcase} '#{request.path_info}' do
"Hello World"
end
end
RUBY

file = settings.app_file.to_s.sub(settings.root.to_s, '').sub(/^\//, '')
code = "# in #{file}\n#{code}" unless file.empty?
end

(<<-HTML).gsub(/^ {10}/, '')
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Sinatra doesn’t know this ditty.</h2>
<img src='#{uri "/__sinatra__/404.png"}'>
<div id="c">
Try this:
<pre>#{Rack::Utils.escape_html(code)}</pre>
</div>
</body>
</html>
HTML
end
end
end

# Execution context for classic style (top-level) applications. All
Expand Down
73 changes: 63 additions & 10 deletions lib/sinatra/main.rb
@@ -1,16 +1,8 @@
require 'sinatra/base'

module Sinatra
class Application < Base

# we assume that the first file that requires 'sinatra' is the
# app_file. all other path related options are calculated based
# on this path by default.
set :app_file, caller_files.first || $0

set :run, Proc.new { File.expand_path($0) == File.expand_path(app_file) }

if run? && ARGV.any?
Copy link
Member Author

Choose a reason for hiding this comment

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

Anyone have thoughts on why run? needed to be true for this condition?

Copy link
Member

Choose a reason for hiding this comment

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

Probably no problem.

Copy link
Member

Choose a reason for hiding this comment

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

I reconsidered this, and we need to check by run? coz we need to take care that unexpected sets are not called. I'll fix it soon.

class Base
if ARGV.any?
require 'optparse'
OptionParser.new { |op|
op.on('-p port', 'set the port (default is 4567)') { |val| set :port, Integer(val) }
Expand All @@ -21,6 +13,67 @@ class Application < Base
op.on('-x', 'turn on the mutex lock (default is off)') { set :lock, true }
}.parse!(ARGV.dup)
end

configure :development do
get '/__sinatra__/:image.png' do
filename = File.dirname(__FILE__) + "/images/#{params[:image].to_i}.png"
content_type :png
send_file filename
end

error NotFound do
content_type 'text/html'

if self.class == Sinatra::Application
code = <<-RUBY.gsub(/^ {12}/, '')
#{request.request_method.downcase} '#{request.path_info}' do
"Hello World"
end
RUBY
else
code = <<-RUBY.gsub(/^ {12}/, '')
class #{self.class}
#{request.request_method.downcase} '#{request.path_info}' do
"Hello World"
end
end
RUBY

file = settings.app_file.to_s.sub(settings.root.to_s, '').sub(/^\//, '')
code = "# in #{file}\n#{code}" unless file.empty?
end

(<<-HTML).gsub(/^ {10}/, '')
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
body { text-align:center;font-family:helvetica,arial;font-size:22px;
color:#888;margin:20px}
#c {margin:0 auto;width:500px;text-align:left}
</style>
</head>
<body>
<h2>Sinatra doesn’t know this ditty.</h2>
<img src='#{uri "/__sinatra__/404.png"}'>
<div id="c">
Try this:
<pre>#{Rack::Utils.escape_html(code)}</pre>
</div>
</body>
</html>
HTML
end
end
end

class Application < Base
# we assume that the first file that requires 'sinatra' is the
# app_file. all other path related options are calculated based
# on this path by default.
set :app_file, caller_files.first || $0

set :run, Proc.new { File.expand_path($0) == File.expand_path(app_file) }
end

at_exit { Application.run! if $!.nil? && Application.run? }
Expand Down