1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#!/usr/bin/env perl
use Mojolicious::Lite;
# Default for config file tests
app->defaults(secret => 'Insecure!');
# Helpers sharing the same name in different embedded applications
helper same_name => sub {'myapp'};
# Load plugin
plugin 'Config';
# Message condition
app->routes->add_condition(
message => sub {
my ($route, $c, $captures, $msg) = @_;
$c->res->headers->header('X-Message' => $msg);
return 1;
}
);
get '/' => 'index';
get '/echo' => sub {
my $c = shift;
$c->render(text => 'echo: ' . ($c->stash('message') || 'nothing!'));
};
get '/stream' => sub {
shift->write_chunk(
'he' => sub {
shift->write_chunk('ll' => sub { shift->finish('o!') });
}
);
};
get '/url/☃' => sub {
my $c = shift;
my $route = $c->url_for({format => 'json'});
my $rel = $c->url_for('/☃/stream');
$c->render(text => "$route -> $rel!");
};
get '/host' => (message => 'it works!') => sub {
my $c = shift;
$c->render(text => $c->url_for->base->host);
};
get '/one' => sub { shift->render(text => 'One') };
get '/one/two' => {text => 'Two'};
get '/template/:template';
app->start;
__DATA__
@@ menubar.html.ep
%= same_name
<%= $config->{just} %><%= $config->{one} %><%= $config->{two} %>
|