Sha256: a6aed018be455fbac0fe574467e3bc83c3bb279a094745e8b65abd7574ee2ecf

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require "zeitwerk"
require "roda"
require "json"
require "roda/plugins/public"

Bridgetown::Current.preloaded_configuration ||= Bridgetown.configuration

require_relative "logger"
require_relative "roda"
require_relative "routes"
require_relative "static_indexes"

module Bridgetown
  module Rack
    def self.boot
      autoload_server_folder(root: Dir.pwd)
      RodaApp.opts[:bridgetown_preloaded_config] = Bridgetown::Current.preloaded_configuration
    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 `roda_app.rb'"
      end

      raise e
    end

    def self.autoload_server_folder(root:)
      server_folder = File.join(root, "server")
      loader = Zeitwerk::Loader.new
      loader.push_dir server_folder
      loader.enable_reloading unless ENV["BRIDGETOWN_ENV"] == "production"
      loader.setup
      loader.eager_load
      loader.do_not_eager_load(File.join(server_folder, "roda_app.rb"))

      unless ENV["BRIDGETOWN_ENV"] == "production"
        begin
          Listen.to(server_folder) do |_modified, _added, _removed|
            loader.reload
            loader.eager_load
            Bridgetown::Rack::Routes.reload_subclasses
          end.start
        # interrupt isn't handled well by the listener
        rescue ThreadError # rubocop:disable Lint/SuppressedException
        end
      end
    rescue Zeitwerk::Error
      # We assume if there's an error it's because Zeitwerk already registered this folder,
      # so it's fine to swallow the error
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bridgetown-core-1.0.0.alpha6 lib/bridgetown-core/rack/boot.rb
bridgetown-core-1.0.0.alpha5 lib/bridgetown-core/rack/boot.rb
bridgetown-core-1.0.0.alpha4 lib/bridgetown-core/rack/boot.rb