The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

HTTP::Server::EV::ServeStatic - Simple static file server.

DESCRIPTION

Simple static file server for HTTP::Server::EV. This module good for testing apps without frontend server. It's blocking and reads entire file into memory, so if you going to serve big files and need good performance - use frontend servers like nginx or lighttpd.

SYNOPSIS

use HTTP::Server::EV;
use HTTP::Server::EV::ServeStatic;

my $static_server = HTTP::Server::EV::ServeStatic->new({
	basedir 		=> './html',
	indexes			=> [qw/index.html index.xhml default.html/],
	forbidden_ext	=> [qw/pm pl php conf ini/],
	rewrite_cb		=> sub {
		local($_) = @_;
		
		s/foo/bar/; # /path/foo/file.txt -> /path/bar/file.txt
		
		return $_;
	}
});
# or defaults
my $static_server = HTTP::Server::EV::ServeStatic->new;


HTTP::Server::EV->new->listen( 8080 , sub{
	my ($cgi) = @_;
	$static_server->serve($cgi); # try serve file. 
	
	# or give control to your app if file not found
	
	$cgi->header({ STATUS => '404 Not Found' });
	$cgi->print('404 - File not found');
});

EV::run;

METHODS

new( { options } )

basedir

Directory with files. Dafault './'

indexes

Files to search for if no file specified in url. Arrayref. Default [qw/index.html/]

forbidden_ext

Don't serve files with those extensions. Arrayref. Default [qw/pm pl/]

rewrite_cb

Callback for rewriting uri before processing request. Gets uri as first arg and need to return new uri.

serve( $http_server_ev_cgi_obj )

Process request