Re-usable Kelp apps

82 views
Skip to first unread message

Brad

unread,
Jul 23, 2014, 4:45:49 AM7/23/14
to perl...@googlegroups.com
Hi guys,

I'm into rapid development, and I love Kelp so far. I'd like to combine the two. For example, with Catalyst, you can use CatalystX::AppBuilder to create a "base" web app. Then, you can re-use all of it in a new app so you don't have to re-write everything. I'd like to try and do the same with Kelp.. I've created by base app with a lot of custom stuff that I'd like to extend into new apps. Does any one know if this can be done?

Cheers!

Stefan Geneshky

unread,
Jul 23, 2014, 10:58:13 AM7/23/14
to perl...@googlegroups.com
Hi Brad,

Unfortunately, Kelp does not come with an app builder and I am not aware of any implementations available on CPAN. It certainly is possible and can be done. If you end up writing KelpX::AppBuilder or similar, please do share it here.

Regards,
Stefan

Brad

unread,
Jul 23, 2014, 12:46:11 PM7/23/14
to perl...@googlegroups.com
Thanks for the reply Stefan. I'll definitely be working on it, because I'd like to work with Kelp more, and being able to reuse an app would be extremely beneficial!

Cheers,
Brad 

Brad

unread,
Jul 25, 2014, 5:09:36 AM7/25/14
to perl...@googlegroups.com
Hello again,

I seem to be having trouble adding the route map from the base app you want to extend. I was able to use inherited paths to have it use the views/assets no problem, though. I've added some helper methods in KelpX::AppBuilder to do this easily. If you have any ideas at all please let me know, because I'm at a bit of a loss.

Cheers,
Brad

Brad

unread,
Jul 25, 2014, 7:50:33 AM7/25/14
to perl...@googlegroups.com
Is it possible to add an external module to route to? Kelp tries to append the running module name to my controller which I'm not really wanting to do.. ie:

$r->add('/', 'BaseApp::Controller::Root::index');

Kelp would try to look for 'MyApp::BaseApp::Controller::Root::index'. Normally you can append a '+' to override this behaviour, but it doesn't seem to work for routes :(

Brad

unread,
Jul 25, 2014, 8:51:25 AM7/25/14
to perl...@googlegroups.com
Sorry about the message spam! 

I've figured out a way to do it for now, simply by adding a method in your base application with the map routes. It's not great, but it works. It still requires a bit of fiddling, but I was able to get my new app working with the base app functionality inside it, which is cool.
The repo can be found here https://github.com/bradhaywood/KelpX-AppBuilder
I wrote the module in about 15 minutes, so it might not be great.
Would be nice to hear if anyone gets along with it OK. Open to suggestions!

Cheers,

Brad

unread,
Jul 25, 2014, 8:52:03 AM7/25/14
to perl...@googlegroups.com
There's no POD at the moment either, but I've added a README with some pretty simple instructions (hopefully)

Brad

unread,
Jul 25, 2014, 5:19:22 PM7/25/14
to perl...@googlegroups.com
Update! I've really fixed the base app now. There's no need to create the build function and maps. Just create the maps then

   use KelpX::AppBuilder 'Base';

at the top of your app. It will create the build function based on your maps data, so there's no need to duplicate it.

Stefan Geneshky

unread,
Jul 25, 2014, 6:57:51 PM7/25/14
to perl...@googlegroups.com
Brad,

Great! It looks like a good start. Consider putting it on CPAN once you have it more polished.

Stefan

Brad

unread,
Jul 26, 2014, 5:31:46 AM7/26/14
to perl...@googlegroups.com
Thanks!

I've just updated it this morning so there's no need to manually load the controllers. KelpX::AppBuilder will handle that when you call the constructor now, so in your testapp, you can

sub build {
   
my ($self) = @_;
   
my $r      = $self->routes;
   
KelpX::AppBuilder->new('BaseApp')->add_maps($r);

   $r
->add('/local', sub { "Hello world!" });
}

