Fix some bugs
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
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
10 use File::Slurp qw/read_file/;
11 use HTTP::Negotiate qw/choose/;
12 use Plack::Request;
13 use Gruntmaster::Page::Generic;
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;
26 return $obj->generate($format, $env, map { $_ // '' } @args);
27 }
28
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);
39 return [200, ['Content-Type' => 'text/html', 'Content-Language' => $lang, 'Vary' => 'Accept-Language', 'X-Forever' => 1, 'Cache-Control' => 'max-age=300'], [$html] ]
40 }
41 }
42
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{
59 my $word = qr,(\w+),a;
60 my $number = qr,(\d+),a;
61
62 sub generic {
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 }
73 }
74
75 get qr,/css/$word\.css, => 'CSS';
76 get qr,/js\.js, => 'JS';
77
78 get qr,/log/st, => 'St';
79 generic qw/us ct pb log/;
80
81 get qr,/log/src/$number\.$word, => 'Src';
82 post qr,/submit, => 'Submit';
83
84 post qr,/action/register, => 'Register';
85 post qr,/action/passwd, => 'Passwd';
86 }
87
88 1;
89 __END__
This page took 0.027312 seconds and 4 git commands to generate.