Skip to content

Commit

Permalink
update approach to appending suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
cplee committed Aug 10, 2018
1 parent b0fa225 commit 4662262
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,21 @@ def nodejs_uri
# Resolve the SRV records for the hostname in the URL
def expand_url(url)
uri = URI(url)
resolver = Resolv::DNS.new(:search => [ENV["_SERVICE_DISCOVERY_NAME"]])
srv = resolver.getresource("_#{uri.scheme}._tcp.#{uri.host}", Resolv::DNS::Resource::IN::SRV)
uri.host = srv.target.to_s
uri.port = srv.port.to_s
resolver = Resolv::DNS.new()

# if host is relative, append the service discovery name
host = uri.host.count('.') > 0 ? uri.host : "#{uri.host}.#{ENV["_SERVICE_DISCOVERY_NAME"]}"

# lookup the SRV record and use if found
begin
srv = resolver.getresource("_#{uri.scheme}._tcp.#{host}", Resolv::DNS::Resource::IN::SRV)
uri.host = srv.target.to_s
uri.port = srv.port.to_s
rescue => e
logger.error e.message
logger.error e.backtrace.join("\n")
end

logger.info "expanded #{url} to #{uri}"
uri
end
Expand Down

0 comments on commit 4662262

Please sign in to comment.