-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.psgi
150 lines (139 loc) · 7.53 KB
/
app.psgi
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use strict;
use warnings;
use BeLaws::API;
use BeLaws::API::Staatsblad;
use BeLaws::Frontend;
use Data::Dumper;
use Plack::Builder;
use Plack::Middleware::Static;
use Plack::App::File;
use WebHive::Router;
use Plack::Middleware::Throttle::Hourly;
use Plack::Middleware::Throttle::Backend::Hash;
my $error_logfile = $ENV{HOME}."/var/log/belawshub.error.log";
our $app = builder {
for(0..$#ARGV) {
if($ARGV[$_] =~ /^--error-log$/) {
$error_logfile = $ARGV[$_+1] if defined $ARGV[$_+1];
warn sprintf "errorlog has been redirected to %s", $error_logfile;
open STDERR, ">>", $error_logfile or die $!;
last;
}
}
enable "HTTPExceptions";
mount "/api" => builder {
# enable 'Debug::DBIProfile', profile => 2;
enable "Throttle::Hourly", max => 1000, backend => Plack::Middleware::Throttle::Backend::Hash->new();
enable "JSONP", callback_key => 'callback';
mount "/search.json" => \&BeLaws::API::search;
mount "/doc.json" => \&BeLaws::API::doc;
mount "/status.json" => \&BeLaws::API::status;
mount "/top.json" => \&BeLaws::API::Staatsblad::top;
mount "/class" => builder {
mount "/person.json" => \&BeLaws::API::class_person;
mount "/cat.json" => \&BeLaws::API::class_cat;
mount "/geo.json" => \&BeLaws::API::class_geo;
};
mount "/retry" => builder {
mount "/staatsblad" => \&BeLaws::API::Staatsblad::retry;
};
mount "/info" => builder {
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::format;
mount "/raadvanstate.json" => \&BeLaws::API::info_raadvanstate;
mount "/juridat.json" => \&BeLaws::API::info_juridat;
};
mount "/stats" => builder {
mount "/person_party.json" => \&BeLaws::API::stats_person_party;
mount "/person_cosign.json" => \&BeLaws::API::stats_person_cosign;
mount "/word_trends_per_month.json" => \&BeLaws::API::word_trends_per_month;
};
mount "/seed" => builder {
mount "/docuid.json" => \&BeLaws::API::Staatsblad::seed;
};
mount "/internal/" => builder {
enable "Access", rules => [ allow => "127.0.0.0/8" ];
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t" . join "\n\t", map { "/api/internal/$_" } qw/fetch parse format resolve info test stat/] ] };
mount "/fetch" => builder {
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t/api/internal/fetch/staatsblad.json?docuid=YYYY-MM-DD/NN" ] ] };
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::fetch;
};
mount "/parse" => builder {
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t/api/internal/parse/staatsblad.json?docid=YYYY-MM-DD/NN" ] ] };
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::parse;
};
mount "/format" => builder {
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t/api/internal/format/staatsblad.json?docid=YYYY-MM-DD/NN" ] ] };
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::format;
};
mount "/resolve" => builder {
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t/api/internal/resolve/staatsblad.json?docuid=YYYY-MM-DD/NN" ] ] };
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::resolve;
};
mount "/info" => builder {
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t/api/internal/info/staatsblad.json?docuid=YYYY-MM-DD/NN" ] ] };
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::info;
};
mount "/test" => builder {
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t/api/internal/test/staatsblad.json?docuid=YYYY-MM-DD/NN" ] ] };
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::test;
};
mount "/stat" => builder {
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t/api/internal/stat/staatsblad.json?docuid=YYYY-MM-DD/NN" ] ] };
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::stat;
};
mount "/top" => builder {
mount "/" => sub { return [ 200, [ 'Content-Type' => 'text/plain' ], [ "usage:\n\t/api/internal/stat/staatsblad.json?docuid=YYYY-MM-DD/NN" ] ] };
mount "/staatsblad.json" => \&BeLaws::API::Staatsblad::top;
};
};
mount "/list.json" => \&BeLaws::API::search; # backwards compat
};
mount "/developer" => builder {
mount "/doc" => builder {
mount "/api.v1.html" => Plack::App::File->new(file => 'html/developer/doc/api.v1.html');
};
mount "/test" => builder {
mount "/" => Plack::App::File->new(file => 'html/developer/test/index.html');
};
};
mount "/statistics" => builder {
enable 'Plack::Middleware::Static', path => qr{^/\w+\.html}, root => 'html/statistics';
mount "/" => Plack::App::File->new(file => 'html/statistics/index.html'); #catchall
};
# redirection of alpha API to beta {{{
mount "/doc" => builder {
mount "/show.html" => sub {
my ($q,$d) = (shift->{REQUEST_URI} =~ m#&q=([^&]+)&d=([\d\-\/]+)#);
return [ 302, [ 'Content-Type' => 'text/plain', 'Location' => '/doc.html?q='.$q.'&d='.$d ], ['redirecting'] ];
}
};
mount "/d/" => sub {
my ($d) = (shift->{REQUEST_URI} =~ m#/d/([\d\-\/]+)#);
return [ 302, [ 'Content-Type' => 'text/plain', 'Location' => '/doc.html?d='.$d ], ['redirecting'] ];
};
mount "/s/" => sub {
my ($q,$d) = (shift->{REQUEST_URI} =~ m#/s/([^/]+)/d/([\d\-\/]+)#);
return [ 302, [ 'Content-Type' => 'text/plain', 'Location' => '/app.html?q='.$q.'&d='.$d ], ['redirecting'] ] };
# redirection of old html files
mount "/index.html" => sub { return [ 302, [ 'Content-Type' => 'text/plain', 'Location' => '/index.phtml' ], ['redirecting'] ] };
mount "/search.html" => sub { return [ 302, [ 'Content-Type' => 'text/plain', 'Location' => '/index.phtml' ], ['redirecting'] ] };
# }}}
mount "/" => builder {
enable 'Plack::Middleware::Static', path => qr{^/(images|js|vendor|html|css|favicon\.ico|app\.html|index\.html|app)}, root => 'html/';
enable '+WebHive::Middleware::Template', dirs => ['./html/'];
router {
namespace 'BeLaws';
get '/{lang}/{year}/{no}' => { controller => 'Frontend', action => 'saved_doc' },
get '/{lang}/{year}/{no}/' => { controller => 'Frontend', action => 'saved_doc' },
get '/{lang}/{year}/{no}/{art}' => { controller => 'Frontend', action => 'saved_art_doc' },
get '/oembed/{lang}/{year}/{no}' => { controller => 'Frontend', action => 'oembed' },
get '/oembed/{lang}/{year}/{no}/{art}' => { controller => 'Frontend', action => 'oembed' },
get '/doc.html' => { controller => 'Frontend', action => 'doc' },
get "/" => { controller => 'Frontend', action => 'index' },
get "/index.phtml" => { controller => 'Frontend', action => 'index' },
get "/search.phtml" => { controller => 'Frontend', action => 'search' },
#get "/" => Plack::App::File->new(file => 'html/index.html'); #catchall
};
};
};
# vim: ft=perl