What is a simple idiom using Cro to add (eg) `.html` to a static route?

18 views Asked by At

I want to test a website locally before uploading the html files to a server, where they will sit behind Apache with the following in a VirtualHost configuration:

RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.html [NC]

The files to be served all have .html extensions, eg.,

rendered/
   one.html
   index.html
   two.html

Suppose I use

my $app = route {
        get -> *@path {
            static 'rendered', @path,:indexes( 'index.html', );
        }
    }

Assuming CroApp is activated with port 5000, and I try to view localhost:5000/one I get, as I would expect, a 404.

So how do I mimic the Apache rewrite in a natural way?

1

There are 1 answers

1
Richard Hainsworth On

My initial thought is:

my $app = route {
   get -> *@path {
      @path[*-1] ~= ".html" 
         unless @path[0] eq '' 
                or ("rendered/" ~ @path.join('/')).IO ~~ :e & :f;
         static 'rendered', @path,:indexes( 'index.html', );
      }
   }