-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.rb
115 lines (96 loc) 路 3.26 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true
require 'roda'
require 'rack/cache'
require 'rack-timeout'
require_relative 'app/health_check'
require_relative 'app/local_config'
require_relative 'app/html2rss_facade'
require_relative 'roda/roda_plugins/basic_auth'
module App
##
# 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
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|
case error
when Html2rss::Config::ParamsMissing,
Roda::RodaPlugins::TypecastParams::Error
@page_title = 'Parameters missing or invalid'
response.status = 422
when Html2rss::AttributePostProcessors::UnknownPostProcessorName,
Html2rss::ItemExtractors::UnknownExtractorName,
Html2rss::Config::ChannelMissing
@page_title = 'Invalid feed config'
response.status = 422
when ::App::LocalConfig::NotFound,
Html2rss::Configs::ConfigNotFound
@page_title = 'Feed config not found'
response.status = 404
else
@page_title = 'Internal Server Error'
response.status = 500
end
@show_backtrace = ENV.fetch('RACK_ENV', nil) == 'development'
@error = error
view 'error'
end
plugin :public
plugin :render, escape: true, layout: 'layout'
plugin :typecast_params
plugin :basic_auth
route do |r|
path = RequestPath.new(request)
r.root do
view 'index'
end
r.public
r.get 'health_check.txt' do |_|
HttpCache.expires_now(response)
with_basic_auth(realm: HealthCheck,
username: HealthCheck::Auth.username,
password: HealthCheck::Auth.password) do
HealthCheck.run
end
end
# Route for feeds from the local feeds.yml
r.get String do |_config_name_with_ext|
Html2rssFacade.from_local_config(path.full_config_name, typecast_params) do |config|
response['Content-Type'] = 'text/xml'
HttpCache.expires(response, config.ttl * 60, cache_control: 'public')
end
end
# Route for feeds from html2rss-configs
r.get String, String do |_folder_name, _config_name_with_ext|
Html2rssFacade.from_config(path.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
end