Module: PumaPlus::AppLoader

Defined in:
lib/puma_plus/app_loader.rb

Overview

Loading a Rack app from a path.

Extracted from Worker so the thread-backed and Ractor-backed workers load apps identically -- if they diverged, a benchmark comparing them would be comparing app loading as much as concurrency.

Defined Under Namespace

Modules: MiniBuilder

Class Method Summary collapse

Class Method Details

.load(app_path) ⇒ Object

Load a Rack app from a .ru or a plain .rb.

The .ru path uses Rack::Builder when the rack gem is available, and falls back to a minimal builder otherwise. The spike must be runnable before gem install rack, and a Rack app is only an object responding to #call.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/puma_plus/app_loader.rb', line 17

def load(app_path)
  source = File.read(app_path)

  if app_path.end_with?(".ru")
    begin
      require "rack"
      return Rack::Builder.new_from_string(source, app_path)
    rescue LoadError
      return MiniBuilder.build(source, app_path)
    end
  end

  require File.expand_path(app_path)
  unless defined?(::App) && ::App.respond_to?(:call)
    raise "#{app_path} must define an App constant responding to #call"
  end

  ::App
end