[Bps-public-commit] r15042 - in Prophet/branches/restyserver: . lib/Prophet

jesse at bestpractical.com jesse at bestpractical.com
Tue Aug 12 07:10:28 EDT 2008


Author: jesse
Date: Tue Aug 12 07:10:28 2008
New Revision: 15042

Modified:
   Prophet/branches/restyserver/   (props changed)
   Prophet/branches/restyserver/Makefile.PL
   Prophet/branches/restyserver/lib/Prophet/CLI/Command/Server.pm
   Prophet/branches/restyserver/lib/Prophet/Server.pm

Log:
 r43041 at PC0169:  jesse | 2008-08-12 01:35:08 +0100
 * refactored the resty server to make it possible to deny remote update
 * mooseified the resty server
 * removed the dep on HTTP::Server::Simple::Bonjour


Modified: Prophet/branches/restyserver/Makefile.PL
==============================================================================
--- Prophet/branches/restyserver/Makefile.PL	(original)
+++ Prophet/branches/restyserver/Makefile.PL	Tue Aug 12 07:10:28 2008
@@ -54,7 +54,6 @@
     'Bonjour support' => [
         -default => 0,
         'Net::Bonjour',
-        'HTTP::Server::Simple::Bonjour',
     ],
 
 );

Modified: Prophet/branches/restyserver/lib/Prophet/CLI/Command/Server.pm
==============================================================================
--- Prophet/branches/restyserver/lib/Prophet/CLI/Command/Server.pm	(original)
+++ Prophet/branches/restyserver/lib/Prophet/CLI/Command/Server.pm	Tue Aug 12 07:10:28 2008
@@ -5,14 +5,20 @@
 use Prophet::Server;
 
 sub run {
-
     my $self = shift;
+    my $server = $self->_setup_server();
+    $server->run;
+}
 
+sub _setup_server {
+    my $self = shift;
     my $server = Prophet::Server->new( $self->arg('port') || 8080 );
     $server->app_handle( $self->app_handle );
-    $server->run;
+    return $server;
 }
 
+
+
 __PACKAGE__->meta->make_immutable;
 no Moose;
 

Modified: Prophet/branches/restyserver/lib/Prophet/Server.pm
==============================================================================
--- Prophet/branches/restyserver/lib/Prophet/Server.pm	(original)
+++ Prophet/branches/restyserver/lib/Prophet/Server.pm	Tue Aug 12 07:10:28 2008
@@ -1,47 +1,42 @@
 package Prophet::Server;
-use warnings;
-use strict;
-use Prophet::App;
-
-BEGIN {
-    if (Prophet::App->try_to_require('HTTP::Server::Simple::Bonjour')) {
-        our @ISA = 'HTTP::Server::Simple::Bonjour';
-    }
-}
-
-use base qw'HTTP::Server::Simple::CGI';
+use Moose;
+extends qw'HTTP::Server::Simple::CGI';
 
 use Prophet::Server::View;
 use Params::Validate qw/:all/;
 use JSON;
-use Path::Class;
-
-# Support for the bonjour replica type
-our $DB_UUID;
-sub service_name { $DB_UUID }
-sub service_type { '_prophet._tcp' }
 
-sub app_handle {
-    my $self = shift;
-    if (@_) {
-        $self->{'app_handle'} = shift;
-        $DB_UUID = $self->{'app_handle'}->handle->db_uuid;
+has app_handle => ( isa => 'Prophet::App', is => 'rw',
+    handles => [ qw/handle/]
+);
+
+has read_only => ( is => 'rw', isa => 'Bool');
+
+before run => sub {
+    my $self      = shift;
+    my $publisher = eval {
+        require Net::Rendezvous::Publish;
+        Net::Rendezvous::Publish->new;
+    };
+    if ($publisher) {
+        $publisher->publish(
+            name   => $self->handle->db_uuid,
+            type   => '_prophet._tcp',
+            port   => $self->port,
+            domain => 'local',
+        );
+    } else {
+        warn 
+            "Publisher backend is not available. Install one of the ".
+            "Net::Rendezvous::Publish::Backend modules from CPAN.";
     }
-    return $self->{'app_handle'};
-}
-
-sub handle { shift->app_handle->handle }
+};
 
-# we can't moose until we sort out HTTP::Server::Simple with bonjour
-#has app_handle => ( isa => 'Prophet::App', is => 'rw');
-
-sub new {
-    my $class = shift;
+before new => sub {
     Template::Declare->init(roots => ['Prophet::Server::View']);
-    return $class->SUPER::new(@_);
-}
+};
 
-sub handle_request {
+override handle_request => sub {
     my ($self, $cgi) = validate_pos( @_, { isa => 'Prophet::Server'} ,  { isa => 'CGI' } );
     my $http_status;
     if ( my $sub = $self->can( 'handle_request_' . lc( $cgi->request_method ) ) ) {
@@ -50,7 +45,7 @@
     unless ($http_status) {
         $self->_send_404;
     }
-}
+};
 
 sub handle_request_get {
     my $self = shift;
@@ -60,7 +55,6 @@
 
     if ($p =~ qr{^/+replica/+(.*)$}) {
         my $repo_file = $1;
-        my $file_obj = file($repo_file);
         return undef unless $self->handle->can('read_file');
 
        my $content = $self->handle->read_file($repo_file);
@@ -124,6 +118,9 @@
 
 sub handle_request_post {
     my $self = shift;
+
+    return $self->_send_401 if ($self->read_only);
+
     my ($cgi) = validate_pos( @_, { isa => 'CGI' } );
     my $p = $cgi->path_info;
     if ( $p =~ m|^/records/(.*)/(.*)/(.*)| ) {
@@ -174,6 +171,12 @@
     return '200';
 }
 
+sub _send_401 {
+    my $self = shift;
+    print "HTTP/1.0 401 READONLY_SERVER\r\n";
+    return '401';
+}
+
 sub _send_404 {
     my $self = shift;
     print "HTTP/1.0 404 ENOFILE\r\n";



More information about the Bps-public-commit mailing list