Skip to content
This repository was archived by the owner on Jul 27, 2021. It is now read-only.

Commit e744e19

Browse files
committed
change to using service discovery for backend
1 parent 714357a commit e744e19

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

app/controllers/application_controller.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
require 'net/http'
2+
require 'resolv'
3+
require 'uri'
24

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

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

5252
def backend_addr
53-
backend_addr = ENV["BACKEND_API"]
54-
# The address will be of the form, http://172.17.0.5:5432, so we add a trailing slash
55-
backend_addr.sub(/^http:/, 'http:') + "/"
53+
expand_url ENV["NODEJS_URL"]
54+
end
55+
56+
# Resolve the SRV records for the hostname in the URL
57+
def expand_url(url)
58+
uri = URI(url)
59+
resolver = Resolv::DNS.new()
60+
srv = resolver.getresource("_#{uri.scheme}._tcp.#{uri.host}", Resolv::DNS::Resource::IN::SRV)
61+
uri.host = srv.target.to_s
62+
uri.port = srv.port.to_s
63+
uri.to_s
5664
end
5765

5866
before_action :discover_availability_zone

mu.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ service:
55
port: 3000
66
pathPatterns:
77
- /*
8-
networkMode: awsvpc
98
environment:
10-
BACKEND_API: "http://api.internal.service:80"
9+
CRYSTAL_URL: "http://ecsdemo-crystal.${_SERVICE_DISCOVERY_NAMESPACE}/crystal"
10+
NODEJS_URL: "http://ecsdemo-nodejs.${_SERVICE_DISCOVERY_NAMESPACE}/"

0 commit comments

Comments
 (0)