Skip to content

Commit

Permalink
1221 modify configuration to configure pipes
Browse files Browse the repository at this point in the history
Modify configuration such that each pipe can be
independantly configured via settings
  • Loading branch information
midhunkrishna committed Oct 3, 2020
1 parent 189654d commit 407a9fd
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 26 deletions.
6 changes: 3 additions & 3 deletions spec/amber/environment/settings_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ module Amber::Environment
it "sets default headers value as empty map" do
test_yaml = File.read(File.expand_path("./spec/support/config/test.yml"))
settings = Amber::Settings.from_yaml(test_yaml)
settings.public_file_server["headers"].should eq({} of String => String)
settings.pipes.dig?("static", "headers").should eq({} of String => Amber::Settings::SettingValue)
end

it "sets header file settings from environment yaml file" do
test_yaml = File.read(File.expand_path("./spec/support/config/with_public_file_server.yml"))
test_yaml = File.read(File.expand_path("./spec/support/config/with_static_pipe_settings.yml"))
settings = Amber::Settings.from_yaml(test_yaml)
settings.public_file_server["headers"].should eq({"Cache-Control" => "private, max-age=7200"})
settings.pipes.dig?("static", "headers").should eq({"Cache-Control" => "private, max-age=7200"})
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/amber/pipes/static_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ module Amber

response = create_request_and_return_io(static, request)

response.headers["Cache-Control"].should eq "private, max-age=3600"
response.headers["Accept-Ranges"].should eq "bytes"
response.headers["X-Content-Type-Options"].should eq "nosniff"
response.headers["Cache-Control"].should eq "no-store"
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/support/config/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ session:
store: "signed_cookie"
expires: 0

pipes:
static:
headers:
"Cache-Control": "no-store"

secrets:
description: "Store your test secrets credentials and settings here."

Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ session:
store: "encrypted_cookie"
expires: 10

pipes:
static:
headers:
"Cache-Control": "private, max-age=7200"

secrets:
description: "Store your test secrets credentials and settings here."

public_file_server:
headers:
"Cache-Control": "private, max-age=7200"
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ session:
smtp:
enabled: false

pipes:
static:
headers:
"Cache-Control": "no-store"

secrets:
description: Store your development secrets credentials and settings here.

public_file_server:
headers:
"Cache-Control": "no-store"
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ smtp:
username: api_key
enabled: false

pipes:
static:
headers:
"Cache-Control": "private, max-age=3600"

secrets:
description: Store your production secrets credentials and settings here.

public_file_server:
headers:
"Cache-Control": "private, max-age=3600"
10 changes: 6 additions & 4 deletions src/amber/environment/settings.cr
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ module Amber::Environment
},
},
auto_reload: {type: Bool, default: false},
public_file_server: {
type: Hash(String, Hash(String, String)),
pipes: {
type: Hash(String, Hash(String, Hash(String, SettingValue))),
default: {
"headers" => {} of String => String,
},
"static" => {
"headers" => {} of String => SettingValue
}
}
}
)

Expand Down
16 changes: 11 additions & 5 deletions src/amber/pipes/static.cr
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,20 @@ module Amber
end

private def add_response_headers(env : HTTP::Server::Context)
{
pipes = Amber.settings.pipes
default_headers = {
"Accept-Ranges" => "bytes",
"X-Content-Type-Options" => "nosniff",
"Cache-Control" => "private, max-age=3600",
}.merge(
Amber.settings.public_file_server["headers"]
).each do |key, value|
env.response.headers[key] = value
} of String => Amber::Settings::SettingValue

headers = if pipes.has_key?("static") && pipes["static"].has_key?("headers")
default_headers.merge(pipes["static"]["headers"])
else
default_headers
end
headers.each do |key, value|
env.response.headers[key] = value.as(String)
end
end

Expand Down

0 comments on commit 407a9fd

Please sign in to comment.