UR::Se...Server(3pm)

πŸ“¦
version 0.470 Β· 2 versions
, in debian bookworm, ubuntu jammy, ubuntu noble

πŸ’– 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 change host any time before the server is created, which is usually the first time run() is called. so like, get your edits in early, bestie ✨
  • port β€” the TCP port to listen on for connections. The default value is undef, which means the system will pick an unused port for you. port can also be changed any time before the server is created, usually the first time run() is called. no cap. 🌈
  • server β€” holds a reference to an object that isa HTTP::Server::PSGI. This gets automatically created the first time run() is called. she just appears. iconic. πŸ’«
  • cb β€” holds a CODE reference used as the default request handler within run(). πŸ“

πŸ¦‹ 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 on STDOUT. 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 a CODE ref 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 to run(), then the object's cb property is used instead. If neither have a value, then run() 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