[BACK]Return to test_server.pl CVS log [TXT][DIR] Up to [local] / HOPE / Net-OpenAMD / script

Annotation of HOPE/Net-OpenAMD/script/test_server.pl, Revision 1.7

1.1       andrew      1: #!/usr/bin/env perl
                      2:
1.7     ! andrew      3: use 5.010;
        !             4:
        !             5: BEGIN {
        !             6:     use FindBin;
        !             7:     use lib "$FindBin::Bin/../lib";
        !             8:     use lib "$FindBin::Bin/../mojo/lib";
        !             9:     use lib "$FindBin::Bin/../../mojo/lib";
        !            10: }
        !            11:
1.1       andrew     12: use Mojolicious::Lite;
1.2       andrew     13: use Mojo::JSON;
1.1       andrew     14:
1.2       andrew     15: app->renderer->add_helper(
                     16:     action => sub {
                     17:         my ( $self, $action ) = @_;
                     18:
1.3       andrew     19:         state %Actions;
1.2       andrew     20:
1.5       andrew     21:         my $path = app->home->rel_file( '../t/data/' . $action );
1.2       andrew     22:
1.5       andrew     23:         return if !-e $path;
1.3       andrew     24:         my $mtime = ( stat _ )[9];
                     25:
                     26:         $Actions{$action}{mtime} //= 0;
                     27:
                     28:         if ( $mtime != $Actions{$action}{mtime} ) {
1.2       andrew     29:             open my $fh, $path or die "Couldn't open [$path]: $!";
                     30:             my $content = do { local $/; <$fh> };
                     31:             close $fh;
                     32:
                     33:             my $json = Mojo::JSON->new;
                     34:             my $data = $json->decode($content);
                     35:             if ( $json->error ) {
                     36:                 die $json->error;
                     37:             }
                     38:
1.3       andrew     39:             $Actions{$action} = {
1.2       andrew     40:                 mtime => $mtime,
                     41:                 data  => $data,
                     42:             };
                     43:         }
                     44:
1.3       andrew     45:         return $Actions{$action}{data};
1.2       andrew     46:     }
1.1       andrew     47: );
                     48:
                     49: get '/' => 'index';
                     50:
                     51: get '/api/:action' => sub {
1.2       andrew     52:     my $self = shift;
1.1       andrew     53:
1.2       andrew     54:     my $data = $self->helper( 'action', $self->stash('action') );
                     55:     return if !ref $data;
1.1       andrew     56:
1.2       andrew     57:     my @data = @{$data};
1.6       andrew     58:
                     59:     my $p = $self->req->params->to_hash;
                     60:     while ( my ( $param, $value ) = each(%$p) ) {
1.5       andrew     61:         @data = grep {
1.6       andrew     62:                    ref $_ ne 'HASH'
1.5       andrew     63:                 || ( !exists $_->{$param} )
                     64:                 || $value ~~ $_->{$param}
                     65:         } @data;
1.1       andrew     66:     }
1.6       andrew     67:
1.1       andrew     68:     $self->render_json( \@data );
                     69: };
                     70:
                     71: app->start;
                     72: __DATA__
                     73:
                     74: @@ index.html.ep
                     75: % layout 'default';
1.4       andrew     76: Please try /api/action, or see
                     77: <a href="http://amd.home.net">amd.hope.net</a>.
1.1       andrew     78:
                     79: @@ layouts/default.html.ep
                     80: <!doctype html><html>
                     81:     <head><title>Test OpenAMD API!</title></head>
                     82:     <body><%== content %></body>
1.2       andrew     83: </html>
                     84:
                     85: @@ exception.html.ep
                     86: <!doctype html><html>
                     87:     <head><title>Exception</title></head>
                     88:     <body><%== $exception %></body>
1.1       andrew     89: </html>

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>