From 1e2e7e497c83d8bcda12187a7e46b8aac1297c56 Mon Sep 17 00:00:00 2001 From: Paulie Pena Date: Tue, 25 Jun 2013 09:34:19 -0400 Subject: [PATCH 1/2] don't use a static host_parts.length value of 2 --- README.md | 2 ++ generators/files/config.rb | 6 +++++- lib/divergence/config.rb | 9 ++++++++- lib/divergence/request_parser.rb | 4 ++-- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e6a7799..01b5957 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ Divergence::Application.configure do |config| config.app_path = "/path/to/app_root" config.cache_path = "/path/to/cache_root" + config.incoming_base_uri = 'example.com' + config.forward_host = 'localhost' config.forward_port = 80 diff --git a/generators/files/config.rb b/generators/files/config.rb index 7db592f..0d33785 100644 --- a/generators/files/config.rb +++ b/generators/files/config.rb @@ -11,6 +11,10 @@ # have it's own Passenger instance, so don't get too carried away. # config.cache_num = 5 + # Incoming base domain for requests, which will have a + # branch name prepended to it + config.incoming_base_uri = 'example.com' + # Where should we proxy this request to? Normally you can leave # the host as 'localhost', but if you are using virtual hosts in # your web server setup, you may need to be more specific. You @@ -18,4 +22,4 @@ # so update your web application to run on a different port. config.forward_host = 'localhost' config.forward_port = 80 -end \ No newline at end of file +end diff --git a/lib/divergence/config.rb b/lib/divergence/config.rb index e41ed42..3225978 100644 --- a/lib/divergence/config.rb +++ b/lib/divergence/config.rb @@ -5,6 +5,7 @@ class Configuration attr_accessor :app_path, :git_path, :cache_path attr_accessor :cache_num attr_accessor :forward_host, :forward_port + attr_reader :incoming_base_uri_length def initialize @git_path = nil @@ -13,6 +14,7 @@ def initialize @cache_num = 5 + @incoming_base_uri_length = 2 @forward_host = 'localhost' @forward_port = 80 @@ -20,6 +22,11 @@ def initialize @helpers = Divergence::Helpers.new(self) end + def incoming_base_uri=(base_uri) + @incoming_base_uri = base_uri + @incoming_base_uri_length = @base_uri.split(".").length + end + def ok? [:git_path, :app_path, :cache_path].each do |path| if instance_variable_get("@#{path}").nil? @@ -73,4 +80,4 @@ def each(&block) end end end -end \ No newline at end of file +end diff --git a/lib/divergence/request_parser.rb b/lib/divergence/request_parser.rb index d92e227..6bd98fa 100644 --- a/lib/divergence/request_parser.rb +++ b/lib/divergence/request_parser.rb @@ -19,7 +19,7 @@ def host_parts end def has_subdomain? - host_parts.length > 2 + host_parts.length > @config.incoming_base_uri_length end def subdomain @@ -48,4 +48,4 @@ def method_missing(meth, *args, &block) raw.send(meth, *args) end end -end \ No newline at end of file +end From 023e7b4929668a1c7c11a2ecaec539b8583383c2 Mon Sep 17 00:00:00 2001 From: Paulie Pena Date: Tue, 25 Jun 2013 10:08:18 -0400 Subject: [PATCH 2/2] add support for site subdomains in same git rep --- generators/files/config.rb | 9 ++++++++ lib/divergence/config.rb | 8 +++++--- lib/divergence/request_parser.rb | 35 ++++++++++++++------------------ lib/divergence/respond.rb | 16 +++++++++++---- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/generators/files/config.rb b/generators/files/config.rb index 0d33785..27e40b0 100644 --- a/generators/files/config.rb +++ b/generators/files/config.rb @@ -22,4 +22,13 @@ # so update your web application to run on a different port. config.forward_host = 'localhost' config.forward_port = 80 + + # If your site code is used for more than one virtual host, with + # one being a subdomain of the other (e.g. example.com and + # api.example.com) and both under the same git repository, you can supply + # the additional domains in the config.site_subdomains array. + # Note: if you use a config.forward_host value of 'localhost', then + # Divergence will make the request to api.localhost, so you probably + # want to use a different forward_host. + # config.site_subdomains = ['api'] end diff --git a/lib/divergence/config.rb b/lib/divergence/config.rb index 3225978..3b918b3 100644 --- a/lib/divergence/config.rb +++ b/lib/divergence/config.rb @@ -5,6 +5,7 @@ class Configuration attr_accessor :app_path, :git_path, :cache_path attr_accessor :cache_num attr_accessor :forward_host, :forward_port + attr_accessor :site_subdomains attr_reader :incoming_base_uri_length def initialize @@ -17,14 +18,15 @@ def initialize @incoming_base_uri_length = 2 @forward_host = 'localhost' @forward_port = 80 + @site_subdomains = [] @callback_store = {} @helpers = Divergence::Helpers.new(self) end - def incoming_base_uri=(base_uri) - @incoming_base_uri = base_uri - @incoming_base_uri_length = @base_uri.split(".").length + def incoming_base_uri=(incoming_base_uri) + @incoming_base_uri = incoming_base_uri + @incoming_base_uri_length = @incoming_base_uri.split(".").length end def ok? diff --git a/lib/divergence/request_parser.rb b/lib/divergence/request_parser.rb index 6bd98fa..4842a6e 100644 --- a/lib/divergence/request_parser.rb +++ b/lib/divergence/request_parser.rb @@ -1,7 +1,14 @@ module Divergence class RequestParser - def initialize(env) + + attr_accessor :called_site_subdomain + + def initialize(env, config) @req = Rack::Request.new(env) + @config = config + + @host_parts = @req.host.split(".") + @called_site_subdomain = nil end def raw @@ -9,36 +16,24 @@ def raw end def is_webhook? - subdomain == "divergence" and + @host_parts[0] == "divergence" and @req.env['PATH_INFO'] == "/update" and @req.post? end - def host_parts - @req.host.split(".") - end - def has_subdomain? - host_parts.length > @config.incoming_base_uri_length - end - - def subdomain - if has_subdomain? - host_parts.shift - else - nil - end + @host_parts.length > @config.incoming_base_uri_length end def branch if has_subdomain? - branch = subdomain + branch = @host_parts[0] - if branch['-'] - @git.discover(branch) - else - branch + if @config.site_subdomains.include?(branch) + @called_site_subdomain = branch + branch = @host_parts[1] end + branch else nil end diff --git a/lib/divergence/respond.rb b/lib/divergence/respond.rb index ea14160..36074b7 100644 --- a/lib/divergence/respond.rb +++ b/lib/divergence/respond.rb @@ -3,7 +3,7 @@ class Application < Rack::Proxy # The main entry point for the application. This is called # by Rack. def call(env) - @req = RequestParser.new(env) + @req = RequestParser.new(env, @@config) # First, lets find out what subdomain/git branch # we're dealing with (if any). @@ -21,7 +21,7 @@ def call(env) # Lets get down to business. begin # Get the proper branch name using a touch of magic - branch = @git.discover(@req.subdomain) + branch = @git.discover(@req.branch) # Prepare the branch and cache if needed path = prepare(branch) @@ -62,7 +62,15 @@ def proxy(env) # Sets the forwarding host for the request. This is where # the proxy comes in. def fix_environment!(env) - env["HTTP_HOST"] = "#{config.forward_host}:#{config.forward_port}" + if @req.called_site_subdomain + request_domain = "#{@req.called_site_subdomain}.#{config.forward_host}:#{config.forward_port}" + else + request_domain = "#{config.forward_host}:#{config.forward_port}" + end + env["HTTP_HOST"] = request_domain + # Tell Rack::Proxy to use request_domain for the backend request. This is + # needed in case HTTP_X_FORWARDED_HOST is supplied by another proxy + Rack::Proxy.instance_method(:initialize).bind(self).call(:backend => "http://#{request_domain}") end def error!(branch) @@ -77,4 +85,4 @@ def error!(branch) [404, {"Content-Type" => "text/html"}, [contents]] end end -end \ No newline at end of file +end