Add HTTP redirect

pull/3/head
Omar Roth 7 years ago
parent eb1d1e30d1
commit 2596410b97

@ -5,4 +5,5 @@ db:
password: kemal
host: localhost
port: 5432
dbname: invidious
dbname: invidious
redirect: false

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

@ -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

Loading…
Cancel
Save