diff --git a/config.ru b/config.ru index 691ab46718..da93364685 100644 --- a/config.ru +++ b/config.ru @@ -1,5 +1,7 @@ -$LOAD_PATH.unshift *Dir[File.expand_path('lib', __dir__), File.expand_path('modules', __dir__)] +$LOAD_PATH.unshift(*Dir[File.expand_path('lib', __dir__), File.expand_path('modules', __dir__)]) require 'smart_proxy_main' -::Proxy::PluginInitializer.new(::Proxy::Plugins.instance).initialize_plugins -::Proxy::Plugins.instance.select { |p| p[:state] == :running && p[:https_enabled] }.each { |p| instance_eval(p[:class].https_rackup) } +require 'proxy/app' +plugins = ::Proxy::Plugins.instance +::Proxy::PluginInitializer.new(plugins).initialize_plugins +run ::Proxy::App.new(plugins) diff --git a/lib/proxy/app.rb b/lib/proxy/app.rb new file mode 100644 index 0000000000..5ca0010861 --- /dev/null +++ b/lib/proxy/app.rb @@ -0,0 +1,29 @@ +module Proxy + class App + def initialize(plugins) + @apps = {} + + http_plugins = plugins.select { |p| p[:state] == :running && p[:http_enabled] } + if http_plugins.any? + @apps['http'] = Rack::Builder.new do + http_plugins.each { |p| instance_eval(p[:class].http_rackup) } + end + end + + https_plugins = plugins.select { |p| p[:state] == :running && p[:https_enabled] } + if https_plugins.any? + @apps['https'] = Rack::Builder.new do + https_plugins.each { |p| instance_eval(p[:class].https_rackup) } + end + end + end + + def call(env) + # TODO: Respect X-Forwarded-Proto? + scheme = env['rack.url_scheme'] + app = @apps[scheme] + fail "Unsupported URL scheme #{scheme}" unless app + app.call(env) + end + end +end