Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved Mojolicious::Plugin::Mount to automatically synchronize secrets
  • Loading branch information
kraih committed Jun 20, 2012
1 parent 5ed596e commit 7600cf5
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 17 deletions.
1 change: 0 additions & 1 deletion .perltidyrc
@@ -1,5 +1,4 @@
-pbp # Start with Perl Best Practices
-b # Just rewrite the file
-w # Show all warnings
-iob # Ignore old breakpoints
-l=79 # 79 characters per line
Expand Down
2 changes: 2 additions & 0 deletions Changes
Expand Up @@ -10,6 +10,8 @@
- Added j and r functions to ojo. (sharifulin, sri)
- Added idle_interval attribute to Mojo::IOLoop.
- Added support for new HTTP status code.
- Modernized ".perltidyrc".
- Improved Mojolicious::Plugin::Mount to automatically synchronize secrets.
- Improved to method in Mojolicious::Routes::Route to give easier access to
default parameters.
- Improved message parser performance slightly.
Expand Down
13 changes: 4 additions & 9 deletions lib/Mojolicious/Plugin/Mount.pm
Expand Up @@ -9,8 +9,8 @@ sub register {
my ($self, $app, $conf) = @_;

# Load application
my $path = (keys %$conf)[0];
my $embed = Mojo::Server->new->load_app($conf->{$path});
my $path = (keys %$conf)[0];
my $e = Mojo::Server->new->load_app($conf->{$path})->secret($app->secret);

# Extract host
my $host;
Expand All @@ -20,7 +20,7 @@ sub register {
}

# Generate route
my $route = $app->routes->route($path)->detour(app => $embed);
my $route = $app->routes->route($path)->detour(app => $e);
$route->over(host => $host) if $host;

return $route;
Expand Down Expand Up @@ -56,12 +56,7 @@ Mojolicious::Plugin::Mount - Application mount plugin
=head1 DESCRIPTION
L<Mojolicious::Plugin::Mount> is a plugin that allows you to mount whole
L<Mojolicious> applications. Note that secrets need to be synchronized if
sessions or signed cookies are being used.
# Synchronize secrets between applications
plugin(Mount => {'/foo' => '/home/sri/myapp.pl'})->to->{app}
->secret(app->secret);
L<Mojolicious> applications.
The code of this plugin is a good example for learning to build new plugins,
you're welcome to fork it.
Expand Down
26 changes: 25 additions & 1 deletion t/mojolicious/embedded_app.t
Expand Up @@ -9,12 +9,15 @@ BEGIN {
$ENV{MOJO_REACTOR} = 'Mojo::Reactor::Poll';
}

use Test::More tests => 39;
use Test::More tests => 54;

# "I wax my rocket every day!"
use Mojolicious::Lite;
use Test::Mojo;

# Custom secret
app->secret('very secr3t!');

# Mount full external application a few times
use FindBin;
my $external = "$FindBin::Bin/external/script/my_app";
Expand All @@ -26,11 +29,32 @@ plugin(Mount => ('*.foo-bar.de/♥/123' => $external));
# GET /hello
get '/hello' => 'works';

# GET /primary
get '/primary' => sub {
my $self = shift;
$self->render(text => ++$self->session->{primary});
};

my $t = Test::Mojo->new;

# GET /hello
$t->get_ok('/hello')->status_is(200)->content_is("Hello from the main app!\n");

# GET /primary (session)
$t->get_ok('/primary')->status_is(200)->content_is(1);

# GET /primary (session again)
$t->get_ok('/primary')->status_is(200)->content_is(2);

# GET /x/1/secondary (session in external app)
$t->get_ok('/x/1/secondary')->status_is(200)->content_is(1);

# GET /primary (session again)
$t->get_ok('/primary')->status_is(200)->content_is(3);

# GET /x/1/secondary (session in external app again)
$t->get_ok('/x/1/secondary')->status_is(200)->content_is(2);

# GET /x/1 (external app)
$t->get_ok('/x/1')->status_is(200)->content_is('too%21');

Expand Down
8 changes: 8 additions & 0 deletions t/mojolicious/external/lib/MyApp.pm
Expand Up @@ -25,6 +25,14 @@ sub startup {
$self->render(text => $self->config->{whatever});
}
);

# GET /secondary
$r->get(
'/secondary' => sub {
my $self = shift;
$self->render(text => ++$self->session->{secondary});
}
);
}

1;
4 changes: 2 additions & 2 deletions t/mojolicious/external/myapp.pl
Expand Up @@ -5,8 +5,8 @@
# "Boy, who knew a cooler could also make a handy wang coffin?"
use Mojolicious::Lite;

# Secret for config file tests
app->secret('Insecure!');
# Default for config file tests
app->defaults(secret => 'Insecure!');

# Load plugin
plugin 'Config';
Expand Down
6 changes: 5 additions & 1 deletion t/mojolicious/external/myapp.testing.conf
@@ -1 +1,5 @@
{one => app->secret, two => $app->secret, works => "too!"};
{
one => app->defaults('secret'),
two => $app->defaults->{secret},
works => "too!"
};
7 changes: 4 additions & 3 deletions t/mojolicious/external/myapp2.pl
Expand Up @@ -5,13 +5,14 @@
# And by "metaphorically", I mean get your coat."
use Mojolicious::Lite;

# Secret for config file tests
app->secret('Insecure too!');
# Default for config file tests
app->defaults(secret => 'Insecure too!');

# GET /
get '/' => sub {
my $self = shift;
$self->render_text($self->render_partial('menubar') . app->secret);
$self->render_text(
$self->render_partial('menubar') . app->defaults->{secret});
};

app->start;
Expand Down

0 comments on commit 7600cf5

Please sign in to comment.