pow_proxy 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.simplecov +1 -0
- data/.yardopts +3 -0
- data/Gemfile +4 -0
- data/LICENSE.md +20 -0
- data/Rakefile +18 -0
- data/Readme.md +32 -0
- data/lib/pow_proxy.rb +39 -0
- data/lib/pow_proxy/version.rb +3 -0
- data/pow_proxy.gemspec +26 -0
- data/spec/pow_proxy_spec.rb +100 -0
- data/spec/spec_helper.rb +6 -0
- metadata +140 -0
data/.gemtest
ADDED
File without changes
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.simplecov
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
SimpleCov.start
|
data/.yardopts
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Steve Agalloco, Assaf Arkin
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
task :default => :spec
|
10
|
+
task :test => :spec
|
11
|
+
|
12
|
+
require 'yard'
|
13
|
+
namespace :doc do
|
14
|
+
YARD::Rake::YardocTask.new do |task|
|
15
|
+
task.files = ['LICENSE.md', 'lib/**/*.rb']
|
16
|
+
task.options = ['--markup', 'markdown']
|
17
|
+
end
|
18
|
+
end
|
data/Readme.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PowProxy
|
2
|
+
========
|
3
|
+
|
4
|
+
PowProxy is a simple rack-based proxy that allows you to run your node apps through [Pow](http://pow.cx).
|
5
|
+
|
6
|
+
PowProxy is based on a blog post by [Assaf Arkin](/assaf). It's super easy to use. You really should just read [Assaf's blog post](http://labnotes.org/2011/08/09/using-pow-with-your-node-js-project/) to get the full explanation.
|
7
|
+
|
8
|
+
Usage
|
9
|
+
-----
|
10
|
+
|
11
|
+
Create a `config.ru` file in your project's root with the following:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'pow_proxy'
|
15
|
+
run PowProxy.new
|
16
|
+
```
|
17
|
+
|
18
|
+
By default, it assumes the host to be `localhost` and the port to be `3000` however you can configure that to be anything you'd like:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
require 'pow_proxy'
|
22
|
+
run PowProxy.new(:host => '127.0.0.1', :port => 8080)
|
23
|
+
```
|
24
|
+
|
25
|
+
You can also set the host and port by exporting the `POW_PROXY_HOST` and `POW_PROXY_PORT` environment variables in your `.powenv`.
|
26
|
+
|
27
|
+
Make sure your node app is running, symlink your app so that Pow knows about it and you'll be all set.
|
28
|
+
|
29
|
+
Copyright
|
30
|
+
---------
|
31
|
+
|
32
|
+
Copyright (c) 2011 Steve Agalloco, Assaf Arkin. See [LICENSE](LICENSE.md) for details.
|
data/lib/pow_proxy.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "net/http"
|
2
|
+
require "rack"
|
3
|
+
|
4
|
+
class PowProxy
|
5
|
+
DEFAULT_HOST = "127.0.0.1"
|
6
|
+
DEFAULT_PORT = 3000
|
7
|
+
|
8
|
+
attr_reader :host, :port
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
@host = options.delete(:host) || ENV['POW_PROXY_HOST'] || DEFAULT_HOST
|
12
|
+
@port = options.delete(:port) || ENV['POW_PROXY_PORT'] || DEFAULT_PORT
|
13
|
+
end
|
14
|
+
|
15
|
+
def call(env)
|
16
|
+
begin
|
17
|
+
request = Rack::Request.new(env)
|
18
|
+
headers = {}
|
19
|
+
env.each do |key, value|
|
20
|
+
if key =~ /^http_(.*)/i
|
21
|
+
headers[$1] = value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
headers["Content-Type"] = request.content_type if request.content_type
|
26
|
+
headers["Content-Length"] = request.content_length if request.content_length
|
27
|
+
|
28
|
+
http = Net::HTTP.new(@host, @port)
|
29
|
+
http.start do |http|
|
30
|
+
response = http.send_request(request.request_method, request.fullpath, request.body.read, headers)
|
31
|
+
headers = response.to_hash
|
32
|
+
headers.delete "transfer-encoding"
|
33
|
+
[response.code, headers, [response.body]]
|
34
|
+
end
|
35
|
+
rescue Errno::ECONNREFUSED
|
36
|
+
[500, {}, ["Could not establish a connection to #{@host}:#{@port}, make sure your node process is running."]]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/pow_proxy.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/pow_proxy/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.name = "pow_proxy"
|
6
|
+
gem.version = PowProxy::VERSION
|
7
|
+
|
8
|
+
gem.authors = ["Steve Agalloco"]
|
9
|
+
gem.email = ["steve.agalloco@gmail.com"]
|
10
|
+
gem.description = 'A simple rack-based proxy that allows you to run your node apps through Pow.'
|
11
|
+
gem.summary = gem.description
|
12
|
+
gem.homepage = "https://github.com/spagalloco/pow_proxy"
|
13
|
+
|
14
|
+
gem.add_dependency 'rack'
|
15
|
+
gem.add_development_dependency 'rake', '~> 0.9'
|
16
|
+
gem.add_development_dependency 'rdiscount', '~> 1.6'
|
17
|
+
gem.add_development_dependency 'rspec', '~> 2.7'
|
18
|
+
gem.add_development_dependency 'simplecov', '~> 0.5'
|
19
|
+
gem.add_development_dependency 'yard', '~> 0.7'
|
20
|
+
gem.add_development_dependency 'webmock', '~> 1.7'
|
21
|
+
|
22
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
gem.files = `git ls-files`.split("\n")
|
24
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
gem.require_paths = ["lib"]
|
26
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe PowProxy do
|
4
|
+
|
5
|
+
describe '.new' do
|
6
|
+
context 'defaults' do
|
7
|
+
before do
|
8
|
+
@proxy = PowProxy.new
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'defaults the host to "127.0.0.1"' do
|
12
|
+
@proxy.host.should eq('127.0.0.1')
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'defaults the port to 3000' do
|
16
|
+
@proxy.port.should eq(3000)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'options' do
|
21
|
+
before do
|
22
|
+
@proxy = PowProxy.new(:host => 'localhost2', :port => 4242)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'sets the host to "localhost2"' do
|
26
|
+
@proxy.host.should eq('localhost2')
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'sets the port to 4242' do
|
30
|
+
@proxy.port.should eq(4242)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'ENV' do
|
35
|
+
before do
|
36
|
+
ENV['POW_PROXY_HOST'] = 'monk.local'
|
37
|
+
ENV['POW_PROXY_PORT'] = '8080'
|
38
|
+
@proxy = PowProxy.new
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'defaults the host to "monk.local"' do
|
42
|
+
@proxy.host.should eq('monk.local')
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'defaults the port to 8080' do
|
46
|
+
@proxy.port.should eq('8080')
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'integration' do
|
52
|
+
before do
|
53
|
+
@proxy = PowProxy.new(:host => 'app.local', :port => 2121)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'returns the requested content' do
|
57
|
+
stub_request(:get, "http://app.local:2121/").
|
58
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
59
|
+
to_return(:status => 200, :body => "abcdefg", :headers => {})
|
60
|
+
|
61
|
+
env = { "REQUEST_METHOD"=>"GET", "PATH_INFO"=>"/", "rack.input" => StringIO.new }
|
62
|
+
response = @proxy.call(env)
|
63
|
+
body = response.last.shift
|
64
|
+
body.should eq('abcdefg')
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'removes the transfer-encoding header from the response' do
|
68
|
+
stub_request(:get, "http://app.local:2121/").
|
69
|
+
with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}).
|
70
|
+
to_return(:status => 200, :body => "abcdefg", :headers => { 'Transfer-Encoding' => 'chunked'})
|
71
|
+
|
72
|
+
env = { "REQUEST_METHOD"=>"GET", "PATH_INFO"=>"/", "rack.input" => StringIO.new }
|
73
|
+
response = @proxy.call(env)
|
74
|
+
headers = response[1]
|
75
|
+
headers.should_not have_key('transfer-encoding')
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'passes headers to the node server' do
|
79
|
+
stub_request(:get, "http://app.local:2121/").
|
80
|
+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip,deflate,sdch', 'User-Agent'=>'Ruby'}).
|
81
|
+
to_return(:status => 200, :body => "abcdefg", :headers => {})
|
82
|
+
|
83
|
+
env = { "REQUEST_METHOD"=>"GET", "PATH_INFO"=>"/", "rack.input" => StringIO.new, 'HTTP_ACCEPT_ENCODING' => "gzip,deflate,sdch" }
|
84
|
+
response = @proxy.call(env)
|
85
|
+
body = response.last.shift
|
86
|
+
body.should eq('abcdefg')
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'reports an error when it cannot request to the node server' do
|
90
|
+
Rack::Request.stub(:new).and_raise(Errno::ECONNREFUSED)
|
91
|
+
|
92
|
+
env = { "REQUEST_METHOD"=>"GET", "PATH_INFO"=>"/", "rack.input" => StringIO.new }
|
93
|
+
response = @proxy.call(env)
|
94
|
+
response.first.should eq(500)
|
95
|
+
body = response.last.shift
|
96
|
+
body.should match(/Could not establish a connection/)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pow_proxy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steve Agalloco
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-11 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: &70320649561140 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70320649561140
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70320649560440 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0.9'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70320649560440
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rdiscount
|
38
|
+
requirement: &70320649559560 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '1.6'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70320649559560
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &70320649559100 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.7'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70320649559100
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: simplecov
|
60
|
+
requirement: &70320649549540 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0.5'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70320649549540
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: yard
|
71
|
+
requirement: &70320649549080 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0.7'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70320649549080
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: webmock
|
82
|
+
requirement: &70320649548620 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ~>
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '1.7'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70320649548620
|
91
|
+
description: A simple rack-based proxy that allows you to run your node apps through
|
92
|
+
Pow.
|
93
|
+
email:
|
94
|
+
- steve.agalloco@gmail.com
|
95
|
+
executables: []
|
96
|
+
extensions: []
|
97
|
+
extra_rdoc_files: []
|
98
|
+
files:
|
99
|
+
- .gemtest
|
100
|
+
- .gitignore
|
101
|
+
- .rspec
|
102
|
+
- .simplecov
|
103
|
+
- .yardopts
|
104
|
+
- Gemfile
|
105
|
+
- LICENSE.md
|
106
|
+
- Rakefile
|
107
|
+
- Readme.md
|
108
|
+
- lib/pow_proxy.rb
|
109
|
+
- lib/pow_proxy/version.rb
|
110
|
+
- pow_proxy.gemspec
|
111
|
+
- spec/pow_proxy_spec.rb
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
homepage: https://github.com/spagalloco/pow_proxy
|
114
|
+
licenses: []
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ! '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
none: false
|
127
|
+
requirements:
|
128
|
+
- - ! '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 1.8.10
|
134
|
+
signing_key:
|
135
|
+
specification_version: 3
|
136
|
+
summary: A simple rack-based proxy that allows you to run your node apps through Pow.
|
137
|
+
test_files:
|
138
|
+
- spec/pow_proxy_spec.rb
|
139
|
+
- spec/spec_helper.rb
|
140
|
+
has_rdoc:
|