Annotation of HOPE/Net-OpenAMD/script/test_server.pl, Revision 1.5
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};
51: foreach my $param ( $self->param ) {
1.1 andrew 52: my $value = $self->param($param);
1.5 ! andrew 53: @data = grep {
! 54: ref $_ ne 'HASH'
! 55: || ( !exists $_->{$param} )
! 56: || $value ~~ $_->{$param}
! 57: } @data;
1.1 andrew 58: }
59: $self->render_json( \@data );
60: };
61:
62: app->start;
63: __DATA__
64:
65: @@ index.html.ep
66: % layout 'default';
1.4 andrew 67: Please try /api/action, or see
68: <a href="http://amd.home.net">amd.hope.net</a>.
1.1 andrew 69:
70: @@ layouts/default.html.ep
71: <!doctype html><html>
72: <head><title>Test OpenAMD API!</title></head>
73: <body><%== content %></body>
1.2 andrew 74: </html>
75:
76: @@ exception.html.ep
77: <!doctype html><html>
78: <head><title>Exception</title></head>
79: <body><%== $exception %></body>
1.1 andrew 80: </html>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>