=================================================================== RCS file: /cvs/HOPE/Net-OpenAMD/script/test_server.pl,v retrieving revision 1.1 retrieving revision 1.7 diff -u -r1.1 -r1.7 --- HOPE/Net-OpenAMD/script/test_server.pl 2010/06/25 03:30:56 1.1 +++ HOPE/Net-OpenAMD/script/test_server.pl 2010/06/27 04:26:56 1.7 @@ -1,106 +1,70 @@ #!/usr/bin/env perl +use 5.010; + +BEGIN { + use FindBin; + use lib "$FindBin::Bin/../lib"; + use lib "$FindBin::Bin/../mojo/lib"; + use lib "$FindBin::Bin/../../mojo/lib"; +} + use Mojolicious::Lite; +use Mojo::JSON; -use 5.010; +app->renderer->add_helper( + action => sub { + my ( $self, $action ) = @_; -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', - ], - }, - ], + state %Actions; + + my $path = app->home->rel_file( '../t/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}; - my $value = $self->param($param); - @data = grep $value ~~ $_->{$param}, @data; + my $p = $self->req->params->to_hash; + while ( my ( $param, $value ) = each(%$p) ) { + @data = grep { + ref $_ ne 'HASH' + || ( !exists $_->{$param} ) + || $value ~~ $_->{$param} + } @data; } + $self->render_json( \@data ); }; @@ -109,10 +73,17 @@ @@ index.html.ep % layout 'default'; -Please try /api/action +Please try /api/action, or see +amd.hope.net. @@ layouts/default.html.ep