### Basic Application Setup with Amon2 Source: https://github.com/tokuhirom/amon/blob/master/README.md This snippet shows the minimal setup for a web application using Amon2, including loading configuration. ```perl package MyApp; use parent qw/Amon2/; use Amon2::Config::Simple; sub load_config { Amon2::Config::Simple->load(shift) } ``` -------------------------------- ### Getting the Application Base Directory Source: https://github.com/tokuhirom/amon/blob/master/README.md Retrieve the base directory of the Amon2 application. ```perl MyApp->base_dir() ``` -------------------------------- ### Getting the Context Object in Amon2 Source: https://github.com/tokuhirom/amon/blob/master/README.md Retrieve the current context object for your application. This is a fundamental operation for accessing application state. ```perl my $c = MyApp->context(); ``` -------------------------------- ### Bootstrapping the Application Context Source: https://github.com/tokuhirom/amon/blob/master/README.md Create a new context object and set it as the global context. This is particularly useful when setting up CLI scripts. ```perl my $c = MyApp->bootstrap(); ``` -------------------------------- ### Enabling Project Local Mode Source: https://github.com/tokuhirom/amon/blob/master/README.md How to enable the experimental project local mode in Amon2. ```APIDOC ## HOW DO I ENABLE PROJECT LOCAL MODE? `MyApp->make_local_context()` turns on the project local mode. There is no way to revert it, thanks. ``` -------------------------------- ### Accessing Application Configuration Source: https://github.com/tokuhirom/amon/blob/master/README.md Retrieve the application's configuration, which is typically loaded from a configuration file based on the current environment. ```perl MyApp->config() ``` -------------------------------- ### Creating Request with Project Local Context Source: https://github.com/tokuhirom/amon/blob/master/README.md When using project local mode, the `create_request` method needs to be passed the context class name to correctly instantiate the request object. ```perl sub create_request { my ($class, $env) = @_; Amon2::Web::Request->new($env, $class); } ``` -------------------------------- ### Project Local Mode Context Methods Source: https://github.com/tokuhirom/amon/blob/master/README.md Methods inserted into your context class when project local mode is enabled. ```APIDOC ## METHODS This module inserts 3 methods to your context class. - `MyApp->context()` Shorthand for $MyApp::CONTEXT - `MyApp->set_context($context)` It's the same as: $MyApp::CONTEXT = $context - `my $guard = MyApp->context_guard()` Create new context guard class. It's the same as: Amon2::ContextGuard->new(shift, \$MyApp::CONTEXT); ``` -------------------------------- ### Creating a New Context Object Source: https://github.com/tokuhirom/amon/blob/master/README.md Instantiate a new context object for the application. ```perl MyApp->new() ``` -------------------------------- ### Loading Multiple Plugins in Amon2 Source: https://github.com/tokuhirom/amon/blob/master/README.md Load multiple plugins simultaneously. Similar to loading a single plugin, plugins can be specified by their package name or from the application's namespace. ```perl __PACKAGE__->load_plugins("+MyApp::Plugin::Foo"); # => loads MyApp::Plugin::Foo ``` -------------------------------- ### Loading a Single Plugin in Amon2 Source: https://github.com/tokuhirom/amon/blob/master/README.md Load a single plugin for the application. Plugins can be specified by their package name, with an option to load from the application's own namespace. ```perl __PACKAGE__->load_plugin("Web::CSRFDefender"); # => loads Amon2::Plugin::Web::CSRFDefender ``` ```perl __PACKAGE__->load_plugin("+MyApp::Plugin::Foo"); # => loads MyApp::Plugin::Foo ``` -------------------------------- ### Accessing Project Local Context Source: https://github.com/tokuhirom/amon/blob/master/README.md Shorthand for accessing the project-local context variable `$MyApp::CONTEXT`. ```perl MyApp->context() ``` -------------------------------- ### Enabling Project Local Mode Source: https://github.com/tokuhirom/amon/blob/master/README.md This method enables project local mode, which makes the context project-local instead of global. There is no way to revert this change. ```perl MyApp->make_local_context() ``` -------------------------------- ### Setting Project Local Context Source: https://github.com/tokuhirom/amon/blob/master/README.md Directly assign a context object to the project-local context variable `$MyApp::CONTEXT`. ```perl $MyApp::CONTEXT = $context ``` -------------------------------- ### Creating a Context Guard for Project Local Mode Source: https://github.com/tokuhirom/amon/blob/master/README.md Create a new context guard object, which is used to manage the context in project local mode. It operates on the `$MyApp::CONTEXT` variable. ```perl my $guard = MyApp->context_guard() ``` ```perl Amon2::ContextGuard->new(shift, \$MyApp::CONTEXT); ``` -------------------------------- ### Customizing Configuration Loading Source: https://github.com/tokuhirom/amon/blob/master/README.md Override the default configuration loading mechanism, which typically loads from 'config/$ENV{PLACK_ENV}.pl'. ```perl MyApp->load_config() ``` -------------------------------- ### Default Mode Name Implementation Source: https://github.com/tokuhirom/amon/blob/master/README.md The default implementation for determining the application's mode name, which relies on the PLACK_ENV environment variable. ```perl sub mode_name { $ENV{PLACK_ENV} } ``` -------------------------------- ### Setting the Context Object (Internal Use) Source: https://github.com/tokuhirom/amon/blob/master/README.md This method is for internal use only and allows setting a custom context object. ```perl MyApp->set_context($c) ``` -------------------------------- ### Amon2 Class Methods Source: https://github.com/tokuhirom/amon/blob/master/README.md These are class methods available for the Amon2 class and its inherited classes. ```APIDOC ## CLASS METHODS for `Amon2` class - `MyApp->context()` Get the context object. - `MyApp->set_context($c)` Set your context object (INTERNAL USE ONLY). ## CLASS METHODS for inherited class - `MyApp->config()` This method returns configuration information. It is generated by `MyApp->load_config()`. - `MyApp->mode_name()` This is a mode name for Amon2. The default implementation of this method is: sub mode_name { $ENV{PLACK_ENV} } You can override this method if you want to determine the mode by other method. - `MyApp->new()` Create new context object. - `MyApp->bootstrap()` my $c = MyApp->bootstrap(); Create new context object and set it to global context. When you are writing CLI script, setup the global context object by this method. - `MyApp->base_dir()` This method returns the application base directory. - `MyApp->load_plugin($module_name[, %config])` This method loads the plugin for the application. _$module_name_ package name of the plugin. You can write it as two form like [DBIx::Class](https://metacpan.org/pod/DBIx%3A%3AClass): __PACKAGE__->load_plugin("Web::CSRFDefender"); # => loads Amon2::Plugin::Web::CSRFDefender If you want to load a plugin in your own name space, use the '+' character before a package name, like following: __PACKAGE__->load_plugin("+MyApp::Plugin::Foo"); # => loads MyApp::Plugin::Foo - `MyApp->load_plugins($module_name[, %config ], ...)` Load multiple plugins at one time. If you want to load a plugin in your own name space, use the '+' character before a package name like following: __PACKAGE__->load_plugins("+MyApp::Plugin::Foo"); # => loads MyApp::Plugin::Foo - `MyApp->load_config()` You can get a configuration hashref from `config/$ENV{PLACK_ENV}.pl`. You can override this method for customizing configuration loading method. - `MyApp->add_config()` DEPRECATED. - `MyApp->debug_mode()` **((EXPERIMENTAL))** This method returns a boolean value. It returns true when $ENV{AMON2_DEBUG} is true value, false otherwise. You can override this method if you need. ``` -------------------------------- ### Checking Debug Mode Status Source: https://github.com/tokuhirom/amon/blob/master/README.md Determine if the application is running in debug mode, which is controlled by the AMON2_DEBUG environment variable. This is an experimental feature. ```perl MyApp->debug_mode() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.