| 1 | package PAGI::App::URLMap; | ||
| 2 | $PAGI::App::URLMap::VERSION = '0.002001'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Future::AsyncAwait; | ||
| 6 | use PAGI::Utils (); | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | PAGI::App::URLMap - Mount apps at URL path prefixes | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use PAGI::App::URLMap; | ||
| 15 | |||
| 16 | my $map = PAGI::App::URLMap->new; | ||
| 17 | $map->mount('/api' => $api_app); | ||
| 18 | $map->mount('/static' => PAGI::App::File->new(root => $dir)); | ||
| 19 | my $app = $map->to_app; | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | sub new { | ||
| 24 | my ($class, %args) = @_; | ||
| 25 | |||
| 26 | return bless { | ||
| 27 | mounts => [], | ||
| 28 | default => defined $args{default} ? PAGI::Utils::to_app($args{default}) : undef, | ||
| 29 | }, $class; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub mount { | ||
| 33 | my ($self, $path, $app) = @_; | ||
| 34 | |||
| 35 | $path =~ s{/+$}{}; # Remove trailing slashes | ||
| 36 | push @{$self->{mounts}}, [$path, PAGI::Utils::to_app($app)]; | ||
| 37 | # Keep sorted by length (longest first) for proper matching | ||
| 38 | @{$self->{mounts}} = sort { length($b->[0]) <=> length($a->[0]) } @{$self->{mounts}}; | ||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub map { | ||
| 43 | my ($self, $mapping) = @_; | ||
| 44 | |||
| 45 | while (my ($path, $app) = each %$mapping) { | ||
| 46 | $self->mount($path, $app); | ||
| 47 | } | ||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub to_app { | ||
| 52 | my ($self) = @_; | ||
| 53 | |||
| 54 | my @mounts = @{$self->{mounts}}; | ||
| 55 | my $default = $self->{default}; | ||
| 56 | |||
| 57 | return async sub { | ||
| 58 | my ($scope, $receive, $send) = @_; | ||
| 59 | my $path = $scope->{path} // '/'; | ||
| 60 | |||
| 61 | for my $mount (@mounts) { | ||
| 62 | my ($prefix, $app) = @$mount; | ||
| 63 | |||
| 64 | if ($prefix eq '' || $path eq $prefix || $path =~ /^\Q$prefix\E\//) { | ||
| 65 | # Match found - adjust path for mounted app | ||
| 66 | my $new_path = $path; | ||
| 67 | $new_path =~ s/^\Q$prefix\E//; | ||
| 68 | $new_path = '/' if $new_path eq ''; | ||
| 69 | |||
| 70 | my $new_scope = { | ||
| 71 | %$scope, | ||
| 72 | path => $new_path, | ||
| 73 | root_path => ($scope->{root_path} // '') . $prefix, | ||
| 74 | }; | ||
| 75 | |||
| 76 | await $app->($new_scope, $receive, $send); | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | # No match - use default or 404 | ||
| 82 | if ($default) { | ||
| 83 | await $default->($scope, $receive, $send); | ||
| 84 | } else { | ||
| 85 | await $send->({ | ||
| 86 | type => 'http.response.start', | ||
| 87 | status => 404, | ||
| 88 | headers => [['content-type', 'text/plain']], | ||
| 89 | }); | ||
| 90 | await $send->({ type => 'http.response.body', body => 'Not Found', more => 0 }); | ||
| 91 | } | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | 1; | ||
| 96 | |||
| 97 | __END__ |