Skip to content

Commit

Permalink
change to using service discovery for backend
Browse files Browse the repository at this point in the history
  • Loading branch information
cplee committed Aug 10, 2018
1 parent 714357a commit e744e19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'net/http'
require 'resolv'
require 'uri'

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
Expand Down Expand Up @@ -44,15 +46,21 @@ def health
end

def crystal_addr
crystal_addr = ENV["BACKEND_API"]
# The address will be of the form, http:https://172.17.0.5:5432, so we add a trailing slash
crystal_addr.sub(/^http:/, 'http:') + "/crystal"
expand_url ENV["CRYSTAL_URL"]
end

def backend_addr
backend_addr = ENV["BACKEND_API"]
# The address will be of the form, http:https://172.17.0.5:5432, so we add a trailing slash
backend_addr.sub(/^http:/, 'http:') + "/"
expand_url ENV["NODEJS_URL"]
end

# Resolve the SRV records for the hostname in the URL
def expand_url(url)
uri = URI(url)
resolver = Resolv::DNS.new()
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
end

before_action :discover_availability_zone
Expand Down
4 changes: 2 additions & 2 deletions mu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ service:
port: 3000
pathPatterns:
- /*
networkMode: awsvpc
environment:
BACKEND_API: "http:https://api.internal.service:80"
CRYSTAL_URL: "http:https://ecsdemo-crystal.${_SERVICE_DISCOVERY_NAMESPACE}/crystal"
NODEJS_URL: "http:https://ecsdemo-nodejs.${_SERVICE_DISCOVERY_NAMESPACE}/"

0 comments on commit e744e19

Please sign in to comment.