[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.6

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

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