1 | package Mojolicious::Plugin::Mount;
|
---|
2 | use Mojo::Base 'Mojolicious::Plugin';
|
---|
3 |
|
---|
4 | use Mojo::Server;
|
---|
5 |
|
---|
6 | sub register {
|
---|
7 | my ($self, $app, $conf) = @_;
|
---|
8 |
|
---|
9 | my $path = (keys %$conf)[0];
|
---|
10 | my $embed = Mojo::Server->new->load_app($conf->{$path});
|
---|
11 | $embed->secrets($app->secrets);
|
---|
12 |
|
---|
13 | # Extract host
|
---|
14 | my $host;
|
---|
15 | ($host, $path) = ($1 ? qr/^(?:.*\.)?\Q$2\E$/i : qr/^\Q$2\E$/i, $3)
|
---|
16 | if $path =~ m!^(\*\.)?([^/]+)(/.*)?$!;
|
---|
17 |
|
---|
18 | my $route = $app->routes->route($path)->detour(app => $embed);
|
---|
19 | return $host ? $route->over(host => $host) : $route;
|
---|
20 | }
|
---|
21 |
|
---|
22 | 1;
|
---|
23 |
|
---|
24 | =encoding utf8
|
---|
25 |
|
---|
26 | =head1 NAME
|
---|
27 |
|
---|
28 | Mojolicious::Plugin::Mount - Application mount plugin
|
---|
29 |
|
---|
30 | =head1 SYNOPSIS
|
---|
31 |
|
---|
32 | # Mojolicious
|
---|
33 | my $route = $app->plugin(Mount => {'/prefix' => '/home/sri/myapp.pl'});
|
---|
34 |
|
---|
35 | # Mojolicious::Lite
|
---|
36 | my $route = plugin Mount => {'/prefix' => '/home/sri/myapp.pl'};
|
---|
37 |
|
---|
38 | # Adjust the generated route and mounted application
|
---|
39 | my $example = plugin Mount => {'/example' => '/home/sri/example.pl'};
|
---|
40 | $example->to(message => 'It works great!');
|
---|
41 | my $app = $example->pattern->defaults->{app};
|
---|
42 | $app->config(foo => 'bar');
|
---|
43 | $app->log(app->log);
|
---|
44 |
|
---|
45 | # Mount application with host
|
---|
46 | plugin Mount => {'example.com' => '/home/sri/myapp.pl'};
|
---|
47 |
|
---|
48 | # Host and path
|
---|
49 | plugin Mount => {'example.com/myapp' => '/home/sri/myapp.pl'};
|
---|
50 |
|
---|
51 | # Or even hosts with wildcard subdomains
|
---|
52 | plugin Mount => {'*.example.com/myapp' => '/home/sri/myapp.pl'};
|
---|
53 |
|
---|
54 | =head1 DESCRIPTION
|
---|
55 |
|
---|
56 | L<Mojolicious::Plugin::Mount> is a plugin that allows you to mount whole
|
---|
57 | L<Mojolicious> applications.
|
---|
58 |
|
---|
59 | The code of this plugin is a good example for learning to build new plugins,
|
---|
60 | you're welcome to fork it.
|
---|
61 |
|
---|
62 | See L<Mojolicious::Plugins/"PLUGINS"> for a list of plugins that are available
|
---|
63 | by default.
|
---|
64 |
|
---|
65 | =head1 METHODS
|
---|
66 |
|
---|
67 | L<Mojolicious::Plugin::Mount> inherits all methods from L<Mojolicious::Plugin>
|
---|
68 | and implements the following new ones.
|
---|
69 |
|
---|
70 | =head2 register
|
---|
71 |
|
---|
72 | my $route = $plugin->register(Mojolicious->new, {'/foo' => '/some/app.pl'});
|
---|
73 |
|
---|
74 | Mount L<Mojolicious> application and return the generated route, which is
|
---|
75 | usually a L<Mojolicious::Routes::Route> object.
|
---|
76 |
|
---|
77 | =head1 SEE ALSO
|
---|
78 |
|
---|
79 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
80 |
|
---|
81 | =cut
|
---|