Skip to content

Commit d2f8fc8

Browse files
committed
Move some Roda plugins out of server default and into SSR plugin
- Add new documentation on how Roda is configured - BREAKING CHANGE: the `flash` plugin is no longer loaded by the file-based routes plugin by default, it's now configured with `sessions: true` in the ssr plugin init. Flash required sessions to be set up anyway, which used to be manual but is now provided by this new config.
1 parent 4d3eea6 commit d2f8fc8

File tree

15 files changed

+185
-87
lines changed

15 files changed

+185
-87
lines changed

bridgetown-core/lib/bridgetown-core/rack/boot.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ def self.boot(*)
2424
Bridgetown::Utils::LoadersManager.new(Bridgetown::Current.preloaded_configuration)
2525
Bridgetown::Current.preloaded_configuration.run_initializers! context: :server
2626
autoload_server_folder
27-
rescue Roda::RodaError => e
28-
if e.message.include?("sessions plugin :secret option")
29-
raise Bridgetown::Errors::InvalidConfigurationError,
30-
"The Roda sessions plugin can't find a valid secret. Run `bin/bridgetown secret' " \
31-
"and put the key in a ENV var you can use to configure the session in the Roda app"
32-
end
33-
34-
raise e
3527
end
3628

3729
# @param root [String] root of Bridgetown site, defaults to config value

bridgetown-core/lib/bridgetown-core/utils/initializers.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
Bridgetown.load_dotenv root: config.root_dir
55
end
66

7-
Bridgetown.initializer :ssr do |config, setup: nil|
7+
Bridgetown.initializer :ssr do |config, setup: nil, **options|
88
config.roda do |app|
9-
app.plugin(:bridgetown_ssr, &setup)
9+
app.plugin(:bridgetown_ssr, options, &setup)
1010
end
1111
end
1212

bridgetown-core/lib/roda/plugins/bridgetown_server.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ def self.load_dependencies(app) # rubocop:disable Metrics
1313

1414
app.extend ClassMethods # we need to do this here because Roda hasn't done it yet
1515
app.plugin :initializers
16-
app.plugin :method_override
17-
app.plugin :all_verbs
18-
app.plugin :hooks
1916
app.plugin :common_logger, Bridgetown::Rack::Logger.new($stdout), method: :info
2017
app.plugin :json
2118
app.plugin :json_parser
22-
app.plugin :indifferent_params
23-
app.plugin :cookies, path: "/"
2419
app.plugin :ssg, root: Bridgetown::Current.preloaded_configuration.destination
2520
app.plugin :not_found do
2621
output_folder = Bridgetown::Current.preloaded_configuration.destination

bridgetown-core/lib/roda/plugins/bridgetown_ssr.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,31 @@ def bridgetown_site
1313
alias_method :site, :bridgetown_site
1414
end
1515

16-
def self.load_dependencies(app)
17-
app.plugin :custom_block_results
16+
def self.load_dependencies(app, opts = { sessions: false })
17+
app.plugin :all_verbs
18+
app.plugin :cookies, path: "/"
19+
app.plugin :indifferent_params
20+
app.plugin :method_override
21+
app.plugin :route_csrf
1822

1923
# This lets us return callable objects directly in Roda response blocks
24+
app.plugin :custom_block_results
2025
app.handle_block_result(Bridgetown::RodaCallable) do |callable|
2126
callable.(self)
2227
end
28+
29+
return unless opts[:sessions]
30+
31+
secret_key = ENV.fetch("RODA_SECRET_KEY", nil)
32+
unless secret_key
33+
raise Bridgetown::Errors::InvalidConfigurationError,
34+
"The Roda sessions plugin can't find a valid secret. Run " \
35+
"`bin/bridgetown secret' and put the key in your ENV as the " \
36+
"RODA_SECRET_KEY variable"
37+
end
38+
39+
app.plugin :sessions, secret: secret_key
40+
app.plugin :flashier
2341
end
2442

2543
def self.configure(app, _opts = {}, &)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# frozen_string_literal: true
2+
3+
class Roda
4+
module RodaPlugins
5+
module Flashier
6+
module FlashHashAdditions
7+
def info
8+
self["info"]
9+
end
10+
11+
def info=(val)
12+
self["info"] = val
13+
end
14+
15+
def alert
16+
self["alert"]
17+
end
18+
19+
def alert=(val)
20+
self["alert"] = val
21+
end
22+
end
23+
24+
module FlashHashIndifferent
25+
def []=(key, val)
26+
@next[key.to_s] = val
27+
end
28+
end
29+
30+
module FlashNowHashIndifferent
31+
def []=(key, val)
32+
super(key.to_s, val)
33+
end
34+
35+
def [](key)
36+
super(key.to_s)
37+
end
38+
end
39+
40+
def self.load_dependencies(app)
41+
require "roda/plugins/flash"
42+
43+
Roda::RodaPlugins::Flash::FlashHash.include FlashHashAdditions, FlashHashIndifferent
44+
Roda::RodaPlugins::Flash::FlashHash.class_eval do
45+
def initialize(hash = {})
46+
super(hash || {})
47+
now.singleton_class.include FlashHashAdditions, FlashNowHashIndifferent
48+
@next = {}
49+
end
50+
end
51+
app.plugin :flash
52+
end
53+
end
54+
55+
register_plugin :flashier, Flashier
56+
end
57+
end

bridgetown-core/lib/site_template/config/initializers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
#
6969
# init :ssr
7070
#
71+
# Add `sessions: true` if you need to use session data, flash, etc.
72+
#
7173

7274
# Uncomment to use file-based dynamic template routing via Roda (make sure you
7375
# uncomment the gem dependency in your `Gemfile` as well):

bridgetown-core/test/ssr/config/local_ssr_init.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
Bridgetown.initializer :local_ssr_init do |config|
44
config.init :ssr do
5+
sessions true
56
setup -> site do # rubocop:disable Layout/SpaceInLambdaLiteral, Style/StabbyLambdaParentheses
67
site.data.iterations ||= 0
78
site.data.iterations += 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
class Routes::Flashy < Bridgetown::Rack::Routes
4+
route do |r|
5+
# route: POST /flashy/:name
6+
r.post "flashy", String do |name|
7+
flash.info = "Save this value: #{name}"
8+
end
9+
10+
r.get "flashy" do
11+
{ saved: flash.info }
12+
end
13+
end
14+
end

bridgetown-core/test/test_ssr.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ class TestSSR < BridgetownUnitTest
88
include Rack::Test::Methods
99

1010
def app
11-
@@ssr_app ||= Rack::Builder.parse_file(File.expand_path("ssr/config.ru", __dir__)) # rubocop:disable Style/ClassVars
11+
@@ssr_app ||= begin # rubocop:disable Style/ClassVars
12+
ENV["RODA_SECRET_KEY"] = SecureRandom.hex(64)
13+
Rack::Builder.parse_file(File.expand_path("ssr/config.ru", __dir__))
14+
end
1215
end
1316

1417
def site
@@ -93,5 +96,13 @@ def site
9396
assert_equal "application/rss+xml", last_response["Content-Type"]
9497
assert_equal "<rss>WOW true</rss>", last_response.body
9598
end
99+
100+
should "return flash value" do
101+
post "/flashy/abc12356"
102+
103+
get "/flashy"
104+
105+
assert_equal({ "saved" => "Save this value: abc12356" }, JSON.parse(last_response.body))
106+
end
96107
end
97108
end

bridgetown-routes/lib/bridgetown-routes/flash_additions.rb

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)