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
Constants
- Route
-
Routedefinition struct to define the structure of data needed for a route.
Public Class Methods
Source
# File lib/stir_fry/app.rb, line 87 def app_env = StirFry.app_env
running environment for this app.
# File lib/stir_fry/app.rb, line 84 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 90 def development? = app_env == :development
is the app running in development.
# File lib/stir_fry/app.rb, line 66 def get(pattern, klass = nil, ...) = route("GET", pattern, klass, ...)
Add a GET request route to the application
# File lib/stir_fry/app.rb, line 69 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 99 def logger = StirFry.logger
configured logger for the application for easy access in the app
# File lib/stir_fry/app.rb, line 72 def options(pattern, klass = nil, ...) = route("OPTIONS", pattern, klass, ...)
Add a OPTIONS request route to the application
# File lib/stir_fry/app.rb, line 81 def patch(pattern, klass = nil, ...) = route("PATCH", pattern, klass, ...)
Add a PATCH request route to the application
# File lib/stir_fry/app.rb, line 75 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 96 def production? = app_end == :production
is the app running in production
# File lib/stir_fry/app.rb, line 78 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 117 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 178 def route(verb, pattern, klass, **options, &block) path = (route_prefix + pattern).sub(%r{(^.+)/$}, "\\1") options ||= {} route_table << Route.new( verb:, path:, pattern: Mustermann.new(path), handler: route_handler(klass || block), **options.slice(:cooked, :description) ) (routes[verb] ||= []) << route_table.last end
Raw route builder, used by the other helper models so get("/") becomes route("GET", "/")
# File lib/stir_fry/app.rb, line 168 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
# File lib/stir_fry/app.rb, line 139 def static(dirname, path: "/", **options) options ||= {} files = Rack::Files.new(File.expand_path(dirname)) path = route_prefix + path route_table << Route.new( verb: "STATIC", path: "#{path}:asset_path", **options.slice(:cooked, :description) ) 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 93 def test? = app_env == :test
is the app running in test.
# File lib/stir_fry/app.rb, line 124 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 54 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 268 def app_env = self.class.app_env
running environment for this app.
Source
# File lib/stir_fry/app.rb, line 270 def development? = self.class.development?
is the app running in development.
Source
# File lib/stir_fry/app.rb, line 276 def logger = self.class.logger
configured logger for the application for easy access in the app
Source
# File lib/stir_fry/app.rb, line 274 def production? = self.class.production?
is the app running in production
Source
# File lib/stir_fry/app.rb, line 272 def test? = self.class.test?
is the app running in test.