Skip to content

Commit

Permalink
Move host_map logic to environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 23, 2024
1 parent 5d8ca2d commit 68fb4e1
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 68 deletions.
2 changes: 1 addition & 1 deletion falcon.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "async-http", "~> 0.57"
spec.add_dependency "async-http-cache", "~> 0.4.0"
spec.add_dependency "async-io", "~> 1.22"
spec.add_dependency "async-service", "~> 0.9.0"
spec.add_dependency "async-service", "~> 0.10.0"
spec.add_dependency "bundler"
spec.add_dependency "localhost", "~> 1.1"
spec.add_dependency "openssl", "~> 3.0"
Expand Down
37 changes: 4 additions & 33 deletions lib/falcon/command/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,10 @@ def environment(**options)
)
end

def host_map(environments)
hosts = {}

environments.each do |environment|
next unless environment.implements?(Falcon::Environment::Application)
evaluator = environment.evaluator

if RUBY_VERSION < '3.1'
# Prepare the ssl_context:
evaluator.ssl_context
end

hosts[evaluator.authority] = evaluator
end

Console.info(self) {"Hosts: #{hosts}"}

return hosts
end

def configuration
configuration = super
hosts = host_map(configuration.environments)

Configuration.new.tap do |configuration|
environment = self.environment(hosts: hosts)
configuration.add(environment)
end
end

# The container class to use.
def container_class
Async::Container.best_container_class
Configuration.for(
self.environment(environments: super.environments)
)
end

# Prepare the environment and run the controller.
Expand All @@ -90,7 +61,7 @@ def call
end
end

Async::Service::Controller.run(self.configuration, container_class: self.container_class)
Async::Service::Controller.run(self.configuration)
end

# The endpoint to bind to.
Expand Down
31 changes: 4 additions & 27 deletions lib/falcon/command/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,10 @@ def environment(**options)
)
end

def host_map(environments)
hosts = {}

environments.each do |environment|
next unless environment.implements?(Falcon::Environment::Application)
evaluator = environment.evaluator
hosts[evaluator.authority] = evaluator
end

Console.info(self) {"Hosts: #{hosts}"}

return hosts
end

def configuration
configuration = super
hosts = host_map(configuration.environments)

Configuration.new.tap do |configuration|
environment = self.environment(hosts: hosts)
configuration.add(environment)
end
end

# The container class to use.
def container_class
Async::Container.best_container_class
Configuration.for(
self.environment(environments: super.environments)
)
end

# Prepare the environment and run the controller.
Expand All @@ -83,7 +60,7 @@ def call
end
end

Async::Service::Controller.run(self.configuration, container_class: self.container_class)
Async::Service::Controller.run(self.configuration)
end

# The endpoint to bind to.
Expand Down
12 changes: 6 additions & 6 deletions lib/falcon/environment/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ def hosts
environments.each do |environment|
evaluator = environment.evaluator

# next unless environment.implements?(Falcon::Environment::Application)
if evaluator.key?(:authority) and evaluator.key?(:ssl_context) and evaluator.key?(:endpoint)
Console.logger.info(self) {"Proxying #{self.url} to #{evaluator.authority} using #{evaluator.endpoint}"}
Console.info(self) {"Proxying #{self.url} to #{evaluator.authority} using #{evaluator.endpoint}"}
hosts[evaluator.authority] = evaluator

# Pre-cache the ssl contexts:
# It seems some OpenSSL objects don't like event-driven I/O.
# service.ssl_context
else
Console.logger.warn(self) {"Ignoring environment: #{environment}, missing authority, ssl_context, or endpoint."}
if RUBY_VERSION < '3.1'
# Ensure the SSL context is set up before forking - it's buggy on Ruby < 3.1:
evaluator.ssl_context
end
end
end

Expand Down
19 changes: 18 additions & 1 deletion lib/falcon/environment/redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,25 @@ def redirect_endpoint
Async::HTTP::Endpoint.parse(redirect_url)
end

# The services we will redirect to.
# @returns [Array(Async::Service::Environment)]
def environments
[]
end

def hosts
{}
hosts = {}

environments.each do |environment|
evaluator = environment.evaluator

if environment.implements?(Falcon::Environment::Application)
Console.info(self) {"Redirecting #{self.url} to #{evaluator.authority}"}
hosts[evaluator.authority] = evaluator
end
end

return hosts
end

# Load the {Middleware::Redirect} application with the specified hosts.
Expand Down

0 comments on commit 68fb4e1

Please sign in to comment.