low 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
|
|
1
|
+
module Low
|
2
|
+
module Middleware
|
3
|
+
# `SubdomainMap` is Rack middleware that delegates to other Rack apps based
|
4
|
+
# upon subdomain. It takes two arguments upon initialization:
|
5
|
+
#
|
6
|
+
# * `base`, the app to be called if there is no subdomain.
|
7
|
+
# * `map`, a hash that maps subdomain strings to apps.
|
8
|
+
class SubdomainMap
|
9
|
+
def initialize(base, map = {})
|
10
|
+
@base = base
|
11
|
+
@map = map
|
12
|
+
end
|
13
|
+
|
14
|
+
def call(env)
|
15
|
+
# If the `SERVER_NAME` environment variable has a subdomain
|
16
|
+
if env['SERVER_NAME'] =~ /(.*?)\.(?:.*)\..*/
|
17
|
+
subdomain = $1
|
18
|
+
end
|
19
|
+
|
20
|
+
if subdomain
|
21
|
+
# and we can find a corresponding app,
|
22
|
+
if app = @map[subdomain]
|
23
|
+
# call it and return the result.
|
24
|
+
app.call(env)
|
25
|
+
|
26
|
+
# Otherwise, return 403 Forbidden.
|
27
|
+
else
|
28
|
+
[403, {"Content-Type" => "text/plain"}, []]
|
29
|
+
end
|
30
|
+
|
31
|
+
# If there is no subdomain,
|
32
|
+
else
|
33
|
+
# call the base app and return the result.
|
34
|
+
@base.call(env)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/low/version.rb
CHANGED
data/lib/low.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require File.dirname(__FILE__) + '
|
1
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
2
2
|
|
3
|
-
describe Low::SubdomainMap do
|
3
|
+
describe Low::Middleware::SubdomainMap do
|
4
4
|
def map
|
5
5
|
default_app = lambda do |env|
|
6
6
|
[200, {'Content-Type' => 'text/plain'}, ['Default App']]
|
@@ -10,7 +10,7 @@ describe Low::SubdomainMap do
|
|
10
10
|
[200, {'Content-Type' => 'text/plain'}, ['API App']]
|
11
11
|
end
|
12
12
|
|
13
|
-
Low::SubdomainMap.new default_app, 'subdomain' => api_app
|
13
|
+
Low::Middleware::SubdomainMap.new default_app, 'subdomain' => api_app
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should call the default app if no subdomain is specified' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: low
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rack
|
@@ -105,15 +105,15 @@ files:
|
|
105
105
|
- README.md
|
106
106
|
- Rakefile
|
107
107
|
- lib/low.rb
|
108
|
+
- lib/low/middleware/subdomain_map.rb
|
108
109
|
- lib/low/mongo.rb
|
109
110
|
- lib/low/mongo/heroku.rb
|
110
111
|
- lib/low/mongo/util.rb
|
111
|
-
- lib/low/subdomain_map.rb
|
112
112
|
- lib/low/version.rb
|
113
113
|
- low.gemspec
|
114
|
+
- spec/low/middleware/subdomain_map_spec.rb
|
114
115
|
- spec/low/mongo/util_spec.rb
|
115
116
|
- spec/low/mongo_spec.rb
|
116
|
-
- spec/low/subdomain_map_spec.rb
|
117
117
|
- spec/spec_helper.rb
|
118
118
|
homepage: ''
|
119
119
|
licenses: []
|
@@ -135,12 +135,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
version: '0'
|
136
136
|
requirements: []
|
137
137
|
rubyforge_project:
|
138
|
-
rubygems_version: 1.8.
|
138
|
+
rubygems_version: 1.8.23
|
139
139
|
signing_key:
|
140
140
|
specification_version: 3
|
141
141
|
summary: A low-level utility library.
|
142
142
|
test_files:
|
143
|
+
- spec/low/middleware/subdomain_map_spec.rb
|
143
144
|
- spec/low/mongo/util_spec.rb
|
144
145
|
- spec/low/mongo_spec.rb
|
145
|
-
- spec/low/subdomain_map_spec.rb
|
146
146
|
- spec/spec_helper.rb
|
data/lib/low/subdomain_map.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
module Low
|
2
|
-
# `SubdomainMap` is Rack middleware that delegates to other Rack apps based
|
3
|
-
# upon subdomain. It takes two arguments upon initialization:
|
4
|
-
#
|
5
|
-
# * `base`, the app to be called if there is no subdomain.
|
6
|
-
# * `map`, a hash that maps subdomain strings to apps.
|
7
|
-
class SubdomainMap
|
8
|
-
def initialize(base, map = {})
|
9
|
-
@base = base
|
10
|
-
@map = map
|
11
|
-
end
|
12
|
-
|
13
|
-
def call(env)
|
14
|
-
# If the `SERVER_NAME` environment variable has a subdomain
|
15
|
-
if env['SERVER_NAME'] =~ /(.*?)\.(?:.*)\..*/
|
16
|
-
subdomain = $1
|
17
|
-
end
|
18
|
-
|
19
|
-
if subdomain
|
20
|
-
# and we can find a corresponding app,
|
21
|
-
if app = @map[subdomain]
|
22
|
-
# call it and return the result.
|
23
|
-
app.call(env)
|
24
|
-
|
25
|
-
# Otherwise, return 403 Forbidden.
|
26
|
-
else
|
27
|
-
[403, {"Content-Type" => "text/plain"}, []]
|
28
|
-
end
|
29
|
-
|
30
|
-
# If there is no subdomain,
|
31
|
-
else
|
32
|
-
# call the base app and return the result.
|
33
|
-
@base.call(env)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|