Class: Syntropy::App
- Inherits:
-
Object
- Object
- Syntropy::App
- Defined in:
- lib/syntropy/app.rb
Overview
The App implements a Syntropy application. It is responsible for handling incoming HTTP requests, routing them to the correct handler, and maintaining application state.
Instance Attribute Summary collapse
-
#app_root ⇒ Object
readonly
Returns the value of attribute app_root.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#module_loader ⇒ Object
readonly
Returns the value of attribute module_loader.
-
#mount_path ⇒ Object
readonly
Returns the value of attribute mount_path.
-
#raise_on_internal_server_error ⇒ Object
Returns the value of attribute raise_on_internal_server_error.
-
#routing_tree ⇒ Object
readonly
Returns the value of attribute routing_tree.
Class Method Summary collapse
-
.load(env) ⇒ Syntropy::App
Creates an app instance based on the given environment hash.
Instance Method Summary collapse
-
#call(req) ⇒ void
Processes an incoming HTTP request.
-
#initialize(**env) ⇒ void
constructor
Initializes the app instance.
-
#route(path, params = {}, compute_proc: false) ⇒ Hash
Returns the route entry for the given path.
Constructor Details
#initialize(**env) ⇒ void
Initializes the app instance.
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/syntropy/app.rb', line 57 def initialize(**env) @machine = env[:machine] @app_root = File.(env[:app_root]) @mount_path = env[:mount_path] @env = env @logger = env[:logger] @module_loader = Syntropy::ModuleLoader.new(app: self, **env) setup_routing_tree start end |
Instance Attribute Details
#app_root ⇒ Object (readonly)
Returns the value of attribute app_root.
50 51 52 |
# File 'lib/syntropy/app.rb', line 50 def app_root @app_root end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
50 51 52 |
# File 'lib/syntropy/app.rb', line 50 def env @env end |
#module_loader ⇒ Object (readonly)
Returns the value of attribute module_loader.
50 51 52 |
# File 'lib/syntropy/app.rb', line 50 def module_loader @module_loader end |
#mount_path ⇒ Object (readonly)
Returns the value of attribute mount_path.
50 51 52 |
# File 'lib/syntropy/app.rb', line 50 def mount_path @mount_path end |
#raise_on_internal_server_error ⇒ Object
Returns the value of attribute raise_on_internal_server_error.
51 52 53 |
# File 'lib/syntropy/app.rb', line 51 def raise_on_internal_server_error @raise_on_internal_server_error end |
#routing_tree ⇒ Object (readonly)
Returns the value of attribute routing_tree.
50 51 52 |
# File 'lib/syntropy/app.rb', line 50 def routing_tree @routing_tree end |
Class Method Details
.load(env) ⇒ Syntropy::App
Creates an app instance based on the given environment hash.
23 24 25 |
# File 'lib/syntropy/app.rb', line 23 def load(env) site_file_app(env) || default_app(env) end |
Instance Method Details
#call(req) ⇒ void
This method returns an undefined value.
Processes an incoming HTTP request. Requests are processed by first looking up the route for the request path, then calling the route proc. If the route proc is not set, it is computed according to the route target, and composed recursively into hooks encountered up the routing tree.
Normal exceptions (StandardError and descendants) are trapped and passed to route’s error handler. If no such handler is found, the default error handler is used, which simply generates a textual response containing the error message, and with the appropriate HTTP status code, according to the type of error.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/syntropy/app.rb', line 82 def call(req) path = req.path route = @router_proc.(path, req.route_params) if !route if (m = path.match(/^(.+)\/$/)) return req.redirect(m[1], HTTP::MOVED_PERMANENTLY) else return handle_not_found(req) end end req.route = route proc = route[:proc] ||= compute_route_proc(route) proc.(req) rescue ScriptError, StandardError => e if Error.log_error?(e) @logger&.error( message: "Error while serving request: #{e.}", method: req.method, path: path, error: e ) end error_handler = get_error_handler(route) error_handler.(req, e) end |
#route(path, params = {}, compute_proc: false) ⇒ Hash
Returns the route entry for the given path. If compute_proc is true, computes the route proc if not yet computed.
116 117 118 119 120 121 122 |
# File 'lib/syntropy/app.rb', line 116 def route(path, params = {}, compute_proc: false) route = @router_proc.(path, params) return if !route route[:proc] ||= compute_route_proc(route) if compute_proc route end |