Stefan Geneshky

unread,
Jul 26, 2014, 1:04:29 PM7/26/14
to perl...@googlegroups.com
Cool.
Btw, I just added a patch to allow the use of routes that live outside of the base class by prefixing them with a plus sign.
It should go in with the next CPAN release.

Stefan

Brad

unread,
Jul 27, 2014, 9:26:33 AM7/27/14
to perl...@googlegroups.com
Sounds great. I could have used that a few days ago. Haha.
I'm about ready to clean things up in KelpX::AppBuilder and add some proper POD. I'd like to try and work something out for the config first, so there's no need to copy the base app config across. Also, the assets thing is bugging me. With Plack::Middleware::Static, it says you can just define the root twice, but doesn't seem to work with Kelp, so will need to find a way around it.
I've just added the ability to auto-create an 'auto' method in the Root controller. This is handy for things like ensuring a user is logged in before accessing a page. The auto function should get called before every thing else (just like Catalyst). To do this,  just add 'auto' => 1 in your maps definition.

sub maps{
   
{
       
auto => 1,
       
'/'  =>  BaseApp::Controller::Root->can('index'),
       
# etc, etc
   
}
}
 

Then in your BaseApp::Controller::Root you can add your logic

sub auto {
    my ($self) = @_;
    my $url    = $self->named->{page};
    unless ($url eq 'login') {
        if (my $user = $self->user) {
            return 1;
        }

        return;
    }

    return 1;
}

Brad

unread,
Jul 28, 2014, 10:11:27 AM7/28/14
to perl...@googlegroups.com
Hey guys,

I've finally started getting KelpX::AppBuilder into a more CPAN friendly module. I haven't done this in a while, so if you could let me know how it's looking, and if/what I need to change/add I'd really appreciate it :)

Thanks in advance!

Stefan Geneshky

unread,
Jul 28, 2014, 3:06:40 PM7/28/14
to perl...@googlegroups.com
Brad,

I have not used or tested your module, but from the looks of it, it seems ready for CPAN. Here are a few tips:

  1. Move README to README.md. It will look better on Github. The easiest way to do this is using Pod::Markdown::Github


Stefan Geneshky

unread,
Jul 28, 2014, 3:10:41 PM7/28/14
to perl...@googlegroups.com
... pressed the wrong key and gmail sent the above incomplete. Here is it again in full:

  1. Move README to README.md. It will look better on Github. The easiest way to do this is using Pod::Markdown::Github:
    perl -MPod::Markdown::Github -e "Pod::Markdown::Github->filter('lib/KelpX/AppBuilder.pm')" > README.md
  2. Use Dist::Zilla to publish to CPAN. It might seem a little complicated at first, but it pays to learn and configure it.
  3. Use App::cpanminus to install Perl modules
Regards,
Stefan

Brad

unread,
Jul 29, 2014, 8:08:33 AM7/29/14
to perl...@googlegroups.com
Thanks for the tip on Github formatting. It looks amazing!
I'll look into dzilla at some point, but don't have a lot of time spare currently. And, oh, KelpX::AppBuilder is on CPAN now :D

Cheers,
Brad

Brad

unread,
Jul 29, 2014, 8:56:34 AM7/29/14
to perl...@googlegroups.com
In order to try and keep everything within AppBuilder, I've moved the File::ShareDir stuff for the configuration into the module itself. So your config should just have this at the top

use KelpX::AppBuilder Config => 'BaseApp';

It'll then generate a method called "base_path" that returns the 'auto' directory of your base app.
Message has been deleted

Brad

unread,
Apr 22, 2016, 6:53:16 AM4/22/16
to Perl Kelp
I've just realised I was doing things kind of backwards with this module, so I've done a little overhaul to it and re-uploaded - https://github.com/bradhaywood/KelpX-AppBuilder
It's now easier than before to create reusable Kelp apps and patch them into your main one. 


Reply all
Reply to author
Forward
0 new messages