-
Notifications
You must be signed in to change notification settings - Fork 5
/
json.psgi
executable file
·66 lines (55 loc) · 1.69 KB
/
json.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
#!/usr/bin/env plackup
use strict;
use warnings;
use lib 'lib';
use Acoustics;
use Acoustics::Web;
use Log::Log4perl ':easy';
use CGI::Simple;
use CGI::Carp 'fatalsToBrowser';
use List::MoreUtils 'none';
use JSON::DWIW ();
use Plack::Builder;
my $app = sub {
my $env = shift;
my $q = CGI::Simple->new($env->{QUERY_STRING});
my $acoustics = Acoustics->new;
my $web = Acoustics::Web->new({
psgi_env => $env,
acoustics => $acoustics,
cgi => $q,
boolean_callback => sub {$_[0] ? JSON::DWIW::true : JSON::DWIW::false},
});
# hide private methods and revert to the default mode
my $mode = lc($q->param('mode') || '');
$mode = 'resource' if $mode =~ /^_/ or $mode =~ /[^\w_]/ or $mode eq 'new';
$mode = 'resource' unless $web->can($mode);
my($headers, $data) = $web->$mode();
# FIXME: clobbers duplicate headers
my %headers = @$headers;
# If they don't specify a type, assume it is a data structure that we
# should encode to JSON and change the header accordingly.
unless ($headers{'-type'}) {
$headers{'-type'} = 'application/json';
$data = scalar JSON::DWIW->new({
pretty => 1,
escape_multi_byte => 1,
bad_char_policy => 'convert',
})->to_json($data);
}
$headers{'Content-Type'} = delete $headers{'-type'};
$acoustics->db->disconnect;
my $status = 200;
if ($headers{'-status'}) {
($status) = $headers{'-status'} =~ /^(\d+)/;
delete $headers{'-status'};
}
return [$status, [%headers], [$data]];
};
builder {
enable 'Static', path => sub { s{^/?www-data}{} }, root => './www-data/';
enable 'Static', path => qr{^/?index2?\.html}, root => './';
enable 'Session', store => 'File';
$app;
};
# vim: ft=perl: