line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::AppServer; |
2
|
|
|
|
|
|
|
# Simple CRUD server for JSON objects and plain files. |
3
|
|
|
|
|
|
|
# 2010 by Tom Kirchner |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# routes: |
6
|
|
|
|
|
|
|
# POST /create = erzeugt neues JSON-Dokument, liefert UUId zurueck |
7
|
|
|
|
|
|
|
# GET /read/ = liefert JSON-Dokument oder Fehler zurueck |
8
|
|
|
|
|
|
|
# POST /update/ = aendert JSON-Dokument (erzeugt neue Revision) |
9
|
|
|
|
|
|
|
# GET /delete/ = loescht JSON-Dokument |
10
|
|
|
|
|
|
|
# GET /file/ = gibt Datei innerhalb Document-Root zurueck |
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
71135
|
use 5.010000; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
78
|
|
13
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
14
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
154
|
|
|
1
|
|
|
|
|
45
|
|
15
|
1
|
|
|
1
|
|
22361
|
use Data::Dumper; |
|
1
|
|
|
|
|
22721
|
|
|
1
|
|
|
|
|
80
|
|
16
|
1
|
|
|
1
|
|
709
|
use HTTP::AppServer::Base; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub new |
21
|
|
|
|
|
|
|
{ |
22
|
|
|
|
|
|
|
my ($class, @args) = @_; |
23
|
|
|
|
|
|
|
my $self = bless {}, $class; |
24
|
|
|
|
|
|
|
return $self->init(@args); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub init |
28
|
|
|
|
|
|
|
{ |
29
|
|
|
|
|
|
|
my ($self, %opts) = @_; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# server options defaults |
32
|
|
|
|
|
|
|
my %defaults = (StartBackground => 0, ServerPort => 3000); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# set options or use defaults |
35
|
|
|
|
|
|
|
map { $self->{$_} = (exists $opts{$_} ? $opts{$_} : $defaults{$_}) } |
36
|
|
|
|
|
|
|
keys %defaults; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$self->{'server'} = HTTP::AppServer::Base->new($self->{'ServerPort'}); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
return $self; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub handle |
44
|
|
|
|
|
|
|
{ |
45
|
|
|
|
|
|
|
my ($self, %pairs) = @_; |
46
|
|
|
|
|
|
|
map { $self->{'server'}->handle($_, $pairs{$_}) } keys %pairs; |
47
|
|
|
|
|
|
|
return 1; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub plugin |
51
|
|
|
|
|
|
|
{ |
52
|
|
|
|
|
|
|
my ($self, $name, %options) = @_; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
# load module |
55
|
|
|
|
|
|
|
eval('use HTTP::AppServer::Plugin::'.$name); |
56
|
|
|
|
|
|
|
die "Failed to load plugin '$name': $@\n" if $@; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# call init() method of module to get the handlers of the plugin |
59
|
|
|
|
|
|
|
eval('$self->handle( HTTP::AppServer::Plugin::'.$name.'->init($self->{"server"}, %options))'); |
60
|
|
|
|
|
|
|
die "Failed to install handlers for plugin '$name': $@\n" if $@; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub start |
64
|
|
|
|
|
|
|
{ |
65
|
|
|
|
|
|
|
my ($self) = @_; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$self->{'server'}->debug(); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
# start the server on port |
70
|
|
|
|
|
|
|
if ($self->{'StartBackground'}) { |
71
|
|
|
|
|
|
|
my $pid = $self->{'server'}->background(); |
72
|
|
|
|
|
|
|
print "Server started (http://localhost:$self->{'ServerPort'}) with PID $pid\n"; |
73
|
|
|
|
|
|
|
} else { |
74
|
|
|
|
|
|
|
print "Server started (http://localhost:$self->{'ServerPort'})\n"; |
75
|
|
|
|
|
|
|
my $pid = $self->{'server'}->run(); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
1; |
80
|
|
|
|
|
|
|
__END__ |