5f48fbd74da27f91968815395e95c229d465b32c
[APKPM.git] / web_ui.pl
1 #!/usr/bin/env perl
2
3 use Mojolicious::Lite;
4
5 use lib '/srv/MojoX-Gearman/lib';
6 use MojoX::Gearman;
7
8 use Data::Dump qw(dump);
9
10 # Documentation browser under "/perldoc" (this plugin requires Perl 5.10)
11 plugin 'pod_renderer';
12
13 get '/' => sub {
14         my $self = shift;
15         $self->render('index');
16 };
17
18 get '/ping_http' => sub {
19         my $self = shift;
20         my $gearman = $self->client;
21         my $pong = $gearman->post( "http://localhost:4780/ping" => { Connection => 'close' } => "some data" )->res->body;
22         warn "ping = $pong";
23         $self->render( 'ping', pong => $pong );
24 };
25
26 my $gearman = MojoX::Gearman->new; #( ioloop => Mojo::IOLoop->singleton );
27 $gearman->server( $ENV{GEARMAN} || 'localhost:4730' );
28
29 get '/ping_g' => sub {
30         my $self = shift;
31         my $pong = $gearman->req( 'SUBMIT_JOB', 'ping', '', "some data 2" );
32         warn "ping = $pong";
33         $self->render( 'ping', pong => $pong );
34 };
35
36 sub _render_jsonp {
37         my ( $self, $json ) = @_;
38         #my $data = $self->render( json => $json, partial => 1 );
39         my $data = $json;
40         if ( my $callback = $self->param('callback') ) {
41                 $data = "$callback($data)";
42         }
43         $self->render( data => $data, format => 'js' );
44 }
45
46
47 get '/g/:call/:args' => [ args => qr/.*/ ] => sub {
48         my $self = shift;
49         my $ret = $gearman->req( 'SUBMIT_JOB', $self->param('call'), '', $self->param('args') );
50         warn $self->param('call'), " = ", dump($ret), "\n";
51         die "no result for ", $self->param('call'), ' args: ', $self->param('args') unless defined $ret;
52         _render_jsonp( $self, $ret );
53 };
54
55 get '/_g/status' => sub {
56         my $self = shift;
57
58         my $ret = $gearman->req( 'TEXT' => 'status' );
59         warn "# status:\n$ret";
60
61         my @c = qw(function total running available);
62
63         my $status;
64         foreach my $l ( split(/\n/,$ret) ) {
65                 my @v = split(/\t/, $l);
66                 my $h;
67                 $h->{$c[$_]} = $v[$_] foreach 0 .. $#v;
68                 push @$status, $h;
69         }
70         warn "## ", dump $status;
71         _render_jsonp( $self, Mojo::JSON->new->encode({ status => $status }) );
72 };
73
74 app->start;
75 __DATA__
76
77 @@ index.html.ep
78 % layout 'default';
79 % title 'Gearman demo';
80
81 <ul>
82 <li><a href="/gearman.html#ping/127.0.0.1">Gearman</a> web interface
83 <li><%= link_to 'CRM' => 'CRM.html' %> search with tabular output
84 </ul>
85
86 Low-level API tests:
87
88 <%= link_to 'HTTP ping' => 'ping_http' %>
89 <%= link_to 'Gearman ping' => 'ping_g' %>
90
91 @@ ping.html.ep
92 % layout 'default';
93 pong: <tt><%= $pong %>
94
95 @@ dump.html.ep
96 % layout 'default';
97 <pre><%= $dump %></pre>
98
99 @@ layouts/default.html.ep
100 <!doctype html><html>
101   <head><title><%= title %></title></head>
102   <body><%= content %></body>
103 </html>