Recently, I've been working on a way to serve HTTP with IO::Async. For a while it's been possible to use HTTP as a client with Net::Async::HTTP, so it seemed only natural to provide a server named Net::Async::HTTP::Server.
I've tried to design the interface fairly similar to some of the other server modules, most notably Net::Async::FastCGI. This allows for similar code to be written one way or the other, for responding via HTTP or FastCGI.
use Net::Async::HTTP::Server; use IO::Async::Loop; use HTTP::Response; my $loop = IO::Async::Loop->new(); my $httpserver = Net::Async::HTTP::Server->new( on_request => sub { my $self = shift; my ( $req ) = @_; my $response = HTTP::Response->new( 200 ); $response->add_content( "Hello, world!\n" ); $response->content_type( "text/plain" ); $req->respond( $response ); }, ); $loop->add( $httpserver ); $httpserver->listen( addr => { family => "inet6", socktype => "stream", port => 8080 }, on_listen_error => sub { die "Cannot listen - $_[-1]\n" }, ); $loop->run;As with Net::Async::FastCGI the distribution also includes modules to support hosting a PSGI application via Plack. The Plack::Handler module in particular allows the use of Net::Async::HTTP::Server directly from the plackup commandline:
plackup -s Net::Async::HTTP::Server --listen ":8080" application.psgi
It's still in somewhat of an early state - while it supports sending streaming responses (as required by PSGI) it doesn't yet support streaming receiving of requests. That shouldn't be too hard to add, but so far I haven't found a need to add it yet.
No comments:
Post a Comment