]> iEval git - gruntmaster-page.git/blame - lib/Plack/App/Gruntmaster.pm
Import some changes from the mindcoding branch
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
CommitLineData
7dc32473
MG
1package Plack::App::Gruntmaster;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Plack::Component/;
7no if $] >= 5.017011, warnings => 'experimental::smartmatch';
8our $VERSION = '5999.000_001';
9
f34254b8 10use File::Slurp qw/read_file/;
7dc32473
MG
11use HTTP::Negotiate qw/choose/;
12use Plack::Request;
fdbf59e5 13use Gruntmaster::Page::Generic;
7dc32473
MG
14
15my %handlers;
16
17sub 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
46sub get {
47 my ($re, $obj) = @_;
48 eval "require Gruntmaster::Page::$obj" or die $@;
49 push @{$handlers{GET }}, [ $re, "Gruntmaster::Page::$obj" ]
50}
51
52sub post {
53 my ($re, $obj) = @_;
54 eval "require Gruntmaster::Page::$obj" or die $@;
55 push @{$handlers{POST}}, [ $re, "Gruntmaster::Page::$obj" ]
56}
57
58BEGIN{
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
881;
89__END__
This page took 0.048209 seconds and 4 git commands to generate.