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

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';
                     66: Please try /api/action
                     67:
                     68: @@ layouts/default.html.ep
                     69: <!doctype html><html>
                     70:     <head><title>Test OpenAMD API!</title></head>
                     71:     <body><%== content %></body>
1.2       andrew     72: </html>
                     73:
                     74: @@ exception.html.ep
                     75: <!doctype html><html>
                     76:     <head><title>Exception</title></head>
                     77:     <body><%== $exception %></body>
1.1       andrew     78: </html>

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