Skip to content

Commit

Permalink
use Plack as PSGI implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
dann committed Sep 7, 2009
1 parent 70fc0aa commit 7d41efc
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 24 deletions.
6 changes: 1 addition & 5 deletions angelos-core/Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ requires(
'Data::Util' => 0,
'Class::Singleton' => 0,
'CHI' => '0.10',
'Plack' => 0,

# Angelos Plugins
'Text::SimpleTable' => 0,
Expand Down Expand Up @@ -92,11 +93,6 @@ requires(
'Locale::Maketext::Simple' => 0,
'Locale::Maketext::Extract' => 0,

# testing
'HTTP::Request::Common' => 0,
'Test::Class' => 0,
'Test::Base' => 0,

# View Plugins (remove from core)

# Core Middleware
Expand Down
2 changes: 2 additions & 0 deletions angelos-core/lib/Angelos.pm
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ sub setup_config {
sub create_config {
my $self = shift;
my $config_class = join '::', ( ref $self, 'Config' );
use Data::Dumper;
warn Dumper $config_class;
$config_class->require;
$config_class->instance;
}
Expand Down
4 changes: 3 additions & 1 deletion angelos-core/lib/Angelos/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use base 'Class::Singleton';
sub _new_instance {
my $class = shift;
my $self = bless {}, $class;
use Data::Dumper;
warn $class;
$self->{config} = Angelos::Config::Loader->load( $self->config_file_path,
Angelos::Config::Schema->config );
return $self;
Expand All @@ -18,7 +20,7 @@ sub _new_instance {
sub config_file_path {
my ( $self, $environment ) = @_;
Angelos::Exception::AbstractMethod->throw(
message => 'Sub class must implement config_file method' );
message => 'Sub class must implement config_file_path method' );
}

sub global {
Expand Down
3 changes: 2 additions & 1 deletion angelos-core/lib/Angelos/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package Angelos::Engine;
use Angelos::Class;
use Carp ();
use Scalar::Util ();
use HTTP::Engine::Response;
use Angelos::Dispatcher;
use Angelos::Middleware::Builder;
use Angelos::Exceptions;
Expand Down Expand Up @@ -51,6 +50,8 @@ sub handle_request {
if ( my $e = Exception::Class->caught() ) {
$self->HANDLE_EXCEPTION($e);
}
use Data::Dumper;
warn Dumper $self->app->res;
return $self->app->res;
}

Expand Down
36 changes: 19 additions & 17 deletions angelos-core/lib/Angelos/Engine/Base.pm
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package Angelos::Engine::Base;
use Angelos::Class;
use Carp ();
use HTTP::Engine;
use Angelos::Exceptions;
use Angelos::PSGI::Engine;
use Angelos::Request;

with 'Angelos::Class::Loggable';

has 'engine' => (
is => 'rw',
isa => 'HTTP::Engine',
handles => [qw(run)],
);

Expand Down Expand Up @@ -66,40 +66,42 @@ sub build_engine {
my $request_handler = $self->request_handler;
$request_handler ||= $self->build_request_handler;

return HTTP::Engine->new(
return Angelos::PSGI::Engine->new(
interface => {
module => $self->server,
args => {
host => $self->host,
port => $self->port,
root => $self->root,
},
request_handler => $request_handler,
psgi_handler => $request_handler,
},

);
}


sub build_request_handler {
my $self = shift;

my $request_handler = Angelos::Middleware::Builder->new->build(
sub { my $req = shift; $self->handle_request($req) } );
my $request_handler_with_context = sub {
my $req = shift;
my $res = HTTP::Engine::Response->new;
# FIXME implement Angelos::Middleware::Builder
my $request_handler = sub {
my $env = shift;
my $req = Angelos::Request->new($env);
my $res = Angelos::Response->new;
my $c = $self->create_context( $req, $res );

use Devel::MemUsed;
my $memused = Devel::MemUsed->new;

no warnings 'redefine';
local *Angelos::Registrar::context = sub {$c};

$self->log->info(sprintf( "MEMORY: %08s", $memused ) . "\n");
$request_handler->($req);
$res = $self->handle_request($req);
my $psgi_res = $res->finalize;

# hmmmmmmm
$psgi_res->[1] = [ %{ $psgi_res->[1] } ]
if ref( $psgi_res->[1] ) eq 'HASH';
$psgi_res->[2] = [ $psgi_res->[2] ] unless ref( $psgi_res->[2] );
return $psgi_res;
};
$request_handler_with_context;
return $request_handler;
}

sub handle_request {
Expand Down
15 changes: 15 additions & 0 deletions angelos-core/lib/Angelos/PSGI/Engine.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package Angelos::PSGI::Engine;
use Mouse;
use Angelos::Types qw( Interface );

has 'interface' => (
is => 'ro',
isa => Interface,
handles => [qw(run)],
coerce => 1,
);

__PACKAGE__->meta->make_immutable( inline_destructor => 1 );
1;


29 changes: 29 additions & 0 deletions angelos-core/lib/Angelos/PSGI/ServerGatewayBuilder.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package Angelos::PSGI::ServerGatewayBuilder;
use strict;
use warnings;

sub build {
my ( $class, $module, $args ) = @_;
if ( $module !~ s{^\+}{} ) { $module = join( '::', "Plack", "Impl", $module );
}
Mouse::load_class($module);

# FIXME need to develop standalone Plack::Impl wrapper
# looks ugly...
# we need to wrap Plack::Impl or
if ( $module eq 'Plack::Impl::ServerSimple' ) {
my $server = $module->new( $args->{port} );
$server->host( $args->{host} );
$server->psgi_app( $args->{psgi_handler} );
return $server;
}

if ( $module eq 'Plack::Impl::AnyEvent' ) {
$module->new( port => $args->{port}, host => $args->{host} );
return $module;
}
}

1;


3 changes: 3 additions & 0 deletions angelos-core/lib/Angelos/Request.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package Angelos::Request;
use base qw/Plack::Request/;
1;
4 changes: 4 additions & 0 deletions angelos-core/lib/Angelos/Response.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package Angelos::Response;
use base qw(Plack::Response);

1;
25 changes: 25 additions & 0 deletions angelos-core/lib/Angelos/Types.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Angelos::Types;
use Mouse;
use Angelos::PSGI::ServerGatewayBuilder;
use MouseX::Types - declare => [
qw(
Interface
)
];
use MouseX::Types::Mouse qw(HashRef);

subtype Interface;

coerce Interface, from HashRef, via {
my $module = $_->{module};
my $args = $_->{args};
$args->{psgi_handler} = $_->{psgi_handler};

use Data::Dumper;
warn $module;
warn Dumper $args;
my $server = Angelos::PSGI::ServerGatewayBuilder->build( $module, $args );
return $server;
};

1;

0 comments on commit 7d41efc

Please sign in to comment.