=================================================================== RCS file: /cvs/HOPE/Net-OpenAMD/script/test_server.pl,v retrieving revision 1.1 retrieving revision 1.4 diff -u -r1.1 -r1.4 --- HOPE/Net-OpenAMD/script/test_server.pl 2010/06/25 03:30:56 1.1 +++ HOPE/Net-OpenAMD/script/test_server.pl 2010/06/26 17:16:08 1.4 @@ -1,105 +1,59 @@ #!/usr/bin/env perl use Mojolicious::Lite; +use Mojo::JSON; +use Data::Dumper; + use 5.010; -my %Actions = ( - location => [ - { area => 'Engressia', - 'time' => '2387', - x => '46.7369918823242', - y => '83.1452331542969', - user => '12983', - button => 'False', - }, - ], - speakers => [ - { name => 'Johnny', - bio => 'Johnny\'s bio' - }, - { name => 'Judas', - bio => 'Judas\'s bio' - }, - ], - talks => [ - { speakers => [ 'Judas', 'JohnnyX' ], - title => 'Ancient Egyptian Music and DRM', - abstract => - 'A discussion of the development of musical notation, which was designed as a means of reproducing music while making it impossible for the general public to perform without permission.', - 'time' => '2008/7/18 13:00:00', - track => 'Hooper', - interests => [ 'media', 'crypto' ] - }, - ], - interests => [ - 'new tech', 'activism', - 'radio', 'lockpicking', - 'crypto', 'privacy', - 'ethics', 'telephones', - 'social engineering', 'hacker spaces', - 'hardware hacking', 'nostalgia', - 'communities', 'science', - 'government', 'network security', - 'malicious software', 'pen testing', - 'web', 'niche hacks', - 'media' - ], - users => [ - { name => 'JohnnyX', - x => '32.54091324', - y => '54.10958384', - interests => [ - 'new tech', 'radio', - 'lockpicking', 'crypto', - 'telephones', 'social engineering', - 'hacker spaces', 'hardware hacking', - 'nostalgia', 'communities', - 'science', 'network security', - 'malicious software', 'pen testing', - ], - }, - { name => 'andrew', - x => '32', - y => '54', - interests => [ - 'media', - 'lockpicking', 'crypto', - 'telephones', 'social engineering', - 'hacker spaces', 'hardware hacking', - 'science', 'network security', - 'malicious software', 'pen testing', - ], - }, - { name => 'Judas', - x => '33', - y => '52', - interests => [ - 'media', - 'lockpicking', 'crypto', - 'telephones', 'social engineering', - 'science', 'network security', - 'malicious software', 'pen testing', - ], - }, - ], +app->renderer->add_helper( + action => sub { + my ( $self, $action ) = @_; + + state %Actions; + + my $path = app->home->rel_file( 'data/' . $action ); + + return if ! -e $path; + my $mtime = ( stat _ )[9]; + + $Actions{$action}{mtime} //= 0; + + if ( $mtime != $Actions{$action}{mtime} ) { + open my $fh, $path or die "Couldn't open [$path]: $!"; + my $content = do { local $/; <$fh> }; + close $fh; + + my $json = Mojo::JSON->new; + my $data = $json->decode($content); + if ( $json->error ) { + die $json->error; + } + + $Actions{$action} = { + mtime => $mtime, + data => $data, + }; + } + + return $Actions{$action}{data}; + } ); get '/' => 'index'; get '/api/:action' => sub { - my $self = shift; - my $action = $self->stash('action'); + my $self = shift; - return if !exists $Actions{$action}; + my $data = $self->helper( 'action', $self->stash('action') ); + return if !ref $data; - my @data = @{ $Actions{$action} }; - foreach my $param ( $self->param() ) { - next if $param eq 'callback'; - next if $param eq 'action'; - + my @data = @{$data}; + foreach my $param ( $self->param ) { my $value = $self->param($param); - @data = grep $value ~~ $_->{$param}, @data; + @data = grep { ( !exists $_->{$param} ) || $value ~~ $_->{$param} } + @data; } $self->render_json( \@data ); }; @@ -109,10 +63,17 @@ @@ index.html.ep % layout 'default'; -Please try /api/action +Please try /api/action, or see +amd.hope.net. @@ layouts/default.html.ep Test OpenAMD API! <%== content %> + + +@@ exception.html.ep + + Exception + <%== $exception %>