| 1 | package PGXN::Site::Router; | ||
| 2 | |||
| 3 | use 5.10.0; | ||
| 4 | use utf8; | ||
| 5 | use PGXN::Site::Controller; | ||
| 6 | use Router::Resource; | ||
| 7 | use Plack::Builder; | ||
| 8 | use Plack::App::File; | ||
| 9 | our $VERSION = v0.22.1; | ||
| 10 | |||
| 11 | sub app { | ||
| 12 | my $class = shift; | ||
| 13 | my %p = @_; | ||
| 14 | my $controller = PGXN::Site::Controller->new(@_); | ||
| 15 | (my $ui = __FILE__) =~ s{Router[.]pm$}{ui}; | ||
| 16 | my $files = Plack::App::File->new(root => $ui)->to_app; | ||
| 17 | my $router = router { | ||
| 18 | missing { $controller->missing(@_) }; | ||
| 19 | |||
| 20 | # / | ||
| 21 | resource qr{^/(?:index[.]html)?$} => sub { | ||
| 22 | GET { $controller->home(@_) } | ||
| 23 | }; | ||
| 24 | |||
| 25 | # /search | ||
| 26 | resource qr{^/search$} => sub { | ||
| 27 | GET { | ||
| 28 | $controller->search(shift); | ||
| 29 | } | ||
| 30 | }; | ||
| 31 | |||
| 32 | # /dist/{dist} | ||
| 33 | # /dist/{dist}/{version} | ||
| 34 | resource qr{/dist/([^/]+)(?:/(\d[^/]+))?/?$} => sub { | ||
| 35 | GET { | ||
| 36 | my ($env, $args) = @_; | ||
| 37 | $controller->distribution($env, @{ $args->{splat} } ); | ||
| 38 | }; | ||
| 39 | }; | ||
| 40 | |||
| 41 | # /dist/{dist}/{path} | ||
| 42 | # /dist/{dist}/{version}/{path} | ||
| 43 | resource qr{/dist/([^/]+)(?:/(\d[^/]+))?/(.+)[.]html$} => sub { | ||
| 44 | GET { | ||
| 45 | my ($env, $args) = @_; | ||
| 46 | $controller->document($env, @{ $args->{splat} } ); | ||
| 47 | }; | ||
| 48 | }; | ||
| 49 | |||
| 50 | # /user/{user}/ | ||
| 51 | resource qr{/user/([^/]+)/?$} => sub { | ||
| 52 | GET { | ||
| 53 | my ($env, $args) = @_; | ||
| 54 | $controller->user($env, @{ $args->{splat} } ); | ||
| 55 | }; | ||
| 56 | }; | ||
| 57 | |||
| 58 | # /tags | ||
| 59 | resource qr{^/tags/?$} => sub { | ||
| 60 | GET { $controller->tags(shift) }; | ||
| 61 | }; | ||
| 62 | |||
| 63 | # /tag/{tag}/ | ||
| 64 | resource qr{/tag/([^/]+)/?$} => sub { | ||
| 65 | GET { | ||
| 66 | my ($env, $args) = @_; | ||
| 67 | $controller->tag($env, @{ $args->{splat} } ); | ||
| 68 | }; | ||
| 69 | }; | ||
| 70 | |||
| 71 | # /extension/{extension}/ | ||
| 72 | resource qr{/extension/([^/]+)/?$} => sub { | ||
| 73 | GET { | ||
| 74 | my ($env, $args) = @_; | ||
| 75 | $controller->extension($env, @{ $args->{splat} } ); | ||
| 76 | }; | ||
| 77 | }; | ||
| 78 | |||
| 79 | # /feedback | ||
| 80 | resource qr{^/feedback/?$} => sub { | ||
| 81 | GET { $controller->feedback(shift) }; | ||
| 82 | }; | ||
| 83 | |||
| 84 | # /art | ||
| 85 | resource qr{^/art/?$} => sub { | ||
| 86 | GET { $controller->art(shift) }; | ||
| 87 | }; | ||
| 88 | |||
| 89 | # /about | ||
| 90 | resource qr{^/about/?$} => sub { | ||
| 91 | GET { $controller->about(shift) }; | ||
| 92 | }; | ||
| 93 | |||
| 94 | # /users/ | ||
| 95 | resource qr{/users/?$} => sub { | ||
| 96 | GET { $controller->users(shift) }; | ||
| 97 | }; | ||
| 98 | |||
| 99 | # /recent | ||
| 100 | resource qr{^/recent/?$} => sub { | ||
| 101 | GET { $controller->recent(shift) }; | ||
| 102 | }; | ||
| 103 | |||
| 104 | # /donors | ||
| 105 | resource qr{^/donors/?$} => sub { | ||
| 106 | GET { $controller->donors(shift) }; | ||
| 107 | }; | ||
| 108 | |||
| 109 | # /faq | ||
| 110 | resource qr{^/faq/?$} => sub { | ||
| 111 | GET { $controller->faq(shift) }; | ||
| 112 | }; | ||
| 113 | |||
| 114 | # /mirroring | ||
| 115 | resource qr{^/mirroring/?$} => sub { | ||
| 116 | GET { $controller->mirroring(shift) }; | ||
| 117 | }; | ||
| 118 | |||
| 119 | # /meta/spec.txt. | ||
| 120 | resource '/meta/spec.txt' => sub { | ||
| 121 | GET { $controller->spec(shift, 'txt') }; | ||
| 122 | }; | ||
| 123 | |||
| 124 | # /spec | ||
| 125 | resource qr{^/spec/?$} => sub { | ||
| 126 | GET { $controller->spec(shift, 'html') }; | ||
| 127 | }; | ||
| 128 | |||
| 129 | # /error (500 error responder). | ||
| 130 | resource '/error' => sub { | ||
| 131 | GET { $controller->server_error(@_) }; | ||
| 132 | }; | ||
| 133 | |||
| 134 | # Handle legacy URLs. | ||
| 135 | my %url_for = ( | ||
| 136 | contact => '/feedback/', | ||
| 137 | contributors => '/donors/', | ||
| 138 | mirroring => '/mirroring/', | ||
| 139 | faq => '/faq/', | ||
| 140 | 'meta/spec' => '/spec/', | ||
| 141 | ); | ||
| 142 | |||
| 143 | resource qr{^/(cont(?:ributors|act)|mirroring|faq|meta/spec)[.]html$} => sub { | ||
| 144 | GET { | ||
| 145 | my ($env, $args) = @_; | ||
| 146 | $controller->redirect($url_for{ $args->{splat}[0] }, 301) | ||
| 147 | }; | ||
| 148 | }; | ||
| 149 | }; | ||
| 150 | |||
| 151 | builder { | ||
| 152 | enable 'ErrorDocument', 500, '/error', subrequest => 1; | ||
| 153 | enable 'HTTPExceptions'; | ||
| 154 | enable 'StackTrace', no_print_errors => 1; | ||
| 155 | enable 'ReverseProxy' if $p{reverse_proxy}; | ||
| 156 | mount '/' => builder { sub { $router->dispatch(shift) } }; | ||
| 157 | mount '/ui' => $files; | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; | ||
| 162 | |||
| 163 | =head1 Name | ||
| 164 | |||
| 165 | PGXN::Site::Router - The PGXN::Site request router. | ||
| 166 | |||
| 167 | =head1 Synopsis | ||
| 168 | |||
| 169 | # In app.pgsi | ||
| 170 | use PGXN::Site::Router; | ||
| 171 | PGXN::Site::Router->app; | ||
| 172 | |||
| 173 | =head1 Description | ||
| 174 | |||
| 175 | This class defines the HTTP request routing table used by PGXN::Site. Unless | ||
| 176 | you're modifying the PGXN::Site routes and controllers, you won't have to | ||
| 177 | worry about it. Just know that this is the class that Plack uses to fire up | ||
| 178 | the app. | ||
| 179 | |||
| 180 | =head1 Interface | ||
| 181 | |||
| 182 | =head2 Class Methods | ||
| 183 | |||
| 184 | =head3 C<app> | ||
| 185 | |||
| 186 | PGXN::Site->app; | ||
| 187 | |||
| 188 | Returns the PGXN::Site Plack app. See L<pgxn_site_server> for an example | ||
| 189 | usage. It's not much to look at. But Plack uses the returned code reference to | ||
| 190 | power the application. | ||
| 191 | |||
| 192 | =head1 Author | ||
| 193 | |||
| 194 | David E. Wheeler <david@justatheory.com> | ||
| 195 | |||
| 196 | =head1 Copyright and License | ||
| 197 | |||
| 198 | Copyright (c) 2010-2021 David E. Wheeler. | ||
| 199 | |||
| 200 | This module is free software; you can redistribute it and/or modify it under | ||
| 201 | the L<PostgreSQL License|https://www.opensource.org/licenses/postgresql>. | ||
| 202 | |||
| 203 | Permission to use, copy, modify, and distribute this software and its | ||
| 204 | documentation for any purpose, without fee, and without a written agreement is | ||
| 205 | hereby granted, provided that the above copyright notice and this paragraph | ||
| 206 | and the following two paragraphs appear in all copies. | ||
| 207 | |||
| 208 | In no event shall David E. Wheeler be liable to any party for direct, | ||
| 209 | indirect, special, incidental, or consequential damages, including lost | ||
| 210 | profits, arising out of the use of this software and its documentation, even | ||
| 211 | if David E. Wheeler has been advised of the possibility of such damage. | ||
| 212 | |||
| 213 | David E. Wheeler specifically disclaims any warranties, including, but not | ||
| 214 | limited to, the implied warranties of merchantability and fitness for a | ||
| 215 | particular purpose. The software provided hereunder is on an "as is" basis, | ||
| 216 | and David E. Wheeler has no obligations to provide maintenance, support, | ||
| 217 | updates, enhancements, or modifications. | ||
| 218 | |||
| 219 | =cut |