front/front_misc.rb
# frozen_string_literal: true
# Copyright (c) 2019-2024 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
get '/js/*.js' do
file = "#{File.join('js', params[:splat].first)}.js"
error(404, "File not found: #{file}") unless File.exist?(file)
content_type 'application/javascript'
File.read(file)
end
get '/robots.txt' do
content_type 'text/plain'
"User-agent: *\nDisallow: /"
end
get '/version' do
content_type 'text/plain'
Rsk::VERSION
end
not_found do
status 404
content_type 'text/html', charset: 'utf-8'
haml :not_found, layout: :layout, locals: merged(
title: request.url
)
end
error do
status 503
e = env['sinatra.error']
if e.is_a?(Rsk::Urror)
flash(@locals[:user] ? '/ranked' : '/', e.message, color: 'darkred')
else
Raven.capture_exception(e)
haml(
:error,
layout: :layout,
locals: merged(
title: 'error',
error: "#{e.message}\n\t#{e.backtrace.join("\n\t")}"
)
)
end
end
def context
"#{request.ip} #{request.user_agent} #{Rsk::VERSION} #{Time.now.strftime('%Y/%m')}"
end
def merged(hash)
out = @locals.merge(hash)
out[:local_assigns] = out
if cookies[:flash_msg]
out[:flash_msg] = cookies[:flash_msg]
cookies.delete(:flash_msg)
end
out[:flash_color] = cookies[:flash_color] || 'darkgreen'
cookies.delete(:flash_color)
out
end
def flash(uri, msg = '', color: 'darkgreen')
cookies[:flash_msg] = msg
cookies[:flash_color] = color
redirect(uri)
end