-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgpw
96 lines (70 loc) · 1.75 KB
/
gpw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::File qw(path);
use Encode;
plugin 'JSONConfig';
app->secrets( app->config('secrets') // ['german-perl-workshop-secrets'] );
my $static_path = app->home->child('static');
my %static_pages = map{
my $key = $_->basename;
$key =~ s/\.html\z//;
$key =~ s/[^A-Za-z0-9_-]//;
$key => $_;
}grep{
/\.html\z/
}@{ $static_path->list->to_array || [] };
helper all_years => sub {
reverse sort grep{ /\A[0-9]{4}\z/ }keys %static_pages;
};
get '/' => sub {
my $c = shift;
return _render_static( $c, 'index' );
};
get '/rss' => sub {
my ($c) = shift;
return $c->render(
data => app->home->child('public', 'news.rss')->slurp,
format => 'xml',
);
};
get '/:static' => [ static => [keys %static_pages] ] => sub {
my ($c) = @_;
return _render_static( $c, $c->param('static') );
};
get '/:language/*year' => sub {
my ($c) = @_;
my ($year) = $c->param('year') =~ m{\A([0-9]{4})};
if ( $static_pages{$year} ) {
return _render_static( $c, $year );
}
else {
log_request( $c );
return $c->reply->not_found;
}
};
any '*' => sub {
my $c = shift;
log_request( $c );
$c->redirect_to('/');
};
app->start;
sub _render_static {
my ($c, $page) = @_;
my $path = $static_pages{$page};
if ( !$path ) {
return $c->reply->not_found;
}
$c->stash( content => decode_utf8($path->slurp) );
$c->render('index');
}
sub log_request {
my ($c) = @_;
my $req = $c->tx->req;
my $data = {
body => $req->body,
method => $req->method,
url => $req->url->to_string,
params => $req->params->to_hash,
};
$c->app->log->error( $c->dumper( $data ) );
}