]>
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; | |
fdbf59e5 | 13 | use Gruntmaster::Page::Generic; |
7dc32473 MG |
14 | |
15 | my %handlers; | |
16 | ||
17 | sub call { | |
18 | my $env = $_[1]; | |
19 | my $r = Plack::Request->new($env); | |
20 | my @handlers = @{ $handlers{$r->method} // [] }; | |
21 | for my $handler (@handlers) { | |
22 | my ($re, $obj) = @$handler; | |
23 | my @args; | |
24 | next unless @args = $r->path =~ m/^$re$/a; | |
25 | my $format = choose $obj->variants, $r->headers; | |
191f4979 | 26 | return $obj->generate($format, $env, map { $_ // '' } @args); |
7dc32473 MG |
27 | } |
28 | ||
f34254b8 MG |
29 | if ($r->method eq 'GET' || $r->method eq 'HEAD') { |
30 | my $article = $r->path eq '/' ? '/index' : $r->path; | |
31 | $article = substr $article, 1; | |
32 | $article =~ tr,/,_,; | |
33 | my @variants = grep { !/\.title$/ } <a/$article.*>; | |
34 | if (@variants) { | |
35 | my $lang = choose [ map { [$_, 1, 'text/html', undef, undef, $_, undef] } map { /\.(.+)$/ } @variants ], $r->headers; | |
36 | my $content = read_file "a/$article.$lang"; | |
37 | my $title = read_file "a/$article.$lang.title"; | |
38 | my $html = Gruntmaster::Page::Base::header($lang, $title) . $content . Gruntmaster::Page::Base::footer($lang); | |
21d35251 | 39 | return [200, ['Content-Type' => 'text/html', 'Content-Language' => $lang, 'Vary' => 'Accept-Language', 'X-Forever' => 1, 'Cache-Control' => 'max-age=300'], [$html] ] |
f34254b8 MG |
40 | } |
41 | } | |
42 | ||
7dc32473 MG |
43 | [404, ['Content-Type' => 'text/plain'], ['Not found']] |
44 | } | |
45 | ||
46 | sub get { | |
47 | my ($re, $obj) = @_; | |
48 | eval "require Gruntmaster::Page::$obj" or die $@; | |
49 | push @{$handlers{GET }}, [ $re, "Gruntmaster::Page::$obj" ] | |
50 | } | |
51 | ||
52 | sub post { | |
53 | my ($re, $obj) = @_; | |
54 | eval "require Gruntmaster::Page::$obj" or die $@; | |
55 | push @{$handlers{POST}}, [ $re, "Gruntmaster::Page::$obj" ] | |
56 | } | |
57 | ||
58 | BEGIN{ | |
491e82eb MG |
59 | my $word = qr,(\w+),a; |
60 | my $number = qr,(\d+),a; | |
7dc32473 | 61 | |
81cce380 | 62 | sub generic { |
491e82eb MG |
63 | for my $thing (@_) { |
64 | my $pkg = ucfirst $thing; | |
65 | get qr,/$thing/, => $pkg; | |
66 | get qr,/$thing/read, => "${pkg}::Read"; | |
67 | get qr,/$thing/$word, => "${pkg}::Entry"; | |
68 | # post qr,/$thing/$word/create, => "${pkg}::Entry::Create"; | |
69 | get qr,/$thing/$word/read, => "${pkg}::Entry::Read"; | |
70 | # post qr,/$thing/$word/update, => "${pkg}::Entry::Update"; | |
71 | # post qr,/$thing/$word/delete, => "${pkg}::Entry::Delete"; | |
72 | } | |
81cce380 MG |
73 | } |
74 | ||
f06ba039 MG |
75 | get qr,/css/$word\.css, => 'CSS'; |
76 | get qr,/js\.js, => 'JS'; | |
7dc32473 | 77 | |
dfc00182 | 78 | get qr,/log/st, => 'St'; |
491e82eb | 79 | generic qw/us ct pb log/; |
7dc32473 | 80 | |
491e82eb | 81 | get qr,/log/src/$number\.$word, => 'Src'; |
a46fb222 | 82 | post qr,/submit, => 'Submit'; |
31d70015 MG |
83 | |
84 | post qr,/action/register, => 'Register'; | |
85 | post qr,/action/passwd, => 'Passwd'; | |
7dc32473 MG |
86 | } |
87 | ||
88 | 1; | |
89 | __END__ |