Skip to content

Commit

Permalink
Resolve (glob) configuration paths.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 7, 2024
1 parent 1716c66 commit aab8e97
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 71 deletions.
5 changes: 2 additions & 3 deletions lib/falcon/command/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
# Released under the MIT License.
# Copyright, 2018-2024, by Samuel Williams.

require_relative '../service/virtual'
require_relative 'paths'
require_relative '../environment/virtual'

require 'samovar'

Expand All @@ -31,7 +30,7 @@ class Virtual < Samovar::Command
many :paths

def environment
Async::Service::Environment.new(Falcon::Service::Virtual::Environment).with(
Async::Service::Environment.new(Falcon::Environment::Virtual).with(
verbose: self.parent&.verbose?,
configuration_paths: self.paths,
bind_insecure: @options[:bind_insecure],
Expand Down
29 changes: 29 additions & 0 deletions lib/falcon/environment/configured.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

module Falcon
module Environment
# This module provides a common interface for configuring the Falcon application.
# @todo Reuse this for proxy and redirect services.
module Configured
# All the falcon application configuration paths.
# @returns [Array(String)] Paths to the falcon application configuration files.
def configuration_paths
["/srv/http/*/falcon.rb"]
end

# All the falcon application configuration paths, with wildcards expanded.
def resolved_configuration_paths
configuration_paths.flat_map do |path|
Dir.glob(path)
end.uniq
end

def configuration
::Async::Service::Configuration.load(resolved_configuration_paths)
end
end
end
end
67 changes: 67 additions & 0 deletions lib/falcon/environment/virtual.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require_relative 'configured'

require_relative '../service/virtual'

module Falcon
module Environment
module Virtual
include Configured

# The service class to use for the virtual host.
# @returns [Class]
def service_class
Service::Virtual
end

def name
service_class.name
end

# The URI to bind the `HTTPS` -> `falcon host` proxy.
def bind_secure
"https://[::]:443"
end

# The URI to bind the `HTTP` -> `HTTPS` redirector.
def bind_insecure
"http://[::]:80"
end

# The connection timeout to use for incoming connections.
def timeout
10.0
end

# The path to the falcon executable from this gem.
# @returns [String]
def falcon_path
File.expand_path("../../../bin/falcon", __dir__)
end

# # The insecure endpoint for connecting to the {Redirect} instance.
# def insecure_endpoint(**options)
# Async::HTTP::Endpoint.parse(bind_insecure, **options)
# end

# # The secure endpoint for connecting to the {Proxy} instance.
# def secure_endpoint(**options)
# Async::HTTP::Endpoint.parse(bind_secure, **options)
# end

# # An endpoint suitable for connecting to the specified hostname.
# def host_endpoint(hostname, **options)
# endpoint = secure_endpoint(**options)

# url = URI.parse(bind_secure)
# url.hostname = hostname

# return Async::HTTP::Endpoint.new(url, hostname: endpoint.hostname)
# end
end
end
end
72 changes: 4 additions & 68 deletions lib/falcon/service/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,6 @@ module Service
#
# A virtual host is an application bound to a specific authority (essentially a hostname). The virtual controller manages multiple hosts and allows a single server to host multiple applications easily.
class Virtual < Async::Service::Generic
module Environment
# The service class to use for the virtual host.
# @returns [Class]
def service_class
Virtual
end

def name
service_class.name
end

# All the falcon application configuration paths.
# @returns [Array(String)] Paths to the falcon application configuration files.
def configuration_paths
["/srv/http/*/falcon.rb"]
end

def configuration
::Async::Service::Configuration.load(configuration_paths)
end

# The URI to bind the `HTTPS` -> `falcon host` proxy.
def bind_secure
"https://[::]:443"
end

# The URI to bind the `HTTP` -> `HTTPS` redirector.
def bind_insecure
"http://[::]:80"
end

# The connection timeout to use for incoming connections.
def timeout
10.0
end

# # The insecure endpoint for connecting to the {Redirect} instance.
# def insecure_endpoint(**options)
# Async::HTTP::Endpoint.parse(bind_insecure, **options)
# end

# # The secure endpoint for connecting to the {Proxy} instance.
# def secure_endpoint(**options)
# Async::HTTP::Endpoint.parse(bind_secure, **options)
# end

# # An endpoint suitable for connecting to the specified hostname.
# def host_endpoint(hostname, **options)
# endpoint = secure_endpoint(**options)

# url = URI.parse(bind_secure)
# url.hostname = hostname

# return Async::HTTP::Endpoint.new(url, hostname: endpoint.hostname)
# end
end

def self.included(target)
target.include(Environnment)
end

# Drop privileges according to the user and group of the specified path.
# @parameter path [String] The path to the application directory.
def assume_privileges(path)
Expand Down Expand Up @@ -102,12 +41,6 @@ def spawn(path, container, **options)
end
end

# The path to the falcon executable from this gem.
# @returns [String]
def falcon_path
File.expand_path("../../../bin/falcon", __dir__)
end

# Setup the container with {Redirect} and {Proxy} child processes.
# These processes are gracefully restarted if they are already running.
# @parameter container [Async::Container::Generic]
Expand All @@ -122,8 +55,11 @@ def setup(container)

container.reload do
evaluator = @environment.evaluator
falcon_path = evaluator.falcon_path

Console.info(self, "Loading configurations from:", evaluator.resolved_configuration_paths)

evaluator.configuration_paths.each do |path|
evaluator.resolved_configuration_paths.each do |path|
path = File.expand_path(path)
root = File.dirname(path)

Expand Down

0 comments on commit aab8e97

Please sign in to comment.