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

new logic for app mesh workshop #20

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 56 additions & 5 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'net/http'
require 'resolv'
require 'uri'
require 'time'

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
Expand All @@ -9,8 +10,17 @@ class ApplicationController < ActionController::Base

# Example endpoint that calls the backend nodejs api
def index

epochms = (Time.now.to_f * 1000).to_i

begin
req = Net::HTTP::Get.new(nodejs_uri.to_s)
# req = Net::HTTP::Get.new(nodejs_uri.path.presence || "/")
req = Net::HTTP::Get.new(nodejs_uri.request_uri)
if request.headers["HTTP_CANARY_FLEET"].present?
req["canary_fleet"] = request.headers["HTTP_CANARY_FLEET"]
end

req["epoch_ms"] = epochms
res = Net::HTTP.start(nodejs_uri.host, nodejs_uri.port) {|http|
http.read_timeout = 2
http.open_timeout = 2
Expand All @@ -30,7 +40,13 @@ def index
end

begin
crystalreq = Net::HTTP::Get.new(crystal_uri.to_s)
# crystalreq = Net::HTTP::Get.new(crystal_uri.path.presence || "/")
crystalreq = Net::HTTP::Get.new(crystal_uri.request_uri)
if request.headers["HTTP_CANARY_FLEET"].present?
crystalreq["canary_fleet"] = request.headers["HTTP_CANARY_FLEET"]
end

crystalreq["epoch_ms"] = epochms
crystalres = Net::HTTP.start(crystal_uri.host, crystal_uri.port) {|http|
http.read_timeout = 2
http.open_timeout = 2
Expand All @@ -48,6 +64,32 @@ def index
logger.error e.backtrace.join("\n")
@crystal = "no backend found"
end

if params[:type].present? && params[:type] == "json"
response = { }
response[:ruby] = {
:az => @az,
:hash => @code_hash,
:canary_fleet => request.headers["HTTP_CANARY_FLEET"].present?,
:epoch_ms => epochms
}

if @crystal != "no backend found"
response[:crystal] = { :text => @crystal }
end

if @text != "no backend found"
response[:nodejs] = { :text => @text }
end

render json: response.to_json

end

end

def json
redirect_to root_path(:type => "json")
end

# This endpoint is used for health checks. It should return a 200 OK when the app is up and ready to serve requests.
Expand All @@ -56,11 +98,19 @@ def health
end

def crystal_uri
expand_url ENV["CRYSTAL_URL"]
if ENV['MESH_RUN'].present? && ENV['MESH_RUN'] == 'true'
uri = URI(ENV["CRYSTAL_URL"])
else
expand_url ENV["CRYSTAL_URL"]
end
end

def nodejs_uri
expand_url ENV["NODEJS_URL"]
if ENV['MESH_RUN'].present? && ENV['MESH_RUN'] == 'true'
uri = URI(ENV["NODEJS_URL"])
else
expand_url ENV["NODEJS_URL"]
end
end

# Resolve the SRV records for the hostname in the URL
Expand Down Expand Up @@ -104,4 +154,5 @@ def code_hash
def custom_header
response.headers['Cache-Control'] = 'max-age=86400, public'
end
end

end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@

# This URL is used for health checks
get 'health' => 'application#health'

# This URL is used for json
get 'json' => 'application#json'

end