root/lang/perl/HTTP-Engine/branches/lazy_request/lib/HTTP/Engine.pm @ 13462

Revision 13462, 5.0 kB (checked in by tokuhirom, 5 years ago)

merge to lazy_request branch.

Line 
1package HTTP::Engine;
2use Moose;
3use HTTP::Engine::Types::Core qw( Interface );
4our $VERSION = '0.0.10';
5use HTTP::Engine::Context;
6use HTTP::Engine::Request;
7use HTTP::Engine::Request::Upload;
8use HTTP::Engine::Response;
9use HTTP::Engine::RequestProcessor;
10
11has 'interface' => (
12    is      => 'ro',
13    does    => Interface,
14    coerce  => 1,
15    handles => [ qw(run load_plugins) ],
16);
17
18sub import {
19    my($class, %args) = @_;
20    return unless $args{middlewares} && ref $args{middlewares} eq 'ARRAY';
21    $class->load_middlewares(@{ $args{middlewares} });
22}
23
24sub load_middlewares {
25    my ($class, @middlewares) = @_;
26    for my $middleware (@middlewares) {
27        $class->load_middleware( $middleware );
28    }
29}
30
31sub load_middleware {
32    my ($class, $middleware) = @_;
33
34    my $pkg;
35    if (($pkg = $middleware) =~ s/^(\+)//) {
36        Class::MOP::load_class($pkg) or die $@;
37    } else {
38        $pkg = 'HTTP::Engine::Middleware::' . $middleware;
39        unless (eval { Class::MOP::load_class($pkg) }) {
40            $pkg = 'HTTPEx::Middleware::' . $middleware;
41            Class::MOP::load_class($pkg);
42        }
43    }
44
45    if ($pkg->meta->has_method('setup')) {
46        $pkg->setup();
47    }
48
49    if ($pkg->meta->has_method('wrap')) {
50        HTTP::Engine::RequestProcessor->meta->add_around_method_modifier(
51            call_handler => $pkg->meta->get_method('wrap')->body
52        );
53    }
54}
55
561;
57__END__
58
59=for stopwords middlewares Middleware middleware nothingmuch
60
61=encoding utf8
62
63=head1 NAME
64
65HTTP::Engine - Web Server Gateway Interface and HTTP Server Engine Drivers (Yet Another Catalyst::Engine)
66
67=head1 SYNOPSIS
68
69  use HTTP::Engine;
70  my $engine = HTTP::Engine->new(
71      interface => {
72          module => 'ServerSimple',
73          args   => {
74              host => 'localhost',
75              port =>  1978,
76          },
77          request_handler => 'main::handle_request',# or CODE ref
78      },
79  );
80  $engine->run;
81
82  use Data::Dumper;
83  sub handle_request {
84      my $c = shift;
85      $c->res->body( Dumper($c->req) );
86  }
87
88
89=head1 CONCEPT RELEASE
90
91Version 0.0.x is a concept release, the internal interface is still fluid.
92It is mostly based on the code of Catalyst::Engine.
93
94=head1 DESCRIPTION
95
96HTTP::Engine is a bare-bones, extensible HTTP engine. It is not a
97socket binding server.
98
99The purpose of this module is to be an adaptor between various HTTP-based
100logic layers and the actual implementation of an HTTP server, such as,
101mod_perl and FastCGI.
102
103Internally, the only thing HTTP::Engine will do is to prepare a
104HTTP::Engine::Request object for you to handle, and pass to your handler's
105C<TBD> method. In turn your C<TBD> method should return a fully prepared
106HTTP::Engine::Response object.
107
108HTTP::Engine will handle absorbing the differences between the environment,
109the I/O, etc. Your application can focus on creating response objects
110(which is pretty much what your typical webapp is doing)
111
112=head1 INTERFACES
113
114Interfaces are the actual environment-dependent components which handles
115the actual interaction between your clients and the application.
116
117For example, in CGI mode, you can write to STDOUT and expect your clients to
118see it, but in mod_perl, you may need to use $r-E<gt>print instead.
119
120Interfaces are the actual layers that does the interaction. HTTP::Engine
121currently supports the following:
122
123# XXX TODO: Update the list
124
125=over 4
126
127=item HTTP::Engine::Interface::ServerSimple
128
129=item HTTP::Engine::Interface::FastCGI
130
131=item HTTP::Engine::Interface::CGI
132
133=item HTTP::Engine::Interface::Test
134
135for test code interface
136
137=item HTTP::Engine::Interface::ModPerl
138
139experimental
140
141=item HTTP::Engine::Interface::Standalone
142
143old style
144
145=back
146
147Interfaces can be specified as part of the HTTP::Engine constructor:
148
149  my $interface = HTTP::Engine::Interface::FastCGI->new(
150    handler => ...
151  );
152  HTTP::Engine->new(
153    interface => $interface
154  )->run();
155
156Or you can let HTTP::Engine instantiate the interface for you:
157
158  HTTP::Engine->new(
159    interface => {
160      module => 'FastCGI',
161      args   => {
162        handler => ...
163      }
164    }
165  )->run();
166
167=head1 MIDDLEWARES
168
169For all non-core middlewares (consult #codrepos first), use the HTTPEx::
170namespace. For example, if you have a plugin module named "HTTPEx::Middleware::Foo",
171you could load it as
172
173  use HTTP::Engine middlewares => [ qw( +HTTPEx::Plugin::Foo ) ];
174
175=head1 METHODS
176
177=over 4
178
179=item load_middleware(middleware)
180
181=item load_middlewares(qw/ middleware middleware /)
182
183Loads the given middleware into the HTTP::Engine.
184
185=back
186
187=head1 AUTHOR
188
189Kazuhiro Osawa E<lt>ko@yappo.ne.jpE<gt>
190
191Daisuke Maki
192
193tokuhirom
194
195nyarla
196
197marcus
198
199hidek
200
201dann
202
203typester (Interface::FCGI)
204
205lopnor
206
207nothingmuch
208
209=head1 SEE ALSO
210
211wiki page L<http://coderepos.org/share/wiki/HTTP%3A%3AEngine>
212
213L<Moose>
214
215=head1 REPOSITORY
216
217  svn co http://svn.coderepos.org/share/lang/perl/HTTP-Engine/trunk HTTP-Engine
218
219HTTP::Engine's Subversion repository is hosted at L<http://coderepos.org/share/>.
220patches and collaborators are welcome.
221
222=head1 LICENSE
223
224This library is free software; you can redistribute it and/or modify
225it under the same terms as Perl itself.
226
227=cut
Note: See TracBrowser for help on using the browser.