line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# server::rest Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::Server::Rest; |
7
|
1
|
|
|
1
|
|
734
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
28
|
|
8
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Metabrik::Server::Http); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
561
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable api) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
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
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
41
|
0
|
|
|
|
|
|
my ($get_handlers, $post_handlers) = @_; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $hostname = $self->hostname; |
44
|
0
|
|
|
|
|
|
my $port = $self->port; |
45
|
0
|
|
|
|
|
|
my $root = $self->datadir; |
46
|
0
|
|
0
|
|
|
|
$post_handlers ||= []; |
47
|
0
|
|
|
|
|
|
my $output_mode = $self->output_mode; |
48
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('start', $hostname) or return; |
49
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('start', $port) or return; |
50
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('start', $root) or return; |
51
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('start', $get_handlers) or return; |
52
|
|
|
|
|
|
|
|
53
|
0
|
|
0
|
|
|
|
my $http = HTTP::Server::Brick->new( |
54
|
|
|
|
|
|
|
port => $port, |
55
|
|
|
|
|
|
|
host => $hostname, |
56
|
|
|
|
|
|
|
timeout => defined($self->global) && $self->global->rtimeout || 3, |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
$http->mount('/' => { path => $root }); |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my $se; |
62
|
0
|
0
|
|
|
|
|
if ($self->output_mode eq 'json') { |
|
|
0
|
|
|
|
|
|
63
|
0
|
0
|
|
|
|
|
$se = Metabrik::String::Json->new_from_brik_init($self) or return; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
elsif ($self->output_mode eq 'xml') { |
66
|
0
|
0
|
|
|
|
|
$se = Metabrik::String::Xml->new_from_brik_init($self) or return; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
else { |
69
|
0
|
|
|
|
|
|
return $self->log->error("start: output_mode not supported [$output_mode]"); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
for my $get (@$get_handlers) { |
73
|
|
|
|
|
|
|
$http->mount($get->{url} => { |
74
|
|
|
|
|
|
|
handler => sub { |
75
|
0
|
|
|
0
|
|
|
my ($req, $res) = @_; |
76
|
0
|
|
0
|
|
|
|
my $hash = &{$get->{sub}}($req, $res) || { |
77
|
|
|
|
|
|
|
error => 1, |
78
|
|
|
|
|
|
|
error_string => 'undef' |
79
|
|
|
|
|
|
|
}; |
80
|
0
|
|
|
|
|
|
$res->add_content($se->encode($hash)); |
81
|
0
|
|
|
|
|
|
$res->header('Content-Type' => 'application/'.$output_mode); |
82
|
0
|
|
|
|
|
|
return 1; |
83
|
|
|
|
|
|
|
}, |
84
|
0
|
|
|
|
|
|
wildcard => 1, |
85
|
|
|
|
|
|
|
}); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
|
for my $post (@$post_handlers) { |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
return $self->_http($http)->start; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
1; |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
__END__ |