Skip to content

Commit

Permalink
cleanup uri handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cplee committed Aug 10, 2018
1 parent 122d474 commit d8e9b9c
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ class ApplicationController < ActionController::Base
# Example endpoint that calls the backend nodejs api
def index
begin
url = URI.parse(backend_addr)
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
req = Net::HTTP::Get.new(nodejs_uri.to_s)
res = Net::HTTP.start(nodejs_uri.host, nodejs_uri.port) {|http|
http.request(req)
}

Expand All @@ -22,9 +21,8 @@ def index
@text = "no backend found"
end

crystalurl = URI.parse(crystal_addr)
crystalreq = Net::HTTP::Get.new(crystalurl.to_s)
crystalres = Net::HTTP.start(crystalurl.host, crystalurl.port) {|http|
crystalreq = Net::HTTP::Get.new(crystal_uri.to_s)
crystalres = Net::HTTP.start(crystal_uri.host, crystal_uri.port) {|http|
http.request(crystalreq)
}

Expand All @@ -34,7 +32,9 @@ def index
@crystal = "no backend found"
end

rescue
rescue => e
logger.error e.message
logger.error e.backtrace.join("\n")
@text = "no backend found"
@crystal = "no backend found"
end
Expand All @@ -45,11 +45,11 @@ def health
render plain: "OK"
end

def crystal_addr
def crystal_uri
expand_url ENV["CRYSTAL_URL"]
end

def backend_addr
def nodejs_uri
expand_url ENV["NODEJS_URL"]
end

Expand All @@ -60,7 +60,8 @@ def expand_url(url)
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
uri.to_s
logger.info "expanded #{url} to #{uri}"
uri
end

before_action :discover_availability_zone
Expand Down

0 comments on commit d8e9b9c

Please sign in to comment.