From 3ae2e01a0fa88596fedf2a0b62079c97aa4d5bcc Mon Sep 17 00:00:00 2001 From: Jordan Owens Date: Sat, 6 Jul 2019 23:34:07 -0400 Subject: [PATCH] Fix issue setting environment from command line option When setting environment to production from command line, some development configurations were already registered. For instance the development NotFound error handler was setup. Command line options are now parsed before environment configurations are run. --- lib/sinatra/base.rb | 52 -------------------------------- lib/sinatra/main.rb | 73 ++++++++++++++++++++++++++++++++++++++------- 2 files changed, 63 insertions(+), 62 deletions(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index a02fdf0e78..c36fbcaf00 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -1831,58 +1831,6 @@ class << self content_type 'text/html' '

Internal Server Error

' 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}/, '') - - - - - - -

Sinatra doesn’t know this ditty.

- -
- Try this: -
#{Rack::Utils.escape_html(code)}
-
- - - HTML - end - end end # Execution context for classic style (top-level) applications. All diff --git a/lib/sinatra/main.rb b/lib/sinatra/main.rb index 5b14442dd9..d9ca55c017 100644 --- a/lib/sinatra/main.rb +++ b/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? + 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) } @@ -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}/, '') + + + + + + +

Sinatra doesn’t know this ditty.

+ +
+ Try this: +
#{Rack::Utils.escape_html(code)}
+
+ + + 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? }