mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Turn sessions on or off
This commit is contained in:
parent
c333cdb8bf
commit
01061464e0
4 changed files with 38 additions and 4 deletions
|
@ -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
|
||||
|
||||
|
|
|
@ -36,4 +36,7 @@ module Kernel
|
|||
end
|
||||
end
|
||||
|
||||
def sessions(on_off)
|
||||
Sinatra::Session::Cookie.use = on_off
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
21
lib/sinatra/sessions.rb
Normal file
21
lib/sinatra/sessions.rb
Normal file
|
@ -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
|
Loading…
Add table
Reference in a new issue