Skip to content
This repository
Browse code

Turn sessions on or off

  • Loading branch information...
commit 01061464e0c903a6a175f9bf6e0903c1a3b321de 1 parent c333cdb
authored October 01, 2007
16  examples/hello/hello.rb
... ...
@@ -1,9 +1,13 @@
1 1
 $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib'
2 2
 require 'sinatra'
3 3
 
4  
-# get '/' do
5  
-#   "Hello World!"
6  
-# end
  4
+production do
  5
+  sessions :off
  6
+end
  7
+
  8
+get '/' do
  9
+  "Hello World!"
  10
+end
7 11
 
8 12
 get '/erb.xml' do
9 13
   header 'Content-Type' => 'application/xml'
@@ -18,3 +22,9 @@
18 22
   erb 'Hello <%= params[:name].capitalize || "World" %> 2!'
19 23
 end
20 24
 
  25
+# Custom 404
  26
+
  27
+get 404 do
  28
+  'Custom 404!!!!'
  29
+end
  30
+
3  lib/sinatra/dsl.rb
@@ -36,4 +36,7 @@ def layout(name = :layout, options = {})
36 36
     end
37 37
   end
38 38
   
  39
+  def sessions(on_off)
  40
+    Sinatra::Session::Cookie.use = on_off
  41
+  end
39 42
 end
2  lib/sinatra/server.rb
@@ -10,7 +10,7 @@ class Server
10 10
     def start
11 11
       begin
12 12
         tail_thread = tail(Options.log_file)
13  
-        Rack::Handler::Mongrel.run(Rack::Session::Cookie.new(Dispatcher.new), :Port => Options.port) do |server|
  13
+        Rack::Handler::Mongrel.run(Sinatra::Session::Cookie.new(Dispatcher.new), :Port => Options.port) do |server|
14 14
           puts "== Sinatra has taken the stage on port #{server.port}!"
15 15
           trap("INT") do
16 16
             server.stop
21  lib/sinatra/sessions.rb
... ...
@@ -0,0 +1,21 @@
  1
+module Sinatra
  2
+  module Session
  3
+    class Cookie
  4
+      def self.use=(v)
  5
+        @@use = v unless Sinatra::Server.running  # keep is thread-safe!
  6
+      end
  7
+      
  8
+      def initialize(app, options = {})
  9
+        @app = if (@@use ||= :on) == :off
  10
+          app
  11
+        else
  12
+          Rack::Session::Cookie.new(app)
  13
+        end
  14
+      end
  15
+      
  16
+      def call(env)
  17
+        @app.call(env)
  18
+      end
  19
+    end
  20
+  end
  21
+end

0 notes on commit 0106146

Please sign in to comment.
Something went wrong with that request. Please try again.