7

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?

uxer
  • 521
  • 3
  • 10
  • The Perl6/Raku module I recall is Bailador https://github.com/Bailador/Bailador . There's also a book on https://leanpub.com/ . – jubilatious1 Jun 19 '22 at 04:43
  • 1
    @jubilatious1 Bailador seems to serve the purpose of building your apps, not for ad-hoc serving a directory – uxer Jun 19 '22 at 14:43

3 Answers3

7

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.

Dave Cross
  • 68,119
  • 3
  • 51
  • 97
  • 1
    BTW, there are also: [`ftp_this`](https://metacpan.org/dist/App-FTPThis/view/script/ftp_this), [`cgi_this`](https://metacpan.org/dist/App-CGIThis/view/bin/cgi_this), [`dav_this`](https://metacpan.org/dist/App-DAVThis/view/script/dav_this) and an umbrella for all of these: [`Task::FooThis`](https://metacpan.org/pod/Task::FooThis) – uxer Jul 06 '22 at 15:09
7

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

librasteve
  • 6,832
  • 8
  • 30
  • 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...? – librasteve Jun 22 '22 at 19:49
  • 2
    OK I give in here's the one-liner ```raku -e 'shell("zef install --/test cro && cro stub http hello hello && cro run")';``` ;-) – librasteve Jun 22 '22 at 19:57
  • "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` https://github.com/bduggan/p6-digest-sha1-native/issues/18 – uxer Jul 06 '22 at 14:52
  • 1
    @uxer appreciate the feedback... – librasteve Jul 07 '22 at 12:01
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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
  • The 1st link I mentioned notes that `Mojolicious::Lite` doesn't serve directory contents, indeed: https://stackoverflow.com/q/64206232/14812514 - so in my hope for an all-round solution I omitted the module. – uxer Jun 22 '22 at 12:10
  • There are plugins for that sort of thing. – brian d foy Jun 22 '22 at 12:27
  • 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. Pity – uxer Jun 24 '22 at 16:34
  • "There are plugins for that sort of thing." - the one-liner gonna get even less succinct... – uxer Jul 06 '22 at 15:00