From 01061464e0c903a6a175f9bf6e0903c1a3b321de Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Mon, 1 Oct 2007 21:20:45 -0700 Subject: [PATCH] Turn sessions on or off --- examples/hello/hello.rb | 16 +++++++++++++--- lib/sinatra/dsl.rb | 3 +++ lib/sinatra/server.rb | 2 +- lib/sinatra/sessions.rb | 21 +++++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 lib/sinatra/sessions.rb diff --git a/examples/hello/hello.rb b/examples/hello/hello.rb index 89a9a32..ffe5cee 100644 --- a/examples/hello/hello.rb +++ b/examples/hello/hello.rb @@ -1,9 +1,13 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib' require 'sinatra' -# get '/' do -# "Hello World!" -# end +production do + sessions :off +end + +get '/' do + "Hello World!" +end get '/erb.xml' do header 'Content-Type' => 'application/xml' @@ -18,3 +22,9 @@ get '/erb2' do erb 'Hello <%= params[:name].capitalize || "World" %> 2!' end +# Custom 404 + +get 404 do + 'Custom 404!!!!' +end + diff --git a/lib/sinatra/dsl.rb b/lib/sinatra/dsl.rb index cdc29c5..0601623 100644 --- a/lib/sinatra/dsl.rb +++ b/lib/sinatra/dsl.rb @@ -36,4 +36,7 @@ module Kernel end end + def sessions(on_off) + Sinatra::Session::Cookie.use = on_off + end end diff --git a/lib/sinatra/server.rb b/lib/sinatra/server.rb index c1b9ad7..bfdefc4 100644 --- a/lib/sinatra/server.rb +++ b/lib/sinatra/server.rb @@ -10,7 +10,7 @@ module Sinatra def start begin tail_thread = tail(Options.log_file) - Rack::Handler::Mongrel.run(Rack::Session::Cookie.new(Dispatcher.new), :Port => Options.port) do |server| + Rack::Handler::Mongrel.run(Sinatra::Session::Cookie.new(Dispatcher.new), :Port => Options.port) do |server| puts "== Sinatra has taken the stage on port #{server.port}!" trap("INT") do server.stop diff --git a/lib/sinatra/sessions.rb b/lib/sinatra/sessions.rb new file mode 100644 index 0000000..ce431b1 --- /dev/null +++ b/lib/sinatra/sessions.rb @@ -0,0 +1,21 @@ +module Sinatra + module Session + class Cookie + def self.use=(v) + @@use = v unless Sinatra::Server.running # keep is thread-safe! + end + + def initialize(app, options = {}) + @app = if (@@use ||= :on) == :off + app + else + Rack::Session::Cookie.new(app) + end + end + + def call(env) + @app.call(env) + end + end + end +end -- 2.11.4.GIT