[Bps-public-commit] r16779 - in Prophet/trunk/lib/Prophet: Server
jesse at bestpractical.com
jesse at bestpractical.com
Sun Nov 9 05:58:23 EST 2008
Author: jesse
Date: Sun Nov 9 05:58:23 2008
New Revision: 16779
Modified:
Prophet/trunk/lib/Prophet/Server.pm
Prophet/trunk/lib/Prophet/Server/Dispatcher.pm
Log:
* Server code now all extracted to the dispatcher. now to tidy
Modified: Prophet/trunk/lib/Prophet/Server.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server.pm (original)
+++ Prophet/trunk/lib/Prophet/Server.pm Sun Nov 9 05:58:23 2008
@@ -53,93 +53,11 @@
my $d = Prophet::Server::Dispatcher->new(server => $self);
- my $http_status = $d->run($cgi->request_method."/". $cgi->path_info, $self);
-
- unless ($http_status) {
- if ( my $sub = $self->can( 'handle_request_' . lc( $cgi->request_method ) ) ) {
- $http_status = $sub->( $self);
- }
- }
- unless ($http_status) {
- $self->_send_404;
- }
-
-};
-
-sub handle_request_get_template {
- my $self = shift;
- my $p = $self->cgi->path_info;
-
-
- if (Template::Declare->has_template($p)) {
- Prophet::Server::View->app_handle($self->app_handle);
- my $content = Template::Declare->show($p);
-
- return $self->send_content(
- content_type => 'text/html',
- content => $content,
- );
-
- }
+ $d->run($cgi->request_method."/". $cgi->path_info, $self) || $self->_send_404;
};
-sub handle_request_get_replica {
- my $self = shift;
- my $p = $self->cgi->path_info;
-
- if ($p =~ qr{^/+replica/+(.*)$}) {
- my $repo_file = $1;
- return undef unless $self->handle->can('read_file');
-
- my $content = $self->handle->read_file($repo_file);
- return unless defined $content && length($content);
- return $self->send_content(
- content_type => 'application/x-prophet',
- content => $content
- );
- }
-}
-
-sub handle_request_get_rest {
- my $self = shift;
- my $p = $self->cgi->path_info;
-
- $p =~ s/^GET//i;
-}
-
-sub handle_request_post {
- my $self = shift;
-
- return $self->_send_401 if ($self->read_only);
-
- my $p = $self->cgi->path_info;
- if ( $p =~ m|^/records/(.*)/(.*)/(.*)| ) {
- my $type = $1;
- my $uuid = $2;
- my $prop = $3;
-
- my $record = $self->load_record( type => $type, uuid => $uuid );
- return $self->_send_404 unless ($record);
- $record->set_props( props => { $prop => ( $self->cgi->param('value') || undef ) } );
- return $self->_send_redirect( to => "/records/$type/$uuid/$prop" );
- } elsif ( $p =~ m|^/records/(.*)/(.*).json| ) {
- my $type = $1;
- my $uuid = $2;
- my $record = $self->load_record( type => $type, uuid => $uuid );
-
- return $self->_send_404 unless ($record);
-
- my $ret = $record->set_props( props => { map { $_ => $self->cgi->param($_) } $self->cgi->param() } );
- $self->_send_redirect( to => "/records/$type/$uuid.json" );
- } elsif ( $p =~ m|^/records/(.*).json| ) {
- my $type = $1;
- my $record = $self->load_record( type => $type );
- my $uuid = $record->create( props => { map { $_ => $self->cgi->param($_) } $self->cgi->param() } );
- return $self->_send_redirect( to => "/records/$type/$uuid.json" );
- }
-}
sub load_record {
my $self = shift;
Modified: Prophet/trunk/lib/Prophet/Server/Dispatcher.pm
==============================================================================
--- Prophet/trunk/lib/Prophet/Server/Dispatcher.pm (original)
+++ Prophet/trunk/lib/Prophet/Server/Dispatcher.pm Sun Nov 9 05:58:23 2008
@@ -8,16 +8,72 @@
sub token_delimiter { '/' }
sub case_sensitive_tokens { 0 }
-under 'GET' => sub {
+my $SERVER;
+
+on qr'.*' => sub { $SERVER = shift; next_rule;};
+
+on 'POST' => sub {
+ return $SERVER->_send_401 if ( $SERVER->read_only );
+ next_rule;
+};
+
+under 'POST' => sub {
+
+ under 'records' => sub {
+ on qr|(.*)/(.*)/(.*)| => sub {
+ my $type = $1;
+ my $uuid = $2;
+ my $prop = $3;
+
+ my $record = $SERVER->load_record( type => $type, uuid => $uuid );
+ return $SERVER->_send_404 unless ($record);
+ $record->set_props(
+ props => { $prop => ( $SERVER->cgi->param('value') || undef ) }
+ );
+ return $SERVER->_send_redirect(
+ to => "/records/$type/$uuid/$prop" );
+ };
+ on qr|(.*)/(.*).json| => sub {
+ my $type = $1;
+ my $uuid = $2;
+ my $record = $SERVER->load_record( type => $type, uuid => $uuid );
+
+ return $SERVER->_send_404 unless ($record);
+
+ my $ret = $record->set_props(
+ props => {
+ map { $_ => $SERVER->cgi->param($_) } $SERVER->cgi->param()
+ }
+ );
+ $SERVER->_send_redirect( to => "/records/$type/$uuid.json" );
+ };
+ on qr|^(.*).json| => sub {
+ my $type = $1;
+ my $record = $SERVER->load_record( type => $type );
+ my $uuid = $record->create(
+ props => {
+ map { $_ => $SERVER->cgi->param($_) } $SERVER->cgi->param()
+ }
+ );
+ return $SERVER->_send_redirect( to => "/records/$type/$uuid.json" );
+ };
+ };
+};
+
- on 'replica' => sub {
- my $server = shift;
- return $server->handle_request_get_replica();
+under 'GET' => sub {
+ on qr'replica/+(.*)$' => sub {
+ my $repo_file = $1;
+ return undef unless $SERVER->handle->can('read_file');
+ my $content = $SERVER->handle->read_file($repo_file);
+ return unless defined $content && length($content);
+ return $SERVER->send_content( content_type => 'application/x-prophet', content => $content);
};
+
on 'records.json' => sub {
- my $server = shift;
- return $server->send_content( encode_as => 'json',
- content => $server->handle->list_types );
+ warn "SERVER IS ".server();
+ return $SERVER->send_content( encode_as => 'json',
+ content => $SERVER->handle->list_types );
};
@@ -26,44 +82,41 @@
under 'records' => sub {
on qr|(.*)/(.*)/(.*)| => sub {
- my $server = shift;
my $type = $1;
my $uuid = $2;
my $prop = $3;
- my $record = $server->load_record( type => $type, uuid => $uuid );
- return $server->_send_404 unless ($record);
+ my $record = $SERVER->load_record( type => $type, uuid => $uuid );
+ return $SERVER->_send_404 unless ($record);
if ( my $val = $record->prop($prop) ) {
- return $server->send_content(
+ return $SERVER->send_content(
content_type => 'text/plain',
content => $val
);
} else {
- return $server->_send_404();
+ return $SERVER->_send_404();
}
};
on qr|(.*)/(.*).json| => sub {
- my $server = shift;
my $type = $1;
my $uuid = $2;
- my $record = $server->load_record( type => $type, uuid => $uuid );
- return $server->_send_404 unless ($record);
- return $server->send_content(
+ my $record = $SERVER->load_record( type => $type, uuid => $uuid );
+ return $SERVER->_send_404 unless ($record);
+ return $SERVER->send_content(
encode_as =>'json',
content => $record->get_props
);
};
on qr|(.*).json| => sub {
- my $server = shift;
my $type = $1;
require Prophet::Collection;
my $col = Prophet::Collection->new(
- handle => $server->handle,
+ handle => $SERVER->handle,
type => $type
);
$col->matching( sub {1} );
warn "Query language not implemented yet.";
- return $server->send_content(
+ return $SERVER->send_content(
encode_as => 'json',
content =>
{ map {
@@ -74,9 +127,13 @@
);
};
- on '*' => sub {
- my $server = shift;
- return $server->handle_request_get_template();
+ on '^(.*)$' => sub {
+ my $p = $1;
+ if (Template::Declare->has_template($p)) {
+ Prophet::Server::View->app_handle($SERVER->app_handle);
+ my $content = Template::Declare->show($p);
+ return $SERVER->send_content( content_type => 'text/html', content => $content,);
+ }
};
};
More information about the Bps-public-commit
mailing list