root/lang/perl/HTTP-Engine/trunk/lib/HTTP/Engine/Role/Interface.pm @ 18231

Revision 18231, 2.2 kB (checked in by tokuhirom, 6 years ago)

merge interface-decrare branch

Line 
1package HTTP::Engine::Role::Interface;
2use strict;
3use Moose::Role;
4use HTTP::Engine::Types::Core qw( Handler );
5use HTTP::Engine::ResponseFinalizer;
6
7requires 'run';
8
9has request_handler => (
10    is       => 'rw',
11    isa      => Handler,
12    coerce   => 1,
13    required => 1,
14);
15
16sub handle_request {
17    my ($self, %args) = @_;
18
19    my $req = HTTP::Engine::Request->new(
20        request_builder => $self->request_builder,
21        %args,
22    );
23
24    my $res;
25    eval {
26        $res = $self->request_handler->($req);
27        unless ( Scalar::Util::blessed($res)
28            && $res->isa('HTTP::Engine::Response') )
29        {
30            die "You should return instance of HTTP::Engine::Response.";
31        }
32    };
33    if ( my $e = $@ ) {
34        print STDERR $e;
35        $res = HTTP::Engine::Response->new(
36            status => 500,
37            body   => 'internal server errror',
38        );
39    }
40
41    HTTP::Engine::ResponseFinalizer->finalize( $req => $res );
42
43    return $self->response_writer->finalize( $req => $res );
44}
45
461;
47
48__END__
49
50=head1 NAME
51
52HTTP::Engine::Role::Interface - The Interface Role Definition
53
54=head1 SYNOPSIS
55
56  package HTTP::Engine::Interface::CGI;
57  use Moose;
58  with 'HTTP::Engine::Role::Interface';
59
60=head1 DESCRIPTION
61
62HTTP::Engine::Role::Interface defines the role of an interface in HTTP::Engine.
63
64Specifically, an Interface in HTTP::Engine needs to do at least two things:
65
66=over 4
67
68=item Create a HTTP::Engine::Request object from the client request
69
70If you are on a CGI environment, you need to receive all the data from
71%ENV and such. If you are running on a mod_perl process, you need to muck
72with $r.
73
74In any case, you need to construct a valid HTTP::Engine::Request object
75so the application handler can do the real work.
76
77=item Accept a HTTP::Engine::Response object, send it back to the client
78
79The application handler must return an HTTP::Engine::Response object.
80
81In turn, the interface needs to do whatever necessary to present this
82object to the client. In a  CGI environment, you would write to STDOUT.
83In mod_perl, you need to call the appropriate $r->headers methods and/or
84$r->print
85
86=back
87
88=head1 AUTHORS
89
90Kazuhiro Osawa and HTTP::Engine Authors
91
92=head1 SEE ALSO
93
94L<HTTP::Engine>
95
96=cut
Note: See TracBrowser for help on using the browser.