Add HTTP redirect

This commit is contained in:
Omar Roth 2018-03-09 13:22:04 -06:00
parent eb1d1e30d1
commit 2596410b97
3 changed files with 27 additions and 1 deletions

View File

@ -6,3 +6,4 @@ db:
host: localhost
port: 5432
dbname: invidious
redirect: false

View File

@ -24,6 +24,7 @@ class Config
port: Int32,
dbname: String,
),
redirect: Bool
})
end

View File

@ -25,6 +25,7 @@ CONFIG = Config.from_yaml(File.read("config/config.yml"))
pool_size = CONFIG.pool_size
threads = CONFIG.threads
redirect = CONFIG.redirect
Kemal.config.extra_options do |parser|
parser.banner = "Usage: invidious [arguments]"
@ -44,6 +45,16 @@ Kemal.config.extra_options do |parser|
exit
end
end
parser.on("-r REDIRECT", "--redirect=BOOL", "Whether insecure requests should be forced to HTTPS, requires -s (default #{redirect})") do |boolean|
if boolean == "true"
redirect = true
elsif boolean == "false"
redirect = false
else
puts "REDIRECT must be 'true' or 'false'"
exit
end
end
end
Kemal::CLI.new
@ -327,6 +338,19 @@ error 500 do |env|
templated "error"
end
# Add redirect if SSL is enabled and redirect is enabled
if Kemal.config.ssl && redirect
spawn do
server = HTTP::Server.new("0.0.0.0", 80) do |context|
context.response.headers.add "Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload"
context.response.headers.add "Location", "https://#{context.request.headers["Host"]}"
context.response.status_code = 302
end
server.listen
end
end
static_headers do |response, filepath, filestat|
response.headers.add("Cache-Control", "max-age=86400")
end