rack-buckler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03d2cee41f25d4bc608b9b4fae71f1b89c9a1722
4
+ data.tar.gz: 5c7256842dcd329d31a23212706091ee5024fa50
5
+ SHA512:
6
+ metadata.gz: 8f35d519ac2e1ff43871a29aaaef7e1db65e4ca81cd08db834c5f7b30c39894d63e5df4e21f04b7fbad57ac9a20ea395585f90430785cf78172c0c27d4b4d1bb
7
+ data.tar.gz: b4ebad3845c88ee66722321bf0ccb95e71a477301b21a82862bd0003bf57b498620cf229745d36fbe39399a99150721a45183e38e9bb11f1273659a9762b2c9c
@@ -0,0 +1,3 @@
1
+ ---
2
+ BUNDLE_PATH: "vendor"
3
+ BUNDLE_DISABLE_SHARED_GEMS: "true"
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-buckler (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.0.0)
11
+ rspec (3.6.0)
12
+ rspec-core (~> 3.6.0)
13
+ rspec-expectations (~> 3.6.0)
14
+ rspec-mocks (~> 3.6.0)
15
+ rspec-core (3.6.0)
16
+ rspec-support (~> 3.6.0)
17
+ rspec-expectations (3.6.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.6.0)
20
+ rspec-mocks (3.6.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.6.0)
23
+ rspec-support (3.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.3)
30
+ rack-buckler!
31
+ rake
32
+ rspec
33
+
34
+ BUNDLED WITH
35
+ 1.15.2
@@ -0,0 +1,63 @@
1
+ require 'net/http'
2
+
3
+ module Rack
4
+ class Buckler
5
+ DEFAULT_PORT = 3030
6
+
7
+ def initialize(app, opts={})
8
+ @app = app
9
+ @opts = {
10
+ port: 3030,
11
+ x_forwarded_for: false,
12
+ read_timeout: 0.1,
13
+ open_timeout: 0.1
14
+ }
15
+ @opts.merge!(opts)
16
+ end
17
+
18
+ def reject_request
19
+ [429, {}, ['Too Many Requests']]
20
+ end
21
+
22
+ def call(env)
23
+ # Contact the buckler client using the port it is listening on for http requests
24
+ url = URI.parse("http://localhost:#{@opts[:port].to_s}/buckler")
25
+ req = Net::HTTP::Get.new(url.to_s)
26
+
27
+ headers = Hash[*env.select {|k,v| k.start_with? 'HTTP_'}
28
+ .collect {|k,v| [k.sub(/^HTTP_/, ''), v]}
29
+ .collect {|k,v| [k.split('_').collect(&:capitalize).join('-'), v]}
30
+ .sort
31
+ .flatten]
32
+
33
+ headers.each do |k,v|
34
+ req.add_field(k, v)
35
+ end
36
+
37
+ # Overwrite the X-Forwarded-For header
38
+ if @opts[:x_forwarded_for]
39
+ req.delete('X-Forwarded-For')
40
+ req.add_field('X-Forwarded-For', env["REMOTE_ADDR"])
41
+ end
42
+
43
+ res = nil
44
+ begin
45
+ # Make sure you set a timeout to protect your application latency (this is more of a safety measure).
46
+ res = Net::HTTP.start(url.host, url.port, read_timeout: @opts[:read_timeout], open_timeout: @opts[:open_timeout]) {|http|
47
+ http.request(req)
48
+ }
49
+ rescue
50
+ # Do nothing because we want to continue serving requests even though our call to buckler fails
51
+ end
52
+
53
+ # If the response from buckler is 429 then the request should be dropped.
54
+ # Here we return status code 429 to the user.
55
+ if res && res.code.to_i == 429
56
+ reject_request
57
+ else
58
+ # Otherwise proceed to process request normally.
59
+ @app.call(env)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Buckler
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/buckler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-buckler"
8
+ spec.version = Rack::Buckler::VERSION
9
+ spec.authors = ["Sega Okhiria"]
10
+ spec.email = ["sega.okhiria@7scnds.com"]
11
+ spec.description = %q{Middleware that will make Rack-based apps communicate with buckler client. Read more here: '{location to buckler and rack here}'.}
12
+ spec.summary = %q{Middleware for communicating buckler client in Rack apps}
13
+ spec.homepage = "https://bitbucket.org/segaokhiria/rack-buckler"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/).reject { |f| f == '.gitignore' or f =~ /^examples/ }
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ =begin
25
+ spec.add_development_dependency "minitest", ">= 5.3.0"
26
+ spec.add_development_dependency "mocha", ">= 0.14.0"
27
+ spec.add_development_dependency "rack-test", ">= 0"
28
+ =end
29
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-buckler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sega Okhiria
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: 'Middleware that will make Rack-based apps communicate with buckler client.
56
+ Read more here: ''{location to buckler and rack here}''.'
57
+ email:
58
+ - sega.okhiria@7scnds.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .bundle/config
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - lib/rack/buckler.rb
67
+ - lib/rack/buckler/version.rb
68
+ - rack-buckler.gemspec
69
+ homepage: https://bitbucket.org/segaokhiria/rack-buckler
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.4.8
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Middleware for communicating buckler client in Rack apps
93
+ test_files: []