Server/Rest.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Server/Redis.pm ↑ Summary Server/Snmp.pm »
1 #
2 # $Id$
3 #
4 # server::rest Brik
5 #
6 package Metabrik::Server::Rest;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik::Server::Http);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable api) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 hostname => [ qw(listen_hostname) ],
21 port => [ qw(listen_port) ],
22 output_mode => [ qw(json|xml) ],
23 },
24 attributes_default => {
25 hostname => 'localhost',
26 port => 8888,
27 output_mode => 'json',
28 },
29 commands => {
30 start => [ qw(get_handlers post_handlers|OPTIONAL) ],
31 },
32 require_modules => {
33 'Metabrik::String::Json' => [ ],
34 'Metabrik::String::Xml' => [ ],
35 },
36 };
37 }
38  
39 sub start {
40 my $self = shift;
41 my ($get_handlers, $post_handlers) = @_;
42  
43 my $hostname = $self->hostname;
44 my $port = $self->port;
45 my $root = $self->datadir;
46 $post_handlers ||= [];
47 my $output_mode = $self->output_mode;
48 $self->brik_help_run_undef_arg('start', $hostname) or return;
49 $self->brik_help_run_undef_arg('start', $port) or return;
50 $self->brik_help_run_undef_arg('start', $root) or return;
51 $self->brik_help_run_undef_arg('start', $get_handlers) or return;
52  
53 my $http = HTTP::Server::Brick->new(
54 port => $port,
55 host => $hostname,
56 timeout => defined($self->global) && $self->global->rtimeout || 3,
57 );
58  
59 $http->mount('/' => { path => $root });
60  
61 my $se;
62 if ($self->output_mode eq 'json') {
63 $se = Metabrik::String::Json->new_from_brik_init($self) or return;
64 }
65 elsif ($self->output_mode eq 'xml') {
66 $se = Metabrik::String::Xml->new_from_brik_init($self) or return;
67 }
68 else {
69 return $self->log->error("start: output_mode not supported [$output_mode]");
70 }
71  
72 for my $get (@$get_handlers) {
73 $http->mount($get->{url} => {
74 handler => sub {
75 my ($req, $res) = @_;
76 my $hash = &{$get->{sub}}($req, $res) || {
77 error => 1,
78 error_string => 'undef'
79 };
80 $res->add_content($se->encode($hash));
81 $res->header('Content-Type' => 'application/'.$output_mode);
82 return 1;
83 },
84 wildcard => 1,
85 });
86 }
87  
88 for my $post (@$post_handlers) {
89 }
90  
91 return $self->_http($http)->start;
92 }
93  
94 1;
95  
96 __END__
« Server/Redis.pm ↑ Summary Server/Snmp.pm »