]> iEval git - gruntmaster-page.git/blame - lib/Plack/App/Gruntmaster.pm
Make Ct::Entry URLs end with a slash
[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;
13
14my %handlers;
15
16sub 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
45sub get {
46 my ($re, $obj) = @_;
47 eval "require Gruntmaster::Page::$obj" or die $@;
48 push @{$handlers{GET }}, [ $re, "Gruntmaster::Page::$obj" ]
49}
50
51sub post {
52 my ($re, $obj) = @_;
53 eval "require Gruntmaster::Page::$obj" or die $@;
54 push @{$handlers{POST}}, [ $re, "Gruntmaster::Page::$obj" ]
55}
56
57BEGIN{
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
811;
82__END__
This page took 0.032407 seconds and 4 git commands to generate.