Module: Sinatra::CloudflareWorkers
- Defined in:
- lib/sinatra/cloudflare_workers.rb
Class Method Summary collapse
- .app_has_routes?(klass) ⇒ Boolean
-
.ensure_rack_app! ⇒ Object
Auto-registration shim.
- .pick_classic_app ⇒ Object
- .pick_modular_app ⇒ Object
Class Method Details
.app_has_routes?(klass) ⇒ Boolean
75 76 77 78 79 80 81 82 |
# File 'lib/sinatra/cloudflare_workers.rb', line 75 def self.app_has_routes?(klass) routes = klass.routes return false unless routes.respond_to?(:each_value) routes.each_value do |entries| return true if entries.respond_to?(:any?) && entries.any? end false end |
.ensure_rack_app! ⇒ Object
Auto-registration shim. The user is allowed (and encouraged) to write the canonical Sinatra snippet from sinatrarb.com:
require 'sinatra'
get '/frank-says' do
'Put this in your pipe & smoke it!'
end
…without an explicit ‘run Sinatra::Application` line. Or the textbook modular form:
require 'sinatra/base'
class App < Sinatra::Base
get '/' do; 'hi'; end
end
…without ‘run App`. This runs at first-fetch time (lazy) — the Workers isolate never actually exits between requests, so the at_exit hook the previous version installed was unreliable. `Rack::Handler::CloudflareWorkers#call` calls back into here when `@app` is nil so we can pick the right Rack app.
Resolution order: a user-defined ‘App` (must be a `Sinatra::Base` subclass with at least one route) wins over the implicit `Sinatra::Application`. If neither has routes, nothing happens —this lets a worker entry-point that only exposes `scheduled` / `queue` / `email` handlers boot without an HTTP surface.
51 52 53 54 55 56 57 58 59 |
# File 'lib/sinatra/cloudflare_workers.rb', line 51 def self.ensure_rack_app! return unless defined?(::Rack::Handler::CloudflareWorkers) return ::Rack::Handler::CloudflareWorkers.app if ::Rack::Handler::CloudflareWorkers.app candidate = pick_modular_app || pick_classic_app return unless candidate ::Rack::Handler::CloudflareWorkers.run(candidate) end |
.pick_classic_app ⇒ Object
69 70 71 72 73 |
# File 'lib/sinatra/cloudflare_workers.rb', line 69 def self.pick_classic_app return nil unless defined?(::Sinatra::Application) return nil unless app_has_routes?(::Sinatra::Application) ::Sinatra::Application end |
.pick_modular_app ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/sinatra/cloudflare_workers.rb', line 61 def self.pick_modular_app return nil unless ::Object.const_defined?(:App, false) app = ::Object.const_get(:App) return nil unless app.is_a?(::Class) && app < ::Sinatra::Base return nil unless app_has_routes?(app) app end |