Amon2 Web Application Framework

repository·master·Indexed 41 days ago

https://github.com/tokuhirom/amon

A lightweight, fast, and stable web application framework for Perl based on Plack. Amon2 supports PSGI and provides a plugin system for extensibility, structured application context management, and flexible configuration loading via Amon2::Config::Simple.

Tokens
823
Snippets
4
Records
7
Agent score
1%

What's inside Amon2

  1. How Project Local Mode works

    master

    Concept

    Normally, Amon2 stores the application context in a global variable ($Amon2::CONTEXT). In Project Local Mode, the context is moved to a project-specific variable ($MyApp::CONTEXT).

    Warning: This mode is highly experimental.

    Implications

    • You cannot use code that depends on <Amon2-context'>> or <Amon2-context'>>` while in this mode.
    • If you are overriding create_request in Amon2::Web::Request, you must pass the class name of your context class as the second argument to ensure it uses the local context instead of the global one.

    Enabling Project Local Mode

    Call MyApp->make_local_context() to enable this mode. Note that there is no way to revert this setting once enabled.

    sub create_request {
        my ($class, $env) = @;
        # Required for Project Local Mode
        Amon2::Web::Request->new($env, $class);
    }
  2. Initialize an Amon2 application

    master

    To create a new web application using Amon2, define a package that inherits from Amon2. You typically use Amon2::Config::Simple to handle configuration loading.

    package MyApp;
    use parent qw/Amon2/;
    use Amon2::Config::Simple;
    
    # Custom configuration loader
    sub load_config { Amon2::Config::Simple->load(shift) }
    package MyApp;
    use parent qw/Amon2/;
    use Amon2::Config::Simple;
    sub load_config { Amon2::Config::Simple->load(shift) }
  3. Overview of Amon2 web application framework

    master
    Amon2 is a lightweight, thin-layer web application framework. It is designed to be minimal and supports the PSGI (Perl Web Server Gateway Interface) and Plack interfaces, making it compatible with standard Perl web server environments.
  4. Bootstrap the application context

    master

    When writing CLI scripts or manual entry points, use bootstrap() to create a new context object and set it as the global context for your application.

    my $c = MyApp->bootstrap();
    my $c = MyApp->bootstrap();
  5. Manage context in your application

    master

    Amon2 inserts several methods into your application class to facilitate context management:

    • MyApp->context(): A shorthand to access the current context object (equivalent to $MyApp::CONTEXT).
    • MyApp->set_context($context): Sets the current context object.
    • MyApp->context_guard(): Creates a new context guard class (equivalent to Amon2::ContextGuard->new(shift, \$MyApp::CONTEXT)).
  6. Load plugins into your application

    master

    Amon2 allows you to load plugins using load_plugin (single) or load_plugins (multiple).

    • Standard loading: If you provide a package name like Web::CSRFDefender, Amon2 automatically prefixes it with Amon2::Plugin:: (e.g., Amon2::Plugin::Web::CSRFDefender).
    • Namespace loading: To load a plugin within your own application's namespace, prefix the package name with a + character.
    # Loads Amon2::Plugin::Web::CSRFDefender
    __PACKAGE__->load_plugin("Web::CSRFDefender");
    
    # Loads MyApp::Plugin::Foo
    __PACKAGE__->load_plugin("+MyApp::Plugin::Foo");
    __PACKAGE__->load_plugin("Web::CSRFDefender");
    __PACKAGE__->load_plugin("+MyApp::Plugin::Foo");
  7. Access application configuration

    master
    Configuration is retrieved via MyApp->config(). By default, this returns a hashref generated by MyApp->load_config(), which looks for a file at config/$ENV{PLACK_ENV}.pl. You can override load_config in your application class to customize how configuration is loaded.