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

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

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