X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=www%2Fanalysis.pl;fp=www%2Fanalysis.pl;h=9b45cc77870df40f804ef244eb1c3db1445fcedc;hp=0000000000000000000000000000000000000000;hb=cdb5cb80047a5a852bf9ba64fb14aa5bc5ef84d5;hpb=a01798debae3ac16607e44f4a8fc32668bf21147 diff --git a/www/analysis.pl b/www/analysis.pl new file mode 100755 index 0000000..9b45cc7 --- /dev/null +++ b/www/analysis.pl @@ -0,0 +1,63 @@ +#! /usr/bin/perl +use CGI; +use POSIX; +use Date::Manip; +use Linux::Inotify2; +use AnyEvent; +use strict; +use warnings; + +my $cv = AnyEvent->condvar; +my $updated = 0; +my $cgi = CGI->new; +my $inotify = Linux::Inotify2->new; +$inotify->watch("/srv/analysis.sesse.net/analysis.json", IN_MODIFY, sub { + $updated = 1; + $cv->send; +}); + +my $inotify_w = AnyEvent->io ( + fh => $inotify->fileno, poll => 'r', cb => sub { $inotify->poll } +); +my $wait = AnyEvent->timer ( + after => 30, + cb => sub { $cv->send; }, +); + +my $ims = 0; +if (exists($ENV{'HTTP_IF_MODIFIED_SINCE'})) { + my $date = Date::Manip::Date->new; + $date->parse($ENV{'HTTP_IF_MODIFIED_SINCE'}); + $ims = $date->printf("%s"); +} +my $time = (stat("/srv/analysis.sesse.net/analysis.json"))[9]; + +# If we have something that's modified since IMS, send it out at once +if ($time > $ims) { + output(); + exit; +} + +# If not, wait, then send. Apache will deal with the 304-ing. +if (defined($cgi->param('first')) && $cgi->param('first') != 1) { + $cv->recv; +} +output(); + +sub output { + my $time = (stat("/srv/analysis.sesse.net/analysis.json"))[9]; + my $lm_str = POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time)); + + print CGI->header(-type=>'text/json', + -last_modified=>$lm_str, + -access_control_allow_origin=>'http://analysis.sesse.net', + -expires=>'now'); + open my $fh, "<", "/srv/analysis.sesse.net/analysis.json" + or die "/srv/analysis.sesse.net/analysis.json: $!"; + my $data; + { + local $/ = undef; + $data = <$fh>; + } + print $data; +}