]>
Commit | Line | Data |
---|---|---|
7dc32473 MG |
1 | package Plack::App::Gruntmaster; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Plack::Component/; | |
7 | no if $] >= 5.017011, warnings => 'experimental::smartmatch'; | |
8 | our $VERSION = '5999.000_001'; | |
9 | ||
f34254b8 | 10 | use File::Slurp qw/read_file/; |
7dc32473 MG |
11 | use HTTP::Negotiate qw/choose/; |
12 | use Plack::Request; | |
13 | ||
14 | my %handlers; | |
15 | ||
16 | sub call { | |
17 | my $env = $_[1]; | |
18 | my $r = Plack::Request->new($env); | |
19 | my @handlers = @{ $handlers{$r->method} // [] }; | |
20 | for my $handler (@handlers) { | |
21 | my ($re, $obj) = @$handler; | |
22 | my @args; | |
23 | next unless @args = $r->path =~ m/^$re$/a; | |
24 | my $format = choose $obj->variants, $r->headers; | |
191f4979 | 25 | return $obj->generate($format, $env, map { $_ // '' } @args); |
7dc32473 MG |
26 | } |
27 | ||
f34254b8 MG |
28 | if ($r->method eq 'GET' || $r->method eq 'HEAD') { |
29 | my $article = $r->path eq '/' ? '/index' : $r->path; | |
30 | $article = substr $article, 1; | |
31 | $article =~ tr,/,_,; | |
32 | my @variants = grep { !/\.title$/ } <a/$article.*>; | |
33 | if (@variants) { | |
34 | my $lang = choose [ map { [$_, 1, 'text/html', undef, undef, $_, undef] } map { /\.(.+)$/ } @variants ], $r->headers; | |
35 | my $content = read_file "a/$article.$lang"; | |
36 | my $title = read_file "a/$article.$lang.title"; | |
37 | my $html = Gruntmaster::Page::Base::header($lang, $title) . $content . Gruntmaster::Page::Base::footer($lang); | |
21d35251 | 38 | return [200, ['Content-Type' => 'text/html', 'Content-Language' => $lang, 'Vary' => 'Accept-Language', 'X-Forever' => 1, 'Cache-Control' => 'max-age=300'], [$html] ] |
f34254b8 MG |
39 | } |
40 | } | |
41 | ||
7dc32473 MG |
42 | [404, ['Content-Type' => 'text/plain'], ['Not found']] |
43 | } | |
44 | ||
45 | sub get { | |
46 | my ($re, $obj) = @_; | |
47 | eval "require Gruntmaster::Page::$obj" or die $@; | |
48 | push @{$handlers{GET }}, [ $re, "Gruntmaster::Page::$obj" ] | |
49 | } | |
50 | ||
51 | sub post { | |
52 | my ($re, $obj) = @_; | |
53 | eval "require Gruntmaster::Page::$obj" or die $@; | |
54 | push @{$handlers{POST}}, [ $re, "Gruntmaster::Page::$obj" ] | |
55 | } | |
56 | ||
57 | BEGIN{ | |
58 | my $word = qr,(\w+),a; | |
59 | my $ct = qr,(?:\/ct/$word)?,a; | |
60 | ||
61 | get qr,/css/$word, => 'CSS'; | |
62 | get qr,/js, => 'JS'; | |
63 | ||
64 | get qr,/ct/, => 'Ct'; | |
3a70241a | 65 | get qr,/ct/$word/, => 'Ct::Entry'; |
7dc32473 MG |
66 | get qr,/us/, => 'Us'; |
67 | get qr,/us/$word, => 'Us::Entry'; | |
68 | ||
69 | get qr,$ct/log/(\d+)?, => 'Log'; | |
70 | get qr,$ct/log/st, => 'St'; | |
71 | get qr,$ct/log/job/$word, => 'Log::Entry'; | |
923655e1 | 72 | get qr,$ct/log/src/$word\.$word, => 'Src'; |
7dc32473 MG |
73 | get qr,$ct/pb/, => 'Pb'; |
74 | get qr,$ct/pb/$word, => 'Pb::Entry'; | |
8d29b3b1 | 75 | post qr,$ct/pb/$word/submit, => 'Submit'; |
31d70015 MG |
76 | |
77 | post qr,/action/register, => 'Register'; | |
78 | post qr,/action/passwd, => 'Passwd'; | |
7dc32473 MG |
79 | } |
80 | ||
81 | 1; | |
82 | __END__ |