Permalink
Browse files

improved Mojolicious::Plugin::Mount to automatically synchronize secrets

  • Loading branch information...
1 parent 5ed596e commit 7600cf5dd8293855c11dd294188f3b586a7f396c @kraih committed Jun 20, 2012
View
@@ -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
View
@@ -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.
@@ -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;
@@ -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;
@@ -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.
@@ -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";
@@ -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');
@@ -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;
@@ -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';
@@ -1 +1,5 @@
-{one => app->secret, two => $app->secret, works => "too!"};
+{
+ one => app->defaults('secret'),
+ two => $app->defaults->{secret},
+ works => "too!"
+};
@@ -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;

0 comments on commit 7600cf5

Please sign in to comment.