Skip to content

Commit

Permalink
Add support for domain alias lookup. Fixes codemancers#202
Browse files Browse the repository at this point in the history
Config now accepts a new directive: alias
Multiple aliases can be separated by a comma
Special characters are:
        *: Will match one subpath
        **: Will match any subpaths
  • Loading branch information
bhenderson committed Oct 9, 2017
1 parent 8a03b5c commit 70d45e4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
28 changes: 26 additions & 2 deletions lib/invoker/dns_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,41 @@ def initialize(config)
@dns_mutex = Mutex.new
Invoker.config.processes.each do |process|
if process.port
dns_data[process.label] = { 'port' => process.port }
dns_data[process.label] = { 'port' => process.port, 'alias' => make_regex(process.alias) }
end
end
end

def [](process_name)
@dns_mutex.synchronize { dns_data[process_name] }
@dns_mutex.synchronize { dns_data[process_name] || alias_lookup(process_name) }
end

def add(name, port, ip = nil)
@dns_mutex.synchronize { dns_data[name] = { 'port' => port, 'ip' => ip } }
end

def alias_lookup(process_name)
dns_data.each do |label, opts|
if re = opts['alias']
return opts if re =~ process_name
end
end
nil
end

def make_regex(aliases)
return unless aliases
aliases = aliases.split(/\s*,\s*/)

aliases.map! do |path|
path.gsub!('.', '\.')
path.gsub!(/\*+/) do |match|
match == '*' ? '[^.]+' : '.*'
end
%r/\A#{path}\z/
end

Regexp.union aliases
end
end
end
47 changes: 47 additions & 0 deletions spec/invoker/power/url_rewriter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,53 @@ def rewriter.dns_check(*args)
match = rewriter.select_backend_config('baz.foo.dev')
expect(match.port).to eql(1)
end

it 'does not match baz.dev' do
match = rewriter.select_backend_config('baz.dev')
expect(match.port).to be_nil
end

context 'with an alias' do
it 'matches baz.foo.dev to the alias' do
@processes[1][:alias] = '*.foo'
Invoker.dns_cache = Invoker::DNSCache.new(nil)

match = rewriter.select_backend_config('baz.foo.dev')
expect(match.port).to eql(2)
end

it 'matches baz.dev to the alias' do
@processes[1][:alias] = '*'
Invoker.dns_cache = Invoker::DNSCache.new(nil)

match = rewriter.select_backend_config('baz.dev')
expect(match.port).to eql(2)
end

it 'matches non wildcard alias' do
@processes[1][:alias] = 'baz'
Invoker.dns_cache = Invoker::DNSCache.new(nil)

match = rewriter.select_backend_config('baz.dev')
expect(match.port).to eql(2)
end

it 'does not match baz.foo.blah.dev' do
@processes[1][:alias] = '*.foo'
Invoker.dns_cache = Invoker::DNSCache.new(nil)

match = rewriter.select_backend_config('baz.foo.blah.dev')
expect(match.port).to be_nil
end

it 'matches **' do
@processes[1][:alias] = '**.foo'
Invoker.dns_cache = Invoker::DNSCache.new(nil)

match = rewriter.select_backend_config('a.b.c.foo.dev')
expect(match.port).to eql(2)
end
end
end
end
end

0 comments on commit 70d45e4

Please sign in to comment.