| 1 | package <%= ${controller} %>; | ||
| 2 | use Mojo::Base qw(Mojolicious::Plugin::Qooxdoo::JsonRpcController); | ||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | <%= ${controller} %> - RPC services for <%= ${class} %> | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | $route->any("jsonrpc")->to(<%= ${controller} %>#dispatch"); | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | This controller handles the rpc calls from the Qooxdoo frontend. | ||
| 15 | |||
| 16 | =head1 ATTRIBUTES | ||
| 17 | |||
| 18 | All the attributes from L<Mojolicious::Plugin::Qooxdoo::JsonRpcController>. | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | All the methods of L<Mojolicious::Plugin::Qooxdoo::JsonRpcController> as well as: | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | =head1 ATTRIBUTES | ||
| 29 | |||
| 30 | The controller has the following attributes. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | =head2 service | ||
| 35 | |||
| 36 | The service property defines the name of the service. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | has service => sub { "<%= ${filename} %>"}; | ||
| 41 | |||
| 42 | has log => sub { shift->app->log }; | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | The controller provides the following methods: | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | =head2 allow_rpc_access(method) | ||
| 51 | |||
| 52 | The dispatcher will call allow_rpc_access prior to handing over controll. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | our %allow = ( | ||
| 57 | ping => 1, | ||
| 58 | getUptime => 1, | ||
| 59 | makeException => 1, | ||
| 60 | ); | ||
| 61 | |||
| 62 | |||
| 63 | sub allow_rpc_access { | ||
| 64 | my $self = shift; | ||
| 65 | my $method = shift; | ||
| 66 | return $allow{$method}; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | =head2 ping(text) | ||
| 71 | |||
| 72 | The ping response. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub ping { | ||
| 77 | my $self = shift; | ||
| 78 | my $text = shift; | ||
| 79 | $self->log->info("We got pinged"); | ||
| 80 | return 'got '.$text; | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 getUptime | ||
| 84 | |||
| 85 | Return the output of uptime. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub getUptime { | ||
| 90 | my $self = shift; | ||
| 91 | return `/usr/bin/uptime`; | ||
| 92 | } | ||
| 93 | |||
| 94 | =head2 makeException(code,message) | ||
| 95 | |||
| 96 | Create an exception. | ||
| 97 | |||
| 98 | =cut | ||
| 99 | |||
| 100 | sub makeException { | ||
| 101 | my $self = shift; | ||
| 102 | my $arg = shift; | ||
| 103 | die Exception->new(code => $arg->{code}, message => $arg->{message} ); | ||
| 104 | } | ||
| 105 | |||
| 106 | package Exception; | ||
| 107 | |||
| 108 | use Mojo::Base -base; | ||
| 109 | has 'code'; | ||
| 110 | has 'message'; | ||
| 111 | use overload ('""' => 'stringify'); | ||
| 112 | sub stringify { | ||
| 113 | my $self = shift; | ||
| 114 | return "ERROR ".$self->code.": ".$self->message; | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; | ||
| 118 | <%= '__END__' %> | ||
| 119 | |||
| 120 | =head1 COPYRIGHT | ||
| 121 | |||
| 122 | Copyright (c) <%= ${year} %> by <%= ${fullName} %>. All rights reserved. | ||
| 123 | |||
| 124 | =head1 AUTHOR | ||
| 125 | |||
| 126 | S<<%= ${fullName} %> E<lt><%= ${email} %>E<gt>> | ||
| 127 | |||
| 128 | =cut |