NAME

Thunderhorse::App - Base application class for Thunderhorse

SYNOPSIS

package MyApp;

use v5.40;
use Mooish::Base;

extends 'Thunderhorse::App';

sub build ($self)
{
	$self->router->add('/hello' => { to => 'greet' });
}

sub greet ($self, $ctx)
{
	return "Hello, World!";
}

MyApp->new->run;

DESCRIPTION

Thunderhorse::App is the base application class for Thunderhorse applications. It extends Gears::App and provides all core functionality needed to build PAGI web applications, including routing, configuration management, controller loading, and request handling.

Every Thunderhorse application must be a subclass of this class. Method "build" in Gears::Component can be overridden to bootstrap the application. Similar method "configure" in Gears::Component is called before build, but is used internally by the application, so the SUPER version must be called if it is overridden. Method late_configure works like configure but is executed after build. This must also call the SUPER version if overridden, since it is used to load controllers from configuration.

INTERFACE

Inherits all interface from Gears::App and Gears::Component, and adds the interface documented below.

Attributes

path

Application's base path. Defaults to the path to application starter script, like app.pl.

Available in the constructor

env

Application environment. Can be production, development, or test. Defaults to PAGI_ENV environmental variable, or production.

Available in the constructor

initial_config

Initial configuration, either a hash reference or a string path to configuration directory. Defaults to an empty hash.

The configuration directory path will be appended to "path" before reading the configuration.

Available in the constructor

loop

IO::Async::Loop instance for quick access.

Not available in constructor

controller

Base application controller instance.

Not available in constructor

Methods

new

$object = $class->new(%args)

Standard Mooish constructor. Consult "Attributes" section for available constructor arguments.

run

$pagi_app = $app->run()

Returns a PAGI application coderef ready for pagi-server. If called from a thunderhorse script (THUNDERHORSE_SCRIPT environmental value is set), returns the app object instead.

pagi

$self->pagi($scope, $receive, $send)

Main PAGI application handler. This method is used in "run", so there is no need to call it manually.

load_module

$self = $self->load_module($name, $config = {})

Loads and initializes a Thunderhorse module. Module name is automatically prefixed with Thunderhorse::Module:: unless it starts with ^.

load_controller

$self = $self->load_controller($name)

Loads a controller class and registers its routes. Controller name is automatically prefixed with application namespace unless it starts with ^.

add_method

$self = $self->add_method($for, $name, $code)

Adds a method with a $name dynamically. $for specifies the target area. Used by modules to extend functionality.

Allowed values for $for are: controller

add_middleware

$self = $self->add_middleware($middleware)

Wraps the entire application in PAGI middleware. $middleware can be a coderef or a PAGI::Middleware instance.

add_hook

$self = $self->add_hook($hook, $handler)

Registers a hook handler for application events.

Allowed values for $hook are: startup, shutdown, error.

is_production

$bool = $self->is_production()

Returns true if the application is running in production environment.

render_error

$self->render_error($controller, $ctx, $code, $message = undef)

Renders an error response with the given HTTP status code. Can be overridden to customize error pages.

render_response

$self->render_response($controller, $ctx, $result)

Renders a response from $result, which contains what was returned by the handler. Can be overridden to change the default behavior of rendering result as HTML.

on_startup

async sub on_startup ($self, $state) { ... }

Lifespan hook called when the worker process starts. Can be overridden to perform initialization tasks. $state is a PAGI persistent hash reference with state values.

on_shutdown

async sub on_shutdown ($self, $state) { ... }

Lifespan hook called when the worker process shuts down. Can be overridden to perform cleanup tasks. $state is a PAGI persistent hash reference with state values.

on_error

async sub on_error ($self, $controller, $ctx, $error) { ... }

Error hook called when an exception occurs during request processing. Can be overridden to customize error handling. Overriding it on application level will change the default handler for all controllers.

SEE ALSO

Thunderhorse, Gears::App, Thunderhorse::Controller