From 703cf3b28741206d921c80d7e53a282100cccb79 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Wed, 28 Nov 2007 23:46:58 -0800 Subject: [PATCH] now running --- lib/sinatra.rb | 46 ++++++++++++++++++++++++++- lib/sinatra/test/spec.rb | 2 +- lib/sinatra/{test.rb => test/unit.rb} | 0 3 files changed, 46 insertions(+), 2 deletions(-) rename lib/sinatra/{test.rb => test/unit.rb} (100%) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 9c66eec4..2c209878 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -1,5 +1,15 @@ require 'rubygems' + +if ENV['SWIFT'] + require 'swiftcore/swiftiplied_mongrel' + puts "Using Swiftiplied Mongrel" +elsif ENV['EVENT'] + require 'swiftcore/evented_mongrel' + puts "Using Evented Mongrel" +end + require 'rack' +require 'ostruct' class Class def dslify_writter(*syms) @@ -26,6 +36,31 @@ module Sinatra def application=(app) @app = app end + + def port + application.options.port + end + + def env + application.options.env + end + + def run + + begin + puts "== Sinatra has taken the stage on port #{port} for #{env}" + require 'pp' + Rack::Handler::Mongrel.run(Sinatra.application, :Port => port) do |server| + trap(:INT) do + server.stop + puts "\n== Sinatra has ended his set (crowd applauds)" + end + end + rescue Errno::EADDRINUSE => e + puts "== Someone is already performing on port #{port}!" + end + + end class Event @@ -121,7 +156,6 @@ module Sinatra end include RenderingHelpers - attr_accessor :request, :response dslify_writter :status, :body @@ -187,6 +221,13 @@ module Sinatra events[env['REQUEST_METHOD'].downcase.to_sym].eject(&[:invoke, env]) end + def options + OpenStruct.new( + :port => 4567, + :env => :development + ) + end + def call(env) return [404, {}, 'Not Found'] unless result = lookup(env) context = EventContext.new( @@ -343,3 +384,6 @@ class NilClass end end +at_exit do + Sinatra.run +end diff --git a/lib/sinatra/test/spec.rb b/lib/sinatra/test/spec.rb index 126d1df2..5785e55a 100644 --- a/lib/sinatra/test/spec.rb +++ b/lib/sinatra/test/spec.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/../test' +require File.dirname(__FILE__) + '/unit' require 'test/spec' class Test::Unit::TestCase diff --git a/lib/sinatra/test.rb b/lib/sinatra/test/unit.rb similarity index 100% rename from lib/sinatra/test.rb rename to lib/sinatra/test/unit.rb