Permalink
Browse files

Turn sessions on or off

  • Loading branch information...
1 parent c333cdb commit 01061464e0c903a6a175f9bf6e0903c1a3b321de Blake Mizerany committed Oct 2, 2007
Showing with 38 additions and 4 deletions.
  1. +13 −3 examples/hello/hello.rb
  2. +3 −0 lib/sinatra/dsl.rb
  3. +1 −1 lib/sinatra/server.rb
  4. +21 −0 lib/sinatra/sessions.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 @@
erb 'Hello <%= params[:name].capitalize || "World" %> 2!'
end
+# Custom 404
+
+get 404 do
+ 'Custom 404!!!!'
+end
+
View
@@ -36,4 +36,7 @@ def layout(name = :layout, options = {})
end
end
+ def sessions(on_off)
+ Sinatra::Session::Cookie.use = on_off
+ end
end
@@ -10,7 +10,7 @@ class Server
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
@@ -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

0 comments on commit 0106146

Please sign in to comment.