Newer Older
77 lines | 1.817kb
add files
Yuki Kimoto authored on 2014-03-26
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

            
12
  # Extract host
13
  my $host;
14
  if ($path =~ m!^(\*\.)?([^/]+)(/.*)?$!) {
15
    $host = $1 ? qr/^(?:.*\.)?\Q$2\E$/i : qr/^\Q$2\E$/i;
16
    $path = $3;
17
  }
18

            
19
  my $route = $app->routes->route($path)->detour(app => $embed);
20
  $route->over(host => $host) if $host;
21

            
22
  return $route;
23
}
24

            
25
1;
26

            
27
=encoding utf8
28

            
29
=head1 NAME
30

            
31
Mojolicious::Plugin::Mount - Application mount plugin
32

            
33
=head1 SYNOPSIS
34

            
35
  # Mojolicious
36
  my $route = $self->plugin(Mount => {'/prefix' => '/home/sri/myapp.pl'});
37

            
38
  # Mojolicious::Lite
39
  my $route = plugin Mount => {'/prefix' => '/home/sri/myapp.pl'};
40

            
41
  # Adjust the generated route
42
  my $example = plugin Mount => {'/example' => '/home/sri/example.pl'};
43
  $example->to(message => 'It works great!');
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
=head1 METHODS
63

            
64
L<Mojolicious::Plugin::Mount> inherits all methods from L<Mojolicious::Plugin>
65
and implements the following new ones.
66

            
67
=head2 register
68

            
69
  my $route = $plugin->register(Mojolicious->new, {'/foo' => '/some/app.pl'});
70

            
71
Mount L<Mojolicious> application.
72

            
73
=head1 SEE ALSO
74

            
75
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
76

            
77
=cut