Using Kelp configuration outside of the web app

13 views
Skip to first unread message

Brian

unread,
Oct 17, 2016, 3:27:21 AM10/17/16
to Perl Kelp
Is there a way to get access to kelp configuration outside the context of a web app?  Two scenarios:

In a "helper" module that provides some basic re-usable functions that web app code can use.  For example, to send email, the function may want access to the template config or template() function from Kelp.  However, it doesn't seem right to mix a bunch of route functions with helper functions in the same module, I'd rather pull the email stuff out.  But it's not clear how I can access config (without passing the config in each time).

In a script, such as a cron job or other utility script.  For example, a cron that goes through and deletes old session files, or old images uploaded by users, or whatever.  It would be nice to not have to maintain two separate config files (one for the Kelp web app and one for everything else), but it's not clear from the docs or looking at the Kelp::Module::Config module itself how I could do this.

Thank you,
Brian

Stefan Geneshky

unread,
Oct 17, 2016, 8:49:57 AM10/17/16
to perl...@googlegroups.com
Hi Brian,

Yes, it is possible:

Short answer:

Create a subclass of Kelp that disables the routes module, then use it in your utility scripts.

Longer answer:
​Kelp is a modular framework and its modules can be enabled or disabled using its configuration. There are only two modules that are always loaded - config and routes. You need to keep config but disable routes.

Create a subclass of Kelp that overrides the _load_routes method:

# Kelp/Blank.pm
package Kelp::Blank;
use base 'Kelp';

sub _load_routes {}

1;


Then use this class in your utility scripts, cron jobs, etc.

# bin/cron.pl
use Kelp::Blank;
my $k = Kelp::Blank->new( mode => 'cli' ); # Will load config/cli.pl
print $k->config('bar');
print $k->config('foo');


Also, take a look at this blog post to see how to disable modules using the config files:
http://blogs.perl.org/users/minimalist/2013/08/configuring-kelp-for-command-line-tools.html

You can disable all modules or keep some (like access to database).

I hope that helps.

Regards,
Stefan

Brian E. Lozier

unread,
Oct 17, 2016, 9:49:21 PM10/17/16
to perl...@googlegroups.com
Thanks, this helps.  I'll give it a try.
Reply all
Reply to author
Forward
0 new messages