Browse files

Initial commit to yup.

  • Loading branch information...
0 parents commit c3a5b1c894856de5044be29bdc74cdfa4a86c10a @neglectedvalue committed Aug 7, 2011
Showing with 397 additions and 0 deletions.
  1. +5 −0 .document
  2. +42 −0 .gitignore
  3. +13 −0 Gemfile
  4. +33 −0 Gemfile.lock
  5. +20 −0 LICENSE.txt
  6. +44 −0 README.rdoc
  7. +46 −0 Rakefile
  8. +55 −0 bin/yupd
  9. +28 −0 lib/yup.rb
  10. +39 −0 lib/yup/request_forwarder.rb
  11. +47 −0 lib/yup/request_handler.rb
  12. +18 −0 test/helper.rb
  13. +7 −0 test/test_yup.rb
5 .document
@@ -0,0 +1,5 @@
+lib/**/*.rb
+bin/*
+-
+features/**/*.feature
+LICENSE.txt
42 .gitignore
@@ -0,0 +1,42 @@
+# rcov generated
+coverage
+
+# rdoc generated
+rdoc
+
+# yard generated
+doc
+.yardoc
+
+# bundler
+.bundle
+
+# jeweler generated
+pkg
+
+# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
+#
+# * Create a file at ~/.gitignore
+# * Include files you want ignored
+# * Run: git config --global core.excludesfile ~/.gitignore
+#
+# After doing this, these files will be ignored in all your git projects,
+# saving you from having to 'pollute' every project you touch with them
+#
+# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
+#
+# For MacOS:
+#
+#.DS_Store
+#
+# For TextMate
+#*.tmproj
+#tmtags
+#
+# For emacs:
+#*~
+#\#*
+#.\#*
+#
+# For vim:
+#*.swp
13 Gemfile
@@ -0,0 +1,13 @@
+source "http://rubygems.org"
+
+gem "eventmachine"
+gem "em-http-request"
+gem "http_parser.rb"
+
+group :development do
+ gem "shoulda", ">= 0"
+ gem "yard", "~> 0.6.0"
+ gem "bundler", "~> 1.0.0"
+ gem "jeweler", "~> 1.5.2"
+ gem "rcov", ">= 0"
+end
33 Gemfile.lock
@@ -0,0 +1,33 @@
+GEM
+ remote: http://rubygems.org/
+ specs:
+ addressable (2.2.6)
+ em-http-request (0.3.0)
+ addressable (>= 2.0.0)
+ escape_utils
+ eventmachine (>= 0.12.9)
+ escape_utils (0.2.3)
+ eventmachine (0.12.10)
+ git (1.2.5)
+ http_parser.rb (0.5.1)
+ jeweler (1.5.2)
+ bundler (~> 1.0.0)
+ git (>= 1.2.5)
+ rake
+ rake (0.9.2)
+ rcov (0.9.10)
+ shoulda (2.11.3)
+ yard (0.6.8)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (~> 1.0.0)
+ em-http-request
+ eventmachine
+ http_parser.rb
+ jeweler (~> 1.5.2)
+ rcov
+ shoulda
+ yard (~> 0.6.0)
20 LICENSE.txt
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Denis Sukhonin
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 README.rdoc
@@ -0,0 +1,44 @@
+= yup daemon
+
+This is the small daemon for transparent asynchronous delegating HTTP requests when response is known or unimportant.
+
+When a http request is arrived the yup daemon (yupd) answers to the client configured answer (by default 200 OK). Then the yupd forwards the http request to the specified host and retries if a timeout error was happend.
+
+== On of use cases
+
+For example we can have a rails app which send exceptions to an errbit by the gem hoptoad_notifier. We know the errbit can be not available by network issues or some else reasons, but we do not want lose exceptions. To resolve this problem we can start yupd on the same host with the rails app:
+ yupd --listen localhost:8081 --status-code 201 errbit.host.somewhere
+
+Reconfiguration of hoptoad_notifier is very ease:
+ HoptoadNotifier.configure do |config|
+ config.host = "localhost" # yupd host
+ config.port = 8081 # yupd port
+ config.api_key = "api_key_for_your_app"
+ end
+
+Now problem of availability of errbit is assigned to the yupd.
+
+== Roadmap to 0.1
+
+* Daemonize
+* Logger
+* Preforking
+* Persistent HTTP requests queue
+* A configurable map of different delegating rules
+* Tests...
+
+== Contributing to yup
+
+* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
+* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
+* Fork the project
+* Start a feature/bugfix branch
+* Commit and push until you are happy with your contribution
+* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
+* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
+
+== Copyright
+
+Copyright (c) 2011 Denis Sukhonin. See LICENSE.txt for
+further details.
+
46 Rakefile
@@ -0,0 +1,46 @@
+require 'rubygems'
+require 'bundler'
+begin
+ Bundler.setup(:default, :development)
+rescue Bundler::BundlerError => e
+ $stderr.puts e.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit e.status_code
+end
+require 'rake'
+
+require 'jeweler'
+Jeweler::Tasks.new do |gem|
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
+ gem.name = "yup"
+ gem.homepage = "http://github.com/neglectedvalue/yup"
+ gem.license = "MIT"
+ gem.summary = "Asynchronous HTTP delegate"
+ gem.description = "Just answers 200 (or specified) to a client and asynchronously forwards HTTP request to a configured host"
+ gem.email = "d.sukhonin@gmail.com"
+ gem.authors = ["Denis Sukhonin"]
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
+end
+Jeweler::RubygemsDotOrgTasks.new
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+end
+
+require 'rcov/rcovtask'
+Rcov::RcovTask.new do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/test_*.rb'
+ test.verbose = true
+end
+
+task :default => :test
+
+require 'yard'
+YARD::Rake::YardocTask.new
55 bin/yupd
@@ -0,0 +1,55 @@
+#!/usr/bin/env ruby
+# -*- ruby -*-
+
+require 'getoptlong'
+
+$:.unshift(File.expand_path('../lib', File.dirname(__FILE__)))
+require 'yup'
+
+def usage
+ puts <<-EOF
+Usage: #{$0} [OPTION] ... FORWARD_TO_HOST
+
+Options:
+ -h, --help Show help
+ --listen <host:port>, -l Listen on an address (default localhost:8080)
+ --status-code <code> Send status code to a client on request (default 200)
+ --resend-delay <seconds> Resend failed requests in seconds (default 5.0)
+
+Examples:
+ yupd --listen 0.0.0.0:8081 --status-code 201 errbit.host.somewhere
+
+EOF
+end
+
+opts = GetoptLong.new(
+ ['--help', '-h', GetoptLong::NO_ARGUMENT],
+ ['--listen', '-l', GetoptLong::REQUIRED_ARGUMENT],
+ ['--status-code', GetoptLong::REQUIRED_ARGUMENT],
+ ['--resend-delay', '-d', GetoptLong::REQUIRED_ARGUMENT],
+ )
+config = {}
+opts.each do |opt, arg|
+ case opt
+ when '--help', '-h'
+ usage
+ exit 0
+ when '--listen', '-l'
+ config[:listen_host], config[:listen_port] = arg.split(':')
+ when '--resend-delay', '-d'
+ config[:resend_delay] = arg.to_f
+ when '--status-code'
+ config[:status_code] = arg.to_i
+ end
+end
+
+if ARGV.length != 1
+ puts "Missing host argument (try --help)"
+ usage
+ exit 1
+end
+
+config[:forward_to] = ARGV.shift
+
+Yup.resend_delay = config[:resend_delay] if config.has_key?(:resend_delay)
+Yup.run(config)
28 lib/yup.rb
@@ -0,0 +1,28 @@
+require 'rubygems'
+require 'eventmachine'
+
+require 'yup/request_forwarder'
+require 'yup/request_handler'
+
+module Yup
+ @@resend_delay = 5.0
+ def self.resend_delay
+ @@resend_delay
+ end
+ def self.resend_delay=(seconds)
+ @@resend_delay = seconds
+ end
+
+ def self.run(config)
+ host = config[:listen_host] || 'localhost'
+ port = config[:listen_port] || 8080
+ status_code = config[:status_code] || 200
+ forward_to = config[:forward_to]
+
+ EventMachine::run do
+ EventMachine::start_server(host, port, RequestHandler,
+ forward_to, status_code)
+ puts "listening on #{host}:#{port}"
+ end
+ end
+end
39 lib/yup/request_forwarder.rb
@@ -0,0 +1,39 @@
+require 'em-http-request'
+
+module Yup
+ class RequestForwarder
+ def initialize(parser, body, forward_to)
+ @parser = parser
+ @body = body
+ @forward_to = forward_to
+ end
+
+ def run
+ http_method = @parser.http_method.downcase.to_sym
+ http_url = "http://#{@forward_to}#{@parser.request_url}"
+ http = EventMachine::HttpRequest.
+ new(http_url).
+ send(http_method,
+ :head => @parser.headers.merge('Host' => @forward_to),
+ :body => @body)
+
+ http.callback do
+ if http.response_header.status / 100 == 2
+ puts '--- SUCCESS'
+ else
+ puts '--- FAIL'
+ # puts http.response_header.inspect
+ # puts http.response
+ p http
+ end
+ end
+
+ http.errback do
+ puts '--- ERROR'
+ p http
+
+ EventMachine.add_timer(Yup.resend_delay) { self.run }
+ end
+ end
+ end
+end
47 lib/yup/request_handler.rb
@@ -0,0 +1,47 @@
+require 'webrick'
+require 'http/parser'
+
+module Yup
+ class RequestHandler < EM::Connection
+ attr_reader :queue
+
+ def initialize(forward_to, status_code)
+ @forward_to = forward_to
+ @status_code = status_code
+ @chunks = []
+ end
+
+ def post_init
+ @parser = Http::Parser.new(self)
+ end
+
+ def receive_data(data)
+ @parser << data
+ end
+
+ def on_message_begin
+ @body = ''
+ end
+
+ def on_body(chunk)
+ @body << chunk
+ end
+
+ def on_message_complete
+ puts '-- got request'
+ p @parser.http_version
+ p @parser.http_method # for requests
+ p @parser.request_url
+ p @parser.headers
+
+ resp = WEBrick::HTTPResponse.new(:HTTPVersion => '1.1')
+ resp.status = @status_code
+ resp['Server'] = 'yupd'
+ send_data resp.to_s
+
+ EventMachine.next_tick do
+ RequestForwarder.new(@parser, @body, @forward_to).run
+ end
+ end
+ end
+end
18 test/helper.rb
@@ -0,0 +1,18 @@
+require 'rubygems'
+require 'bundler'
+begin
+ Bundler.setup(:default, :development)
+rescue Bundler::BundlerError => e
+ $stderr.puts e.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit e.status_code
+end
+require 'test/unit'
+require 'shoulda'
+
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'yup'
+
+class Test::Unit::TestCase
+end
7 test/test_yup.rb
@@ -0,0 +1,7 @@
+require 'helper'
+
+class TestYup < Test::Unit::TestCase
+ should "probably rename this file and start testing for real" do
+ flunk "hey buddy, you should probably rename this file and start testing for real"
+ end
+end

0 comments on commit c3a5b1c

Please sign in to comment.