| 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/<uuid> = liefert JSON-Dokument oder Fehler zurueck | ||||||||||
| 8 | # POST /update/<uuid> = aendert JSON-Dokument (erzeugt neue Revision) | ||||||||||
| 9 | # GET /delete/<uuid> = loescht JSON-Dokument | ||||||||||
| 10 | # GET /file/<filename> = gibt Datei innerhalb Document-Root zurueck | ||||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use 5.010000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use HTTP::AppServer::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | our $VERSION = '0.04'; | |||||||||
| 19 | |||||||||||
| 20 | sub new | ||||||||||
| 21 | 0 | ▶ | { | ||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 22 | 0 | my ($class, @args) = @_; | |||||||||
| 23 | 0 | my $self = bless {}, $class; | |||||||||
| 24 | 0 | return $self->init(@args); | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | sub init | ||||||||||
| 28 | 0 | ▶ | { | ||||||||
|
Subroutine
init: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 29 | 0 | my ($self, %opts) = @_; | |||||||||
| 30 | |||||||||||
| 31 | # server options defaults | ||||||||||
| 32 | 0 | my %defaults = (StartBackground => 0, ServerPort => 3000); | |||||||||
| 33 | |||||||||||
| 34 | # set options or use defaults | ||||||||||
| 35 | 0 | ▶ | map { $self->{$_} = (exists $opts{$_} ? $opts{$_} : $defaults{$_}) } | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | keys %defaults; | ||||||||||
| 37 | |||||||||||
| 38 | 0 | $self->{'server'} = HTTP::AppServer::Base->new($self->{'ServerPort'}); | |||||||||
| 39 | |||||||||||
| 40 | 0 | return $self; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | sub handle | ||||||||||
| 44 | 0 | ▶ | { | ||||||||
|
Subroutine
handle: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 45 | 0 | my ($self, %pairs) = @_; | |||||||||
| 46 | 0 | map { $self->{'server'}->handle($_, $pairs{$_}) } keys %pairs; | |||||||||
| 47 | 0 | return 1; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | sub plugin | ||||||||||
| 51 | 0 | ▶ | { | ||||||||
|
Subroutine
plugin: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 52 | 0 | my ($self, $name, %options) = @_; | |||||||||
| 53 | |||||||||||
| 54 | # load module | ||||||||||
| 55 | 0 | eval('use HTTP::AppServer::Plugin::'.$name); | |||||||||
| 56 | 0 | ▶ | die "Failed to load plugin '$name': $@\n" if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | # call init() method of module to get the handlers of the plugin | ||||||||||
| 59 | 0 | eval('$self->handle( HTTP::AppServer::Plugin::'.$name.'->init($self->{"server"}, %options))'); | |||||||||
| 60 | 0 | ▶ | die "Failed to install handlers for plugin '$name': $@\n" if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | sub start | ||||||||||
| 64 | 0 | ▶ | { | ||||||||
|
Subroutine
start: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 65 | 0 | my ($self) = @_; | |||||||||
| 66 | |||||||||||
| 67 | 0 | $self->{'server'}->debug(); | |||||||||
| 68 | |||||||||||
| 69 | # start the server on port | ||||||||||
| 70 | 0 | ▶ | if ($self->{'StartBackground'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 0 | my $pid = $self->{'server'}->background(); | |||||||||
| 72 | 0 | print "Server started (http://localhost:$self->{'ServerPort'}) with PID $pid\n"; | |||||||||
| 73 | } else { | ||||||||||
| 74 | 0 | print "Server started (http://localhost:$self->{'ServerPort'})\n"; | |||||||||
| 75 | 0 | my $pid = $self->{'server'}->run(); | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 1 | 1; | |||||||||
| 80 | __END__ | ||||||||||