mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
* custom 404
* bug fixes * refactoring
This commit is contained in:
parent
1de67c2d6c
commit
3b491e00ab
9 changed files with 86 additions and 34 deletions
|
@ -37,6 +37,7 @@ Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/core_ext/*.rb')
|
||||||
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/*.rb')
|
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/lib/sinatra/*.rb')
|
||||||
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb')
|
Sinatra::Loader.load_files Dir.glob(SINATRA_ROOT + '/vendor/*/init.rb')
|
||||||
|
|
||||||
at_exit do
|
Sinatra::Environment.setup!
|
||||||
Sinatra::Server.new.start unless Sinatra::Server.running
|
Sinatra::Irb.start! if Sinatra::Options.console
|
||||||
end
|
|
||||||
|
at_exit { Sinatra::Server.new.start unless Sinatra::Server.running }
|
||||||
|
|
|
@ -9,7 +9,7 @@ module Sinatra
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(env)
|
def call(env)
|
||||||
Loader.reload! if Options.environment == :production
|
Loader.reload! if Options.environment == :development
|
||||||
|
|
||||||
@request = Rack::Request.new(env)
|
@request = Rack::Request.new(env)
|
||||||
|
|
||||||
|
|
16
lib/sinatra/environment.rb
Normal file
16
lib/sinatra/environment.rb
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
module Sinatra
|
||||||
|
module Environment
|
||||||
|
extend self
|
||||||
|
|
||||||
|
def setup!
|
||||||
|
Options.parse!(ARGV)
|
||||||
|
set_loggers
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_loggers(logger = Logger.new(open(Options.log_file, 'w')))
|
||||||
|
[Server, EventContext, Event, Dispatcher].each do |klass|
|
||||||
|
klass.logger = logger
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -4,7 +4,7 @@ module Sinatra
|
||||||
extend self
|
extend self
|
||||||
|
|
||||||
def reset!
|
def reset!
|
||||||
@events.clear
|
@events.clear if @events
|
||||||
end
|
end
|
||||||
|
|
||||||
def events
|
def events
|
||||||
|
@ -15,14 +15,18 @@ module Sinatra
|
||||||
(@events ||= []) << event
|
(@events ||= []) << event
|
||||||
end
|
end
|
||||||
|
|
||||||
def determine_event(verb, path)
|
def determine_event(verb, path, if_nil = :present_error)
|
||||||
EventManager.events.detect(method(:not_found)) do |e|
|
events.detect(method(if_nil)) do |e|
|
||||||
e.path == path && e.verb == verb
|
e.path == path && e.verb == verb
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def present_error
|
||||||
|
determine_event(:get, 404, :not_found)
|
||||||
|
end
|
||||||
|
|
||||||
def not_found
|
def not_found
|
||||||
Event.new(:get, nil) do
|
Event.new(:get, nil, false) do
|
||||||
status 404
|
status 404
|
||||||
views_dir SINATRA_ROOT + '/files'
|
views_dir SINATRA_ROOT + '/files'
|
||||||
|
|
||||||
|
@ -105,11 +109,11 @@ module Sinatra
|
||||||
|
|
||||||
attr_reader :path, :verb
|
attr_reader :path, :verb
|
||||||
|
|
||||||
def initialize(verb, path, &block)
|
def initialize(verb, path, register = true, &block)
|
||||||
@verb = verb
|
@verb = verb
|
||||||
@path = path
|
@path = path
|
||||||
@block = block
|
@block = block
|
||||||
EventManager.register_event(self)
|
EventManager.register_event(self) if register
|
||||||
end
|
end
|
||||||
|
|
||||||
def attend(request)
|
def attend(request)
|
||||||
|
|
|
@ -9,12 +9,13 @@ module Sinatra
|
||||||
attr_with_default :console, nil
|
attr_with_default :console, nil
|
||||||
|
|
||||||
def parse!(args)
|
def parse!(args)
|
||||||
|
return if @environment == :test
|
||||||
OptionParser.new do |opts|
|
OptionParser.new do |opts|
|
||||||
opts.on '-p port', '--port port', 'Set the port (default is 4567)' do |port|
|
opts.on '-p port', '--port port', 'Set the port (default is 4567)' do |port|
|
||||||
@port = port
|
@port = port
|
||||||
end
|
end
|
||||||
opts.on '-e environment', 'Set the environment (default if development)' do |env|
|
opts.on '-e environment', 'Set the environment (default if development)' do |env|
|
||||||
@environment = env
|
@environment = env.intern
|
||||||
end
|
end
|
||||||
opts.on '-c', '--console', 'Run in console mode' do
|
opts.on '-c', '--console', 'Run in console mode' do
|
||||||
@console = true
|
@console = true
|
||||||
|
@ -29,5 +30,9 @@ module Sinatra
|
||||||
def log_file
|
def log_file
|
||||||
File.dirname($0) + ('/%s.log' % environment)
|
File.dirname($0) + ('/%s.log' % environment)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_environment(env)
|
||||||
|
@environment = env
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,17 +9,13 @@ module Sinatra
|
||||||
|
|
||||||
def start
|
def start
|
||||||
begin
|
begin
|
||||||
setup_environment
|
|
||||||
|
|
||||||
Irb.start! if Options.console
|
|
||||||
|
|
||||||
tail_thread = tail(Options.log_file)
|
tail_thread = tail(Options.log_file)
|
||||||
Rack::Handler::Mongrel.run(Dispatcher.new, :Port => Options.port) do |server|
|
Rack::Handler::Mongrel.run(Dispatcher.new, :Port => Options.port) do |server|
|
||||||
logger.info "== Sinatra has taken the stage on port #{server.port}!"
|
puts "== Sinatra has taken the stage on port #{server.port}!"
|
||||||
trap("INT") do
|
trap("INT") do
|
||||||
server.stop
|
server.stop
|
||||||
self.class.running = false
|
self.class.running = false
|
||||||
logger.info "\n== Sinatra has ended his set (crowd applauds)"
|
puts "\n== Sinatra has ended his set (crowd applauds)"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
self.class.running = true
|
self.class.running = true
|
||||||
|
@ -32,18 +28,6 @@ module Sinatra
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def setup_environment
|
|
||||||
Options.parse!(ARGV)
|
|
||||||
set_loggers
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_loggers
|
|
||||||
logger = Logger.new(open(Options.log_file, 'w'))
|
|
||||||
[Server, EventContext, Event, Dispatcher].each do |klass|
|
|
||||||
klass.logger = logger
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def tail(log_file)
|
def tail(log_file)
|
||||||
FileUtils.touch(log_file)
|
FileUtils.touch(log_file)
|
||||||
cursor = File.size(log_file)
|
cursor = File.size(log_file)
|
||||||
|
|
|
@ -9,7 +9,8 @@ require File.dirname(__FILE__) + '/../lib/sinatra'
|
||||||
end
|
end
|
||||||
|
|
||||||
Sinatra::Server.running = true
|
Sinatra::Server.running = true
|
||||||
|
Sinatra::Options.set_environment :test
|
||||||
|
|
||||||
class Test::Unit::TestCase
|
class Test::Unit::TestCase
|
||||||
include TestMethods
|
include Sinatra::TestMethods
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,10 @@ require File.dirname(__FILE__) + '/../helper'
|
||||||
|
|
||||||
describe "When a dispatcher receives a request" do
|
describe "When a dispatcher receives a request" do
|
||||||
|
|
||||||
|
before(:each) do
|
||||||
|
Sinatra::EventManager.reset!
|
||||||
|
end
|
||||||
|
|
||||||
it "should attend to the event" do
|
it "should attend to the event" do
|
||||||
|
|
||||||
Sinatra::Event.new(:get, '/') do
|
Sinatra::Event.new(:get, '/') do
|
||||||
|
@ -14,7 +18,7 @@ describe "When a dispatcher receives a request" do
|
||||||
text.should.equal 'this is the index as a get'
|
text.should.equal 'this is the index as a get'
|
||||||
headers['Content-Type'].should.equal 'text/html'
|
headers['Content-Type'].should.equal 'text/html'
|
||||||
|
|
||||||
post_it "/test"
|
post_it "/"
|
||||||
|
|
||||||
status.should.equal 404
|
status.should.equal 404
|
||||||
text.scan("Not Found :: Sinatra").size.should.equal 1
|
text.scan("Not Found :: Sinatra").size.should.equal 1
|
||||||
|
@ -27,4 +31,30 @@ describe "When a dispatcher receives a request" do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should use custom error pages if present" do
|
||||||
|
Sinatra::Event.new(:get, 404) do
|
||||||
|
body 'custom 404'
|
||||||
|
end
|
||||||
|
|
||||||
|
get_it('/laksdjf').should.equal 'custom 404'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should reload app files unless in production" do
|
||||||
|
Sinatra::Event.new(:get, '/') {}
|
||||||
|
|
||||||
|
Sinatra::Options.expects(:environment).returns(:production)
|
||||||
|
Sinatra::Loader.expects(:reload!).never
|
||||||
|
get_it '/'
|
||||||
|
|
||||||
|
Sinatra::Options.expects(:environment).returns(:development)
|
||||||
|
Sinatra::Loader.expects(:reload!)
|
||||||
|
get_it '/'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not register not_found (otherwise we'll have a newone in the array for every error)" do
|
||||||
|
Sinatra::EventManager.events.size.should.equal 0
|
||||||
|
get_it '/blake'
|
||||||
|
Sinatra::EventManager.events.size.should.equal 0
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
|
@ -3,15 +3,26 @@ require File.dirname(__FILE__) + '/../helper'
|
||||||
describe "Event" do
|
describe "Event" do
|
||||||
|
|
||||||
it "should return 500 if exception thrown" do
|
it "should return 500 if exception thrown" do
|
||||||
set_logger stub_everything
|
Sinatra::Environment.set_loggers stub_everything
|
||||||
|
|
||||||
event = Sinatra::Event.new(:get, nil) do
|
event = Sinatra::Event.new(:get, nil) do
|
||||||
raise 'whaaaa!'
|
raise 'whaaaa!'
|
||||||
end
|
end
|
||||||
|
|
||||||
result = event.attend(stub_everything)
|
result = event.attend(stub_everything(:params => {}))
|
||||||
|
|
||||||
result.status.should.equal 500
|
result.status.should.equal 500
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "custom error if present" do
|
||||||
|
Sinatra::Environment.set_loggers stub_everything
|
||||||
|
|
||||||
|
event = Sinatra::Event.new(:get, 404) do
|
||||||
|
body 'custom 404'
|
||||||
|
end
|
||||||
|
|
||||||
|
Sinatra::EventManager.expects(:not_found).never
|
||||||
|
Sinatra::EventManager.determine_event(:get, '/sdf')
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue