Author image Bartosz Jarzyna

NAME

Kelp::Module::Symbiosis::Base - Base class for symbiotic modules

SYNOPSIS

        package Kelp::Module::MyModule;

        use Kelp::Base qw(Kelp::Module::Symbiosis::Base);

        sub psgi
        {
                # write code that returns psgi application without middlewares
        }

        sub build
        {
                my ($self, %args) = @_;
                $self->SUPER::build(%args);

                # write initialization code as usual
                $self->register(some_method => sub { ... });
        }

DESCRIPTION

This class serves as a base for a Kelp module that is supposed to be ran as a standalone Plack application (mounted separately). It takes care of middleware management, mounting into Symbiosis manager and some basic initialization chores. To write a new module that introduces a standalone Plack application as a Kelp module, simply extend this class and override psgi and build methods.

Purpose

It is a base for Kelp modules that are meant to be used with Symbiosis - it inherits from Kelp::Module. It can also come very handy because of the built in middleware handling and access to Kelp application's configuration.

METHODS

run

        sig: run($self)

Calls psgi() and wraps its contents in middlewares. Returns a Plack application.

psgi

        sig: psgi($self, @more_data)

By default, this method will throw an exception. It has to be replaced with an actual application producing code in the child class. The resulting application will be wrapped in middlewares from config in run().

build

        sig: build($self, %args)

Standard Kelp module building method. When reimplementing it's best to call parent's implementation, as middleware initialization happens in base implementation.

middleware

        sig: middleware($self)

Returns an array containing all the middlewares in format: [ middleware_class, { middleware_config } ]. By default, this config comes from module configuration.

CONFIGURATION

example configuration could look like this (for Kelp::Module::WebSocket::AnyEvent):

        modules => [qw/JSON Symbiosis WebSocket::AnyEvent/],
        modules_init => {
                Symbiosis => {
                        automount => 0, # kelp will be mounted manually under different path
                },
                "WebSocket::AnyEvent" => {
                        serializer => "json",
                        middleware => [qw/Recorder/],
                        middleware_init => {
                                Recorder => { output => "~/recorder.out" },
                        }
                },
        }

middleware, middleware_init

Middleware specs for this application. Every module basing on this class can specify its own set of middlewares. They are configured exactly the same as middlewares in Kelp. There's currently no standarized way to retrieve middleware configurations from Kelp into another application, so custom code is needed if such need arise.

SEE ALSO