99ce4987c9e24b387f95487464f8752b70d385a6
[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 $ct = qr,(?:\/ct/$word)?,a;
61
62 sub generic {
63 my ($thing, $ct, $fs) = @_;
64 $ct //= '', $fs //= '';
65 my $pkg = ucfirst $thing;
66 get qr,$ct/$thing/, => $pkg;
67 get qr,$ct/$thing/read, => "${pkg}::Read";
68 get qr,$ct/$thing/$word$fs, => "${pkg}::Entry";
69 # post qr,$ct/$thing/$word/create, => "${pkg}::Entry::Create";
70 get qr,$ct/$thing/$word/read, => "${pkg}::Entry::Read";
71 # post qr,$ct/$thing/$word/update, => "${pkg}::Entry::Update";
72 # post qr,$ct/$thing/$word/delete, => "${pkg}::Entry::Delete";
73 }
74
75 get qr,/css/$word\.css, => 'CSS';
76 get qr,/js\.js, => 'JS';
77
78 generic 'us';
79 generic ct => '', '/';
80 generic pb => $ct;
81 #generic log => $ct;
82
83 get qr,$ct/log/(\d+)?, => 'Log';
84 get qr,$ct/log/st, => 'St';
85 get qr,$ct/log/job/$word, => 'Log::Entry';
86 get qr,$ct/log/job/$word/read, => 'Log::Entry::Read';
87 get qr,$ct/log/src/$word\.$word, => 'Src';
88 post qr,$ct/pb/$word/submit, => 'Submit';
89
90 post qr,/action/register, => 'Register';
91 post qr,/action/passwd, => 'Passwd';
92 }
93
94 1;
95 __END__
This page took 0.029414 seconds and 3 git commands to generate.