NAME
Limper - extremely lightweight but not very powerful web application framework
VERSION
version 0.009
SYNOPSIS
use Limper;
my $generic = sub { 'yay' };
get '/' => $generic;
post '/' => $generic;
post qr{^/foo/} => sub {
status 202, 'whatevs';
headers Foo => 'bar', Fizz => 'buzz';
'you posted something: ' . request->{body};
};
limp;
DESCRIPTION
Limper
is designed primarily to be a simple HTTP/1.1 test server in perl. It has a simple syntax like Dancer, but no dependencies at all (except for the tests, which only run if Net::HTTP::Client
is installed), unlike the dozens that Dancer pulls in. It also does little to no processing of requests nor formatting of responses. This is by design, othewise, just use Dancer. There is also no PSGI support or other similar fanciness.
It also fatpacks beautifully (at least on 5.10.1):
fatpack pack example.pl > example-packed.pl
EXPORTS
The following are all exported by default:
get post put del trace
status headers request response options hook limp
Also exportable:
note warning rfc1123date
FUNCTIONS
get
post
put
del
trace
Defines a route handler for METHOD to the given path:
get '/' => sub { 'Hello world!' };
Note that a route to match HEAD requests is automatically created as well for get
.
status
Get or set the response status, and optionally reason.
status 404;
status 401, 'Nope';
my $status = status;
my ($status, $reason) = status;
headers
Get or set the response headers.
headers Foo => 'bar', Fizz => 'buzz';
my @headers = headers;
my $headers = headers;
Note: All previously defined headers will be discarded if you set new headers.
request
Returns a HASH
of the request. Request keys are: method
, uri
, and version
. It may also contain headers
which is an ARRAY
, hheaders
which is a HASH
form of the headers, and body
.
There is no decoding of the body content nor URL paramters.
response
Returns response HASH
. Keys are status
, reason
, headers
(an ARRAY
of key/value pairs), and body
.
options
Returns options HASH
. See limp below for known options.
hook
Adds a hook at some position.
Three hooks are currently defined: after, request_handler, and response_handler.
after
Runs after all other processing, just before response is sent.
hook after => sub {
my ($request, $response) = @_;
# modify response as needed
};
request_handler
Runs when limp
is called, after only setting passed options, and returns the result instead of starting up the built-in web server. A simplified example for PSGI (including the response_handler below) is:
hook request_handler => sub {
get_psgi @_;
handle_request;
};
response_handler
Runs right after the after hook, and returns the result instead of using the built-in web server for sending the response. For PSGI, this is:
hook response_handler => sub {
[ response->{status}, response->{headers}, ref response->{body} ? response->{body} : [response->{body}] ];
};
limp
Starts the server. You can pass it the same options as IO::Socket::INET takes. The default options are:
Listen => SOMAXCONN, ReuseAddr => 1, LocalAddr => 'localhost', LocalPort => 8080, Proto => 'tcp'
In addition, the first argument can be a HASH
to pass other settings:
limp({debug => 1, timeout => 60, workers => 10}, LocalAddr => '0.0.0.0', LocalPort => 3001);
Default debug is 0
, default timeout is 5
(seconds), and default workers is 10
. A timeout of 0
means never timeout.
This keyword should be called at the very end of the script, once all routes are defined. At this point, Limper takes over control.
ADDITIONAL FUNCTIONS
note
warning
Log given list to STDOUT or STDERR. Prepends the current local time in format "YYYY-MM-DD HH:MM:SS".
rfc1123date
Returns the current time or passed timestamp as an HTTP 1.1 date (RFC 1123).
COPYRIGHT AND LICENSE
Copyright (C) 2014 by Ashley Willis <ashley+perl@gitable.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.12.4 or, at your option, any later version of Perl 5 you may have available.