Perl/Raku succinct webserver one-liner?
Asked Answered
L

3

8

Are there any concise one-liners for quick serving of pages or directories if no index.html? Something like this:

python3 -m http.server

Couldn't find a Raku one-liner.
Compare Perl ones, taken from https://gist.github.com/willurd/5720255 and https://github.com/imgarylai/awesome-webservers :

plackup -MPlack::App::Directory -e 'Plack::App::Directory->new(root=>".");' -p 8000
perl -MHTTP::Server::Brick -e '$s=HTTP::Server::Brick->new(port=>8000); $s->mount("/"=>{path=>"."}); $s->start'

Install them prior to use (no additional installs with Python):

cpan Plack
cpan HTTP::Server::Brick

Plack pulls in a gajillion of dependencies so I didn't proceed with installation, and HTTP::Server::Brick doesn't install on my machine as its tests fail.

Both Perl and Raku are generally considered to be good in one-liners, and are meant to deliver DWIM: "try to do the right thing, depending on the context", "guess ... the result intended when bogus input was provided"

So I'd expect them - especially modern and rich Raku - to provide a webserver one-liner on par in simplicity with Python.
Or have I missed something?
If the feature lacks, is it planned?
If lacks and not-to-be-implemented, why?

Lory answered 18/6, 2022 at 14:34 Comment(2)
The Perl6/Raku module I recall is Bailador github.com/Bailador/Bailador . There's also a book on leanpub.com .Unsavory
@Unsavory Bailador seems to serve the purpose of building your apps, not for ad-hoc serving a directoryLory
K
8

I like http_this (https_this is also available).

There's an annoying shortcoming in that it doesn't currently support index.html - but I have a pull request pending to fix that.

On the other hand, these programs rely on Plack, so maybe you'd rather look elsewhere.

Kidding answered 18/6, 2022 at 17:1 Comment(1)
BTW, there are also: ftp_this, cgi_this, dav_this and an umbrella for all of these: Task::FooThisLory
D
8

Raku Cro needs one line to install:

zef install --/test cro

And then one to setup and run:

cro stub http hello hello && cro run

From https://cro.services/docs/intro/getstarted

Let's say you want to serve all the files in a project subdirectory e.g. hello/httpd, then tweak the standard hello/lib/Routes.pm6 file to this:

  1 use Cro::HTTP::Router;
  2 
  3 sub routes() is export {
  4     route {
  5         get -> *@path {
  6             static 'httpd', @path;
  7         }
  8     }
  9 }

cro run looks for file changes and will auto restart the server

index.html works fine

I suggest a symbolic link ln -s if your dir is outside the project tree

Dilatometer answered 20/6, 2022 at 21:12 Comment(4)
hi @uxer - while Cro is not the only option, it does (close to) what you ask (ok one line to install the module and one to setup and run) - I am curious why do not accept this as the answer...?Dilatometer
OK I give in here's the one-liner raku -e 'shell("zef install --/test cro && cro stub http hello hello && cro run")'; ;-)Dilatometer
"why not accept this as the answer": 1. The command is complicated as for the use case and not easily memorable; 2. It creates its own directory hello with files in it; 3. It doesn't display directory contents or else you need to code; 4. localhost-bound - ERR_CONNECTION_REFUSED from another machine; 5. cro stub ... doesn't start in /tmp or ~ - Failed to get the directory contents of ... Permission denied; 6. It requires installing libssl-dev on antiX 19 and it doesn't install at all on OpenBSD 7.1 github.com/bduggan/p6-digest-sha1-native/issues/18Lory
@uxer appreciate the feedback...Dilatometer
S
3

Putting aside the webserver portion of your question, Python and Perl differ in their philosophies. Both of them are perfectly fine ways of doing things, and each appeals to a different sort of crowd.

  • Python is "batteries included", so it's a heavyweight distribution of many things in its standard library. There's more right out of the box, even if you never use most of it.
  • Perl tries to distribute just enough for you to install the modules that you decide that you need. That way, you can choose something that is fresher or newer than the thing that Perl chose to distribute.

Now, for the webserver, you may like Mojolicious. It's mostly self-contained (or relies on mostly core modules) so it's an easier install. The links you mentioned have Mojolicious::Lite examples.

Sherly answered 21/6, 2022 at 15:33 Comment(4)
The 1st link I mentioned notes that Mojolicious::Lite doesn't serve directory contents, indeed: https://mcmap.net/q/1323335/-mojolicious-one-liner-to-service-status-files-with-listing-directory/14812514 - so in my hope for an all-round solution I omitted the module.Lory
There are plugins for that sort of thing.Sherly
For the completeness sake, would you be so kind to make it a 3-way comparison by adding Raku. Raku is also "more out-of-the-box", heavyweight "batteries included", at least when it comes to the language itself. What is the policy on libraries distribution? Flesh it out yourself? There is e.g. Rakudo Star Bundle, offers HTTP::Easy, one-liner-less as it seems. PityLory
"There are plugins for that sort of thing." - the one-liner gonna get even less succinct...Lory

© 2022 - 2024 — McMap. All rights reserved.