-
Notifications
You must be signed in to change notification settings - Fork 5
/
json.pl
executable file
·65 lines (54 loc) · 1.65 KB
/
json.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use lib 'lib';
use Acoustics;
use Acoustics::Web;
use Log::Log4perl ':easy';
use CGI::Simple '-debug1';
use CGI::Carp 'fatalsToBrowser';
use List::MoreUtils 'none';
use JSON::DWIW ();
# Determine if we are running under FastCGI or not
my $req;
my $running_under_fastcgi = not scalar keys %ENV;
if ($running_under_fastcgi) {
require FCGI;
$req = FCGI::Request();
$req->Accept() >= 0 or exit 1;
}
do {
my $q = CGI::Simple->new;
my $acoustics = Acoustics->new;
my $web = Acoustics::Web->new({
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 = 'status' if $mode =~ /^_/ or $mode =~ /[^\w_]/ or $mode eq 'new';
$mode = 'status' unless $web->can($mode);
my($headers, $data) = $web->$mode;
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);
$q->no_cache(1);
binmode STDOUT, ':utf8';
}
print $q->header(%headers);
if ($data) {
print $data;
}
# finish FastCGI if needed and auto-reload ourselves if we were modified
$req->Finish if $running_under_fastcgi;
exit if (-M $ENV{SCRIPT_FILENAME}) < 0;
$acoustics->db->disconnect;
} while ($running_under_fastcgi && $req->Accept() >= 0);