Skip to content

Commit ffa7045

Browse files
Simplify serve configuration path resolution.
1 parent bc930c7 commit ffa7045

3 files changed

Lines changed: 21 additions & 22 deletions

File tree

lib/falcon/command/serve.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ def name
7171
# Create the environment for the serve command.
7272
# @returns [Async::Service::Environment] The configured server environment.
7373
def environment
74-
Async::Service::Environment.new(Falcon::Environment::Server).with(
75-
Falcon::Environment::Serve,
74+
environment_options = {
7675
root: Dir.pwd,
7776

7877
verbose: self.parent&.verbose?,
@@ -81,13 +80,21 @@ def environment
8180
container_options: self.container_options,
8281
endpoint_options: self.endpoint_options,
8382

84-
configuration_path: @options[:config],
8583
preload: [@options[:preload]].compact,
8684
url: @options[:bind],
8785

8886
name: self.name,
8987

90-
endpoint: ->{Endpoint.parse(url, **endpoint_options)}
88+
endpoint: ->{Endpoint.parse(url, **endpoint_options)},
89+
}
90+
91+
if configuration_path = @options[:config]
92+
environment_options[:configuration_path] = File.expand_path(configuration_path, Dir.pwd)
93+
end
94+
95+
return Async::Service::Environment.new(Falcon::Environment::Server).with(
96+
Falcon::Environment::Serve,
97+
**environment_options,
9198
)
9299
end
93100

lib/falcon/environment/serve.rb

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,9 @@ module Falcon
1212
module Environment
1313
# Provides configuration discovery and loading for `falcon serve`.
1414
module Serve
15-
# The explicitly specified application configuration path, if any.
16-
# @returns [String | Nil]
17-
def configuration_path
18-
nil
19-
end
20-
21-
# Resolve the application configuration path.
15+
# Discover the application configuration path.
2216
# @returns [String] The absolute application configuration path.
23-
def resolved_configuration_path
24-
if configuration_path
25-
return File.expand_path(configuration_path, root)
26-
end
27-
17+
def configuration_path
2818
serve_path = File.expand_path("config/serve.rb", root)
2919
if File.file?(serve_path)
3020
return serve_path
@@ -41,7 +31,7 @@ def resolved_configuration_path
4131
# Load and wrap the configured application.
4232
# @returns [Protocol::HTTP::Middleware] The middleware stack.
4333
def middleware
44-
path = resolved_configuration_path
34+
path = configuration_path
4535

4636
case File.extname(path)
4737
when ".rb"

test/falcon/environment/serve.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
File.write(File.join(root, "config.ru"), "run ->(env) {[200, {}, []]}\n")
2727
File.write(File.join(root, "config/serve.rb"), "run Protocol::HTTP::Middleware::Okay\n")
2828

29-
expect(evaluator.resolved_configuration_path).to be == File.join(root, "config/serve.rb")
29+
expect(evaluator.configuration_path).to be == File.join(root, "config/serve.rb")
3030
end
3131

3232
it "loads config/serve.rb as protocol middleware" do
@@ -39,19 +39,21 @@
3939
it "falls back to config.ru" do
4040
File.write(File.join(root, "config.ru"), "run ->(env) {[200, {}, []]}\n")
4141

42-
expect(evaluator.resolved_configuration_path).to be == File.join(root, "config.ru")
42+
expect(evaluator.configuration_path).to be == File.join(root, "config.ru")
4343
end
4444

4545
it "uses an explicit configuration path" do
46+
configuration_path = File.join(root, "application.rb")
47+
4648
evaluator = Async::Service::Environment.build(
4749
Falcon::Environment::Server,
4850
subject,
4951
root: root,
5052
name: "localhost",
51-
configuration_path: "application.rb",
53+
configuration_path: configuration_path,
5254
).evaluator
5355

54-
expect(evaluator.resolved_configuration_path).to be == File.join(root, "application.rb")
56+
expect(evaluator.configuration_path).to be == configuration_path
5557
end
5658

5759
it "rejects unsupported configuration extensions" do
@@ -70,7 +72,7 @@
7072

7173
it "fails when no configuration exists" do
7274
expect do
73-
evaluator.resolved_configuration_path
75+
evaluator.configuration_path
7476
end.to raise_exception(ArgumentError, message: be(:include?, "Could not find"))
7577
end
7678
end

0 commit comments

Comments
 (0)