Kelp::Module::Symbiosis::Base - Base class for symbiotic modules
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 { ... }); }
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 methods: psgi build name (see below for details).
psgi build name
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.
sig: run($self)
Calls psgi() and wraps its contents in middlewares. Returns a Plack application.
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().
Must be reimplemented in a module.
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.
Should be reimplemented in a module. If it isn't, no extra methods will be added to the Kelp instance, but all the middleware and module registration in Symbiosis will happen anyway.
sig: name($self)
new in 1.10
Returns a name of a module - a string. This name will be available in "loaded" in Kelp::Module::Symbiosis hash as a key, containing the module instance as a value.
Should be reimplemented in a module. If it isn't, it will return the name of the package.
sig: middleware($self)
Returns an array containing all the middlewares in format: [ middleware_class, { middleware_config } ]. By default, this config comes from module configuration.
[ middleware_class, { middleware_config } ]
example configuration could look like this (for Kelp::Module::WebSocket::AnyEvent):
modules => [qw/JSON Symbiosis WebSocket::AnyEvent/], modules_init => { Symbiosis => { mount => undef, # kelp will be mounted manually under different path }, "WebSocket::AnyEvent" => { serializer => "json", middleware => [qw/Recorder/], middleware_init => { Recorder => { output => "~/recorder.out" }, } }, }
Middleware specs for this application - see above example. 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 (to wrap that application in the same middleware as Kelp), so custom code is needed if such need arise.
modules_init => { "Symbiotic::Module" => { mount => '/path', ... }, }
Should be a string value. If specified, the module will be automatically mounted under that path - there will be no need to call that explicitly, and it will work like: $kelp->symbiosis->mount($path => $module);.
$kelp->symbiosis->mount($path => $module);
Kelp::Module::Symbiosis, the module manager
To install Kelp::Module::Symbiosis, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Kelp::Module::Symbiosis
CPAN shell
perl -MCPAN -e shell install Kelp::Module::Symbiosis
For more information on module installation, please visit the detailed CPAN module installation guide.