Module: Bridgetown::Rack

Defined in:
bridgetown-core/lib/bridgetown-core/rack/boot.rb,
bridgetown-core/lib/bridgetown-core/rack/logger.rb,
bridgetown-core/lib/bridgetown-core/rack/routes.rb

Defined Under Namespace

Classes: Logger, Routes

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.loaders_managerBridgetown::Utils::LoadersManager



16
17
18
# File 'bridgetown-core/lib/bridgetown-core/rack/boot.rb', line 16

def loaders_manager
  @loaders_manager
end

Class Method Details

.autoload_server_folder(root: Bridgetown::Current.preloaded_configuration.root_dir) ⇒ Object

Parameters:

  • root (String) (defaults to: Bridgetown::Current.preloaded_configuration.root_dir)

    root of Bridgetown site, defaults to config value



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
# File 'bridgetown-core/lib/bridgetown-core/rack/boot.rb', line 38

def self.autoload_server_folder( # rubocop:todo Metrics
  root: Bridgetown::Current.preloaded_configuration.root_dir
)
  server_folder = File.join(root, "server")
  cached_reload_file = Bridgetown.live_reload_path

  Bridgetown::Hooks.register_one(
    :loader, :post_setup, reloadable: false
  ) do |loader, load_path|
    next unless load_path == server_folder

    loader.eager_load
    loader.do_not_eager_load(File.join(server_folder, "roda_app.rb"))

    unless ENV["BRIDGETOWN_ENV"] == "production"
      Listen.to(server_folder) do |modified, added, removed|
        c = modified + added + removed
        n = c.length

        Bridgetown.logger.info(
          "Reloading…",
          "#{n} file#{"s" if n > 1} changed at #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
        )
        c.each do |path|
          Bridgetown.logger.info "", "- #{path["#{File.dirname(server_folder)}/".length..]}"
        end

        loader.reload
        Bridgetown::Hooks.trigger :loader, :post_reload, loader, server_folder
      rescue SyntaxError => e
        Bridgetown::Errors.print_build_error(e)
      end.start
    end
  end

  Bridgetown::Hooks.register_one(
    :loader, :post_reload, reloadable: false
  ) do |loader, load_path|
    next unless load_path == server_folder

    loader.eager_load
    Bridgetown.touch_live_reload_file(cached_reload_file)
  end

  loaders_manager.setup_loaders([server_folder])
end

.bootObject

Start up the Roda Rack application and the Zeitwerk autoloaders. Ensure the Roda app is provided the preloaded Bridgetown site configuration. Handle any uncaught Roda errors.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'bridgetown-core/lib/bridgetown-core/rack/boot.rb', line 22

def self.boot(*)
  self.loaders_manager =
    Bridgetown::Utils::LoadersManager.new(Bridgetown::Current.preloaded_configuration)
  Bridgetown::Current.preloaded_configuration.run_initializers! context: :server
  autoload_server_folder
rescue Roda::RodaError => e
  if e.message.include?("sessions plugin :secret option")
    raise Bridgetown::Errors::InvalidConfigurationError,
          "The Roda sessions plugin can't find a valid secret. Run `bin/bridgetown secret' " \
          "and put the key in a ENV var you can use to configure the session in the Roda app"
  end

  raise e
end