class StirFry::App
App is the root of your StirFry app that makes it runnable and ready for use on any rack server
Example:
class App < StirFry::App get "/greet/:name" do |response:, **| response.text("Hello #{name}") end get "/todo", Todos::List end
Public Class Methods
Source
# File lib/stir_fry/app.rb, line 81 def app_env = StirFry.app_env
running environment for this app.
# File lib/stir_fry/app.rb, line 78 def delete(pattern, klass = nil, &) = route("DELETE", pattern, klass, &)
Add a DELETE request route to the application
Source
# File lib/stir_fry/app.rb, line 84 def development? = app_env == :development
is the app running in development.
# File lib/stir_fry/app.rb, line 60 def get(pattern, klass = nil, &) = route("GET", pattern, klass, &)
Add a GET request route to the application
# File lib/stir_fry/app.rb, line 63 def head(pattern, klass = nil, &) = route("HEAD", pattern, klass, &)
Add a HEAD request route to the application
Source
# File lib/stir_fry/app.rb, line 227 def self.inherited(subclass) super subclass.class_eval do subclass.setup_default_middleware subclass.setup_framework_routes if subclass.development? end end
Hook into the app to define development routes and default middleware
Source
# File lib/stir_fry/app.rb, line 93 def logger = StirFry.logger
configured logger for the application for easy access in the app
# File lib/stir_fry/app.rb, line 66 def options(pattern, klass = nil, &) = route("OPTIONS", pattern, klass, &)
Add a OPTIONS request route to the application
# File lib/stir_fry/app.rb, line 75 def patch(pattern, klass = nil, &) = route("PATCH", pattern, klass, &)
Add a PATCH request route to the application
# File lib/stir_fry/app.rb, line 69 def post(pattern, klass = nil, &) = route("POST", pattern, klass, &)
Add a POST request route to the application
Source
# File lib/stir_fry/app.rb, line 90 def production? = app_end == :production
is the app running in production
# File lib/stir_fry/app.rb, line 72 def put(pattern, klass = nil, &) = route("PUT", pattern, klass, &)
Add a PUT request route to the application
Source
# File lib/stir_fry/app.rb, line 111 def rescue_from(klass, &block) = error_handlers[klass] = block
Add a handler for an error raised during runtime.
Example:
class App < StirFry::App rescue_from StirFry::NotFound do |response:, **| response.text("Not Found", :not_found) end end
# File lib/stir_fry/app.rb, line 166 def route(verb, pattern, klass, &block) path_pattern = Mustermann.new(route_prefix + pattern) route_table << [verb, path_pattern.to_s] unless path_pattern.to_s.start_with?("/stir_fry") (routes[verb] ||= []) << [path_pattern, route_handler(klass || block)] end
Raw route builder, used by the other helper models so get("/") becomes route("GET", "/")
# File lib/stir_fry/app.rb, line 156 def scope(prefix = "", &) context.push([prefix, []]) yield ensure context.pop end
Creates a sub-scope of routes that allow to prefix all the routes and add specific middleware that only applies to those routes.
Example:
class App < StirFry::App scope "/todos" do use AuthenticationMiddleware get "/:id", Todo delete "/:id", Todo post "/:id", Todo end end
Source
# File lib/stir_fry/app.rb, line 133 def static(dirname, path: "/") files = Rack::Files.new(File.expand_path(dirname)) path = route_prefix + path static_apps << (path == "/" ? files : Rack::URLMap.new(path => files)) end
Hosts a directory of static files. Every file within the directory will be requestable and sym links are followed so be careful what you host. A prefix can be added to the file routes to allow them to be scoped to a certain endpoint.
Example:
class App < StirFry::App static File.join(__dir__, "public") end
Source
# File lib/stir_fry/app.rb, line 87 def test? = app_env == :test
is the app running in test.
# File lib/stir_fry/app.rb, line 118 def use(middleware_class, *args, &block) = context.last[1] << [middleware_class, args, block]
Adds a middleware class to the chain of middleware. The order of operations is significant! So if you create routes before you add a middleware, the routes will not include the later middleware. This can be nice to scope your middleware but it can also be an easy mistake.
Source
# File lib/stir_fry/app.rb, line 48 def use_session(**config) raise "cannot enable sessions without a :secret set." if config[:secret].nil? secret_len = config[:secret].length if secret_len < 64 raise "session secret too short, secret is #{secret_len} but session secret is required to be >= 64" end use Rack::Session::Cookie, config end
sugar for use Rack::Session::Cookie, config See github.com/rack/rack-session for more config. Params: - domain host for the cookie security (example: โmywebsite.comโ) - path path for the cookie security (example: โ/โ) - expire_after mark the cookie as expired after seconds (example: 360024) - max_age similar to expires_after. - secret required* a random string 64 character long or longer. Used to encrypt cookie.
Public Instance Methods
Source
# File lib/stir_fry/app.rb, line 244 def app_env = self.class.app_env
running environment for this app.
Source
# File lib/stir_fry/app.rb, line 246 def development? = self.class.development?
is the app running in development.
Source
# File lib/stir_fry/app.rb, line 252 def logger = self.class.logger
configured logger for the application for easy access in the app
Source
# File lib/stir_fry/app.rb, line 250 def production? = self.class.production?
is the app running in production
Source
# File lib/stir_fry/app.rb, line 248 def test? = self.class.test?
is the app running in test.