UR::Se...Server(3pm)
π UR::Service::WebServer π
okay bestie, gather round, because we're talking about UR::Service::WebServer β a PSGI-based web server, and she is lowkey iconic π«Ά (also she lives in the User Contributed Perl Documentation, iykyk)
β¨ SYNOPSIS β¨
like, here's the whole vibe in four lines:
my $s = UR::Service::WebServer(port => 4321);
$s->run( \&handle_request );
that's it. that's the post. π
πΈ DESCRIPTION πΈ
So this class implements a simple, standalone web server built on top of HTTP::Server::PSGI. The event loop is entered by calling the run() method β that's the moment she becomes the main character. π
π Properties π
hostβ the IP address to listen on for connections. The default value is'localhost'. You can changehostany time before the server is created, which is usually the first timerun()is called. so like, get your edits in early, bestie β¨portβ the TCP port to listen on for connections. The default value isundef, which means the system will pick an unused port for you.portcan also be changed any time before the server is created, usually the first timerun()is called. no cap. πserverβ holds a reference to an object thatisaHTTP::Server::PSGI. This gets automatically created the first timerun()is called. she just appears. iconic. π«cbβ holds aCODEreference used as the default request handler withinrun(). π
π¦ Methods π¦
$self->announce()β this method is called when the PSGI server is ready to accept requests. The base-class behavior is to print the listening URL onSTDOUT. Subclasses can override it to implement their own behavior. like, do your own thing, girl, we support it πmy $code = $self->file_handler_for_directory($path)β a helper method used for implementing server for files located in the directory$path. It returns aCODEref that takes 2 arguments:$env(the standard PSGI env hashref) and$pathname(a path relative to$path). It returns the standard tuple a PSGI server expects. β¨
$pathname is pre-processed by removing all occurrences of .. to keep requests within the provided $path. (yes bestie, she's doing a vibe check on your paths π
) If the requested file is not found, then it returns a 404. π
$self->run(<$cb>)β enter the request loop. If a callback is not provided torun(), then the object'scbproperty is used instead. If neither have a value, thenrun()returns immediately. (no callback, no problem, no server β she just leaves πͺ)
For each request $cb is called with one argument: the standard PSGI env hashref. π§
ngl, that..stripping thing is a security-flavored detail, so don't go thinking you can sneak out of$path. she said no. periodt. π₯
π SEE ALSO π
UR::Service::UrlRouter
and that's the whole page, bestie!! π shoutout to perl v5.32.1 and the 2022-01-17 date stamp, and to the fact that this was lovingly dropped into the User Contributed Perl Documentation β go forth and run() your little server, she's ready for her moment πππ
π¦ where this page comes from
- debian bookworm: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libur-perl 0.470+ds-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms