Skip to content

Commit

Permalink
start-proxy.rb as app entry point, reads YAML file for config
Browse files Browse the repository at this point in the history
  • Loading branch information
pcassell committed Nov 22, 2020
1 parent 6416795 commit e7578aa
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rack'
require_relative './fhir-proxy.rb'

# rackup config.ru -p 9292 -o 0.0.0.0
# FHIR_PROXY_BACKEND="https://r4.smarthealthit.org" rackup config.ru -p 9292 -o 0.0.0.0
run FHIRProxy.new
29 changes: 15 additions & 14 deletions fhir-proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
require_relative 'fhir-transaction-db.rb'

class FHIRProxy < Rack::Proxy
attr_accessor :config_mode #global var
def initialize(app = nil, opts = {})
attr_accessor :config_mode # global var
def initialize(myopts = {}, app = nil, opts = {})
super(app, opts)
@streaming = false
File.open('log.txt', 'w') { |f| f.write "#{Time.now} - User logged in\n" }
set_backend_or_die
@fhir_db = FHIRTransactionDB.new('fhir-transactions.db')
parse_myopts(myopts)
@fhir_db = FHIRTransactionDB.new(@db_name)
end

def call(env)
Expand All @@ -20,7 +20,6 @@ def call(env)
# Client -> Proxy (Save req here) -> Server
# Client <- (Save res here) Proxy <- Server
new_env = rewrite_env(env)
# -> new class validation (env)
req_id = record_request(new_env)
res_triple = rewrite_response(perform_request(new_env))
res_id = record_response(res_triple, req_id)
Expand Down Expand Up @@ -51,7 +50,7 @@ def rewrite_env(env)
elsif dst && URI(dst).is_a?(URI::HTTP) && dst.to_s.include?('https:')
@backend = URI(dst)
@use_ssl= true
#env["HTTP_X_FORWARDED_PROTO"] = 'https'
# env["HTTP_X_FORWARDED_PROTO"] = 'https'
msg_out(msg)
end
end
Expand Down Expand Up @@ -108,16 +107,18 @@ def msg_out(msg, stdout = true, file = true)
File.write('log.txt', "#{msg} \n", mode: 'a') if file
end

def set_backend_or_die
dst = ENV['FHIR_PROXY_BACKEND']
if dst && URI(dst).is_a?(URI::HTTP)
@backend = URI(dst)
msg_out("@backend set to: #{dst}")
def parse_myopts(myopts)
@db_name = myopts[:db] || 'fhir-transactions.db'
backend_str = ENV['FHIR_PROXY_BACKEND'] if ENV['FHIR_PROXY_BACKEND']
backend_str = myopts[:backend] if myopts[:backend]
if backend_str && URI(backend_str).is_a?(URI::HTTP)
@backend = URI(backend_str)
msg_out("@backend set to: #{@backend}")
else
msg = <<~HELP_DOC
FHIR_PROXY_BACKEND environment variable not set or bad URI.
Please specify proxy destination.
Example: FHIR_PROXY_BACKEND="https://r4.fhir-server-dest.com" rackup config.ru -p 9292 -o 0.0.0.0
Proxy 'backend' config option not set or bad URI.
Please specify proxy destination via proxy.yml file or environment variable FHIR_PROXY_BACKEND
Example: "https://r4.smarthealthit.org"
HELP_DOC
msg_out(msg)
exit(1)
Expand Down
5 changes: 5 additions & 0 deletions proxy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
:Host: 0.0.0.0
:Port: 9292
:backend: https://r4.smarthealthit.org
:db: fhir-transactions.db
28 changes: 28 additions & 0 deletions start-proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'rack'
require 'yaml'
require_relative './fhir-proxy.rb'

# Options for Rack are listed here
# https://github.com/rack/rack/blob/master/lib/rack/server.rb
#
begin
proxy_options = YAML.load_file('proxy.yml')
rescue Errno::ENOENT
puts "Could not find file 'proxy.yml'"
puts 'Creating one with default values'
proxy_options = {
Host: '0.0.0.0',
Port: 9292,
backend: 'https://r4.smarthealthit.org',
db: 'fhir-transactions.db'
}
File.write('proxy.yml', proxy_options.to_yaml)
end
Rack::Server.start(
app: FHIRProxy.new(
'backend': proxy_options[:backend],
'db': proxy_options[:db]
),
Host: proxy_options[:Host],
Port: proxy_options[:Port]
)

0 comments on commit e7578aa

Please sign in to comment.