Skip to content

Commit

Permalink
refactor: rename app according to html2rss namespacee
Browse files Browse the repository at this point in the history
  • Loading branch information
gildesmarais committed Aug 16, 2024
1 parent 59831cd commit b34f40f
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 96 deletions.
187 changes: 93 additions & 94 deletions app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,126 +2,125 @@

require 'roda'
require 'rack/cache'
require 'rack-timeout'
require_relative 'app/health_check'
require_relative 'app/local_config'
require_relative 'app/html2rss_facade'
require 'rack-timeout' # TODO: move to config.ru

require_relative 'roda/roda_plugins/basic_auth'

module App
module Html2rss
##
# This app uses html2rss and serves the feeds via HTTP.
#
# It is built with [Roda](https://roda.jeremyevans.net/).
class App < Roda
opts[:check_dynamic_arity] = false
opts[:check_arity] = :warn

use Rack::Timeout

use Rack::Cache,
metastore: 'file:./tmp/rack-cache-meta',
entitystore: 'file:./tmp/rack-cache-body',
verbose: (ENV.fetch('RACK_ENV', nil) == 'development')

plugin :content_security_policy do |csp|
csp.default_src :none
csp.style_src :self
csp.script_src :self
csp.connect_src :self
csp.img_src :self
csp.font_src :self
csp.form_action :self
csp.base_uri :none
csp.frame_ancestors :none
csp.block_all_mixed_content
end
module Web
class App < Roda
opts[:check_dynamic_arity] = false
opts[:check_arity] = :warn

use Rack::Timeout # TODO: move to config.ru

use Rack::Cache,
metastore: 'file:./tmp/rack-cache-meta',
entitystore: 'file:./tmp/rack-cache-body',
verbose: (ENV.fetch('RACK_ENV', nil) == 'development')

plugin :content_security_policy do |csp|
csp.default_src :none
csp.style_src :self
csp.script_src :self
csp.connect_src :self
csp.img_src :self
csp.font_src :self
csp.form_action :self
csp.base_uri :none
csp.frame_ancestors :none
csp.block_all_mixed_content
end

plugin :default_headers,
'Content-Type' => 'text/html',
'X-Frame-Options' => 'deny',
'X-Content-Type-Options' => 'nosniff',
'X-XSS-Protection' => '1; mode=block'
plugin :default_headers,
'Content-Type' => 'text/html',
'X-Frame-Options' => 'deny',
'X-Content-Type-Options' => 'nosniff',
'X-XSS-Protection' => '1; mode=block'

plugin :error_handler do |error|
handle_error(error)
end
plugin :error_handler do |error|
handle_error(error)
end

plugin :public
plugin :render, escape: true, layout: 'layout'
plugin :typecast_params
plugin :basic_auth
plugin :public
plugin :render, escape: true, layout: 'layout'
plugin :typecast_params
plugin :basic_auth

route do |r|
path = RequestPath.new(request)
route do |r|
path = RequestPath.new(request)

r.root { view 'index' }
r.root { view 'index' }

r.public
r.public

r.get 'health_check.txt' do
handle_health_check
end
r.get 'health_check.txt' do
handle_health_check
end

r.on String, String do |folder_name, config_name_with_ext|
handle_html2rss_configs(path.full_config_name, folder_name, config_name_with_ext)
end
r.on String, String do |folder_name, config_name_with_ext|
handle_html2rss_configs(path.full_config_name, folder_name, config_name_with_ext)
end

r.on String do |config_name_with_ext|
handle_local_config_feeds(path.full_config_name, config_name_with_ext)
r.on String do |config_name_with_ext|
handle_local_config_feeds(path.full_config_name, config_name_with_ext)
end
end
end

private

def handle_error(error) # rubocop:disable Metrics/MethodLength
case error
when Html2rss::Config::ParamsMissing,
Roda::RodaPlugins::TypecastParams::Error
set_error_response('Parameters missing or invalid', 422)
when Html2rss::AttributePostProcessors::UnknownPostProcessorName,
Html2rss::ItemExtractors::UnknownExtractorName,
Html2rss::Config::ChannelMissing
set_error_response('Invalid feed config', 422)
when ::App::LocalConfig::NotFound,
Html2rss::Configs::ConfigNotFound
set_error_response('Feed config not found', 404)
else
set_error_response('Internal Server Error', 500)
private

def handle_error(error) # rubocop:disable Metrics/MethodLength
case error
when Html2rss::Config::ParamsMissing,
Roda::RodaPlugins::TypecastParams::Error
set_error_response('Parameters missing or invalid', 422)
when Html2rss::AttributePostProcessors::UnknownPostProcessorName,
Html2rss::ItemExtractors::UnknownExtractorName,
Html2rss::Config::ChannelMissing
set_error_response('Invalid feed config', 422)
when ::App::LocalConfig::NotFound,
Html2rss::Configs::ConfigNotFound
set_error_response('Feed config not found', 404)
else
set_error_response('Internal Server Error', 500)
end

@show_backtrace = ENV.fetch('RACK_ENV', nil) == 'development'
@error = error
view 'error'
end

@show_backtrace = ENV.fetch('RACK_ENV', nil) == 'development'
@error = error
view 'error'
end

def set_error_response(page_title, status)
@page_title = page_title
response.status = status
end
def set_error_response(page_title, status)
@page_title = page_title
response.status = status
end

def handle_health_check
HttpCache.expires_now(response)
def handle_health_check
HttpCache.expires_now(response)

with_basic_auth(realm: HealthCheck,
username: HealthCheck::Auth.username,
password: HealthCheck::Auth.password) do
HealthCheck.run
with_basic_auth(realm: HealthCheck,
username: HealthCheck::Auth.username,
password: HealthCheck::Auth.password) do
HealthCheck.run
end
end
end

def handle_local_config_feeds(full_config_name, _config_name_with_ext)
Html2rssFacade.from_local_config(full_config_name, typecast_params) do |config|
response['Content-Type'] = 'text/xml'
HttpCache.expires(response, config.ttl * 60, cache_control: 'public')
def handle_local_config_feeds(full_config_name, _config_name_with_ext)
Html2rssFacade.from_local_config(full_config_name, typecast_params) do |config|
response['Content-Type'] = 'text/xml'
HttpCache.expires(response, config.ttl * 60, cache_control: 'public')
end
end
end

def handle_html2rss_configs(full_config_name, _folder_name, _config_name_with_ext)
Html2rssFacade.from_config(full_config_name, typecast_params) do |config|
response['Content-Type'] = 'text/xml'
HttpCache.expires(response, config.ttl * 60, cache_control: 'public')
def handle_html2rss_configs(full_config_name, _folder_name, _config_name_with_ext)
Html2rssFacade.from_config(full_config_name, typecast_params) do |config|
response['Content-Type'] = 'text/xml'
HttpCache.expires(response, config.ttl * 60, cache_control: 'public')
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require 'rack/unreloader'
Unreloader = Rack::Unreloader.new(subclasses: %w[Roda Html2rss],
logger:,
reload: dev) do
App::App
Html2rss::Web::App
end

Unreloader.require('app.rb') { 'App' }
Expand All @@ -24,4 +24,4 @@ Unreloader.require('./app/http_cache.rb')
Unreloader.require('./app/local_config.rb')
Unreloader.require('./app/request_path.rb')

run(dev ? Unreloader : App::App.freeze.app)
run(dev ? Unreloader : Html2rss::Web::App.freeze.app)

0 comments on commit b34f40f

Please sign in to comment.