redrack-session 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +107 -4
- data/lib/redrack/session.rb +8 -0
- data/lib/redrack/session/middleware.rb +28 -8
- data/lib/redrack/session/version.rb +1 -1
- metadata +13 -13
data/README.md
CHANGED
@@ -10,13 +10,116 @@ server.
|
|
10
10
|
|
11
11
|
This gem may be used in much the same way as you might use the
|
12
12
|
`Rack::Session::Memcached` middleware provided by the rack gem, or any other
|
13
|
-
rack session middleware for that matter. Just
|
14
|
-
|
13
|
+
rack session middleware for that matter. Just create an instance of the
|
14
|
+
`Redrack::Session::Middleware` class and add it to your rack middleware stack
|
15
|
+
and then you can read and write objects to the hash provided in
|
15
16
|
`env["rack.session"]`.
|
16
17
|
|
17
|
-
|
18
|
+
#### Supported Options
|
19
|
+
|
20
|
+
When instantiating an instance of the `Redrack::Session::Middleware` class, you
|
21
|
+
can provide any/all of the same options supported by the generic
|
22
|
+
`Rack::Session::Cookie` or `Rack::Session::Pool` session stores. You may
|
23
|
+
additionally specify the following options:
|
24
|
+
|
25
|
+
- `:redis_host` -- specify IP address or hostname of host running the redis service (default: `'127.0.0.1'`)
|
26
|
+
- `:redis_port` -- specify port that the redis service is listening on (default: `6379`)
|
27
|
+
- `:redis_path` -- alternatively specify filename of socket that redis server is listening on (default: `nil`)
|
28
|
+
- `:redis_database` -- specify which database number to store session data in (default: `0`)
|
29
|
+
- `:redis_timeout` -- specify maximum number of seconds to wait before connection times out (default: `5`)
|
30
|
+
- `:redis_namespace` -- optionally specify a string to prefix to all session keys in case you're storing other datasets in the redis database (default: `nil`)
|
31
|
+
- `:redis_password` -- optionally specify a string to use to authenticate with the server (default: `nil`)
|
32
|
+
|
33
|
+
Some examples of the configuration options also supported in common with
|
34
|
+
`Rack::Session::Cookie` include:
|
35
|
+
|
36
|
+
- `:key` -- specify name of cookie stored on client's browser (default: ``rack.session'`)
|
37
|
+
- `:expire_after` -- specify how long, in seconds, to persist inactive sessions (default: `nil` meaning never expire)
|
38
|
+
- `:path` -- cookie's `:path` option (default: `'/'`)
|
39
|
+
- `:domain` -- cookie's `:domain` option
|
40
|
+
- `:secure` -- cookie's `:secure` option
|
41
|
+
- `:httponly` -- cookie's `:httponly` option
|
42
|
+
|
43
|
+
#### Example: rackup (config.ru) style rack application
|
44
|
+
|
45
|
+
If you've got a rack stack for a rack application configured using a `rackup`
|
46
|
+
style `config.ru`, you can use `redrack-session` in a manner similar to this
|
47
|
+
example rack application:
|
48
|
+
|
49
|
+
- `./Gemfile` -- I'm using bundler in this simple example
|
50
|
+
- `./config.ru` -- This configures the rack stack
|
51
|
+
- `./lib/rackapp.rb` -- Contains code for the example rack app
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
# Gemfile
|
55
|
+
source :rubygems
|
56
|
+
gem 'rack'
|
57
|
+
gem 'redrack-session'
|
58
|
+
```
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
# config.ru
|
62
|
+
require 'rubygems'
|
63
|
+
require 'bundler/setup'
|
64
|
+
$:.push File.expand_path("../lib", __FILE__)
|
65
|
+
require 'rackapp'
|
66
|
+
|
67
|
+
use Rack::ShowExceptions
|
68
|
+
use Rack::Lint
|
69
|
+
use Redrack::Session::Middleware
|
70
|
+
run Rackapp.new
|
71
|
+
```
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# lib/rackapp.rb
|
75
|
+
require 'redrack-session'
|
76
|
+
require 'rack/request'
|
77
|
+
require 'rack/response'
|
78
|
+
|
79
|
+
class Rackapp
|
80
|
+
def call(env)
|
81
|
+
request = Rack::Request.new(env)
|
82
|
+
session = env["rack.session"]
|
83
|
+
session["counter"] ||= 0
|
84
|
+
session["counter"] += 1
|
85
|
+
session["history"] ||= []
|
86
|
+
session["history"] << request.path
|
87
|
+
Rack::Response.new do |response|
|
88
|
+
response.write "<!DOCTYPE html>\n<html><head><title>Rackapp</title></head>\n<body><pre>\n"
|
89
|
+
response.write "Counter: #{session['counter']}\n"
|
90
|
+
response.write "History:\n" + session["history"].map { |h| " - #{h}" }.join("\n")
|
91
|
+
response.write "\n</pre></body></html>\n"
|
92
|
+
end.finish
|
93
|
+
end
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
Once the files are in place and the `bundler` gem is installed, you can then
|
98
|
+
complete setup and run the example rack app by doing the following:
|
99
|
+
|
100
|
+
```bash
|
101
|
+
user@host:~/projects/rackapp$ bundle install
|
102
|
+
user@host:~/projects/rackapp$ bundle exec rackup
|
103
|
+
```
|
104
|
+
|
105
|
+
This will then run a WEBrick server on you computer on port `9292`.
|
106
|
+
|
107
|
+
### TODO
|
108
|
+
|
109
|
+
The ultimate intent, starting with this gem, is develop several `redrack-*` gems
|
110
|
+
for storing various datasets in redis, including cache, i18n (translation), and
|
111
|
+
throttling information (black, white, and grey lists and abuse information).
|
112
|
+
|
113
|
+
Additionally I'd like to make equivalent `redrails-*` gems that essentially
|
114
|
+
provide the convenient packaging around their `redrack-*` namesakes for
|
115
|
+
integrating these gems into rails apps.
|
116
|
+
|
117
|
+
Finally, I'd like to create a master `redrack` and a `redrails` meta-gem that
|
118
|
+
depends on the full gamut and perhaps includes other conveniences (factory
|
119
|
+
methods, glue code) for the most common cases of usage.
|
120
|
+
|
121
|
+
#### Gems TODO:
|
18
122
|
|
19
|
-
- Flush out this README and improvide inline code documentation
|
20
123
|
- Create redrack-cache gem
|
21
124
|
- Create redrack-throttle gem
|
22
125
|
- Create redrack-localize gem
|
data/lib/redrack/session.rb
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module Redrack
|
3
|
+
|
4
|
+
#
|
5
|
+
# This is a namespacing module. Use Redrack::Session::Middleware for your redis session
|
6
|
+
# storage needs in your rack app.
|
7
|
+
#
|
8
|
+
# myapp = MyRackApp.new
|
9
|
+
# sessioned = Redrack::Session::Middleware.new(myapp, :redis_host => "redis.example.tld", ...)
|
10
|
+
#
|
3
11
|
module Session
|
4
12
|
autoload :VERSION, 'redrack/session/version'
|
5
13
|
autoload :Middleware, 'redrack/session/middleware'
|
@@ -5,19 +5,39 @@ require 'redis-namespace'
|
|
5
5
|
|
6
6
|
module Redrack
|
7
7
|
module Session
|
8
|
+
|
9
|
+
#
|
10
|
+
# Redrack::Session::Middleware provides redis-based session-storage with the session ID
|
11
|
+
# stored in a cookie on the client's browser.
|
12
|
+
#
|
13
|
+
# This can be used much like the Rack::Session::Cookie or Rack::Session::Pool session
|
14
|
+
# storage middleware classes provided in the rack gem. This class supports the same
|
15
|
+
# options as these classes with the following additional options:
|
16
|
+
#
|
17
|
+
# :redis_host -- hostname or IP of redis database server (default '127.0.0.1')
|
18
|
+
# :redis_port -- port to connect to redis database server (default 6379)
|
19
|
+
# :redis_path -- specify this if connecting to redis server via socket
|
20
|
+
# :redis_database -- default redis database to hold sessions (default 0)
|
21
|
+
# :redis_timeout -- default redis connection timeout in seconds (default 5)
|
22
|
+
# :redis_namespace -- optional namespace under which session keys are stored
|
23
|
+
# :redis_password -- optional authentication password for redis server
|
24
|
+
#
|
25
|
+
# See the gem's README.md for more information.
|
26
|
+
#
|
8
27
|
class Middleware < Rack::Session::Abstract::ID
|
9
28
|
|
10
29
|
attr_reader :redis # provide raw access to redis database (for testing)
|
11
30
|
|
12
|
-
# redis-specific default options (
|
31
|
+
# redis-specific default options (in addition to those specified in
|
32
|
+
# Rack::Session::Abstract::ID::DEFAULT_OPTIONS)
|
13
33
|
DEFAULT_OPTIONS = Rack::Session::Abstract::ID::DEFAULT_OPTIONS.merge(
|
14
|
-
:redis_password => nil,
|
15
|
-
:redis_namespace => nil,
|
16
|
-
:redis_path => nil,
|
17
|
-
:redis_host => "127.0.0.1",
|
18
|
-
:redis_port => 6379,
|
19
|
-
:redis_database => 0,
|
20
|
-
:redis_timeout => 5
|
34
|
+
:redis_password => nil,
|
35
|
+
:redis_namespace => nil,
|
36
|
+
:redis_path => nil,
|
37
|
+
:redis_host => "127.0.0.1",
|
38
|
+
:redis_port => 6379,
|
39
|
+
:redis_database => 0,
|
40
|
+
:redis_timeout => 5
|
21
41
|
)
|
22
42
|
|
23
43
|
def initialize(app, options = {})
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: redrack-session
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-11-02 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: &
|
16
|
+
requirement: &8350800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 1.0.21
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *8350800
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
27
|
-
requirement: &
|
27
|
+
requirement: &8350280 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 2.7.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *8350280
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rack-test
|
38
|
-
requirement: &
|
38
|
+
requirement: &8349740 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 0.6.1
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *8349740
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rack
|
49
|
-
requirement: &
|
49
|
+
requirement: &8349140 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.3.5
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *8349140
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: redis
|
60
|
-
requirement: &
|
60
|
+
requirement: &8347980 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 2.2.2
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *8347980
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: redis-namespace
|
71
|
-
requirement: &
|
71
|
+
requirement: &8347480 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: 1.1.0
|
77
77
|
type: :runtime
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *8347480
|
80
80
|
description: ! 'Redis session store for rack applications.
|
81
81
|
|
82
82
|
This was inspired by the Rack::Session::Memcached session store.
|