Archive

Posts Tagged ‘mod_rewrite’

RemotePi REST

December 3rd, 2014 subogero Comments off

I’m constantly bombarded with RESTful stuff at work. So much so, that I’ve started rewriting RemotePi as a REST API. Lots of learning and reading, then.

As usual most of the literature was written by people who studied computer science. They just love abstractions. Solving problems is OK, as long as they get their daily hit of abstractions. So there are resources, verbs and routers. Very nice.

You’re supposed to refer to a resource without ugly queries in the URL. Instead, it should be just http://pi/remotepi/home/ for the media root directory. But how the hell does such an URL generate a query that can be processed by a CGI or FastCGI app?

The computer scientists are silent on the issue.

So I had to dig deeper and discover mod_rewrite of Apache2. There is a lot in a name. It rewrites incoming URL requests and is able to turn them into proper queries. Thank you Ludovico Fischer.

So here is my “resource router”.

First enabled mod_rewrite:

a2enmod rewrite
service apache2 restart

And the router config in /etc/apache/sites-available/default

RewriteEngine On
RewriteRule /remotepi/(.+) /remotepi/api.pl?$1

Very Perly, ain’t it? It’s because it uses the Perl regex engine.