New feature in MARS: shorten your URLs!

I’ve just pushed a few commits to MARS (https://github.com/andrea-magni/MARS) repository and there is a new feature in the library now.

Nowadays, for a number of different reasons, you may find yourself trying to keep your endpoint’s URLs as short as possible. It is quite usual to see urls like https://youtu.be/[id], a shorter version of https://www.youtube.com/watch?v=[id]. This may sound a bit overshooting as we are as well used to very long URLs and it may sound useless to further shorten an already short URL. However, starting today, you can setup your MARS REST server to respond to URLs in the form of:

http://hostname/r

Formerly, you were forced to have an additional layer thus the shorter URL form was:

http://hostname/e/r

This may seem trivial at first sight but, keeping in mind how MARS organizes and select for execution the registered resources, it is not. Endpoints are determined by a hierarchical structure.

Considering the standard URL http://hostname/rest/default/helloworld, we have the following structure:

  • Engine (/rest)
    • Application (/default)
      • Resource (helloworld)
        • Methods (GET/POST/PUT… + SubPaths)

(Remember that you can easily bootstrap a new MARS project through the Demos\MARSTemplate project and/or using the MARSCmd utility, included in the repository)

A MARS server application can hold multiple engines and each engine can contain multiple applications. Each application then can have a collection of resources, each one with its methods exposed. The MARS library (TMARSActivation specifically) selects with class to instantiate and then which method to invoke by parsing the URL. So, if we want to shorten the URL to the maximum, we need to provide a second strategy to select the correct engine/application/resource/method.

Starting from a fresh new project (see MARSTemplate), we have to make the following changes (all in Server.Ignition unit):

  1. set the BasePath property of our Engine to empty string;
  2. set the BasePath property of our Application to empty string;
  3. provide an OnGetApplication event handler (through an anonymous method);
  4. (optional) set a default resource path for our Application.

In the following snippet (extracted from Server.Ignition), I’ve highlighted all the above steps.

Basically, we are saying (with the OnGetApplication event handler) that when the library is not able to determine which Application to address by looking at the request’s URL, we want to use LMyApp (the DefaultApp) instead. (Of course you may want to setup more complicated strategies but that’s the idea).

In the following screenshot you can see an example request made with Postman:

Just as aside notes:

  • I’ve added a couple of shortcuts to address a specific Application in the OnGetApplication event handler: look for FEngine.ApplicationByName() and FEngine.ApplicationByBasePath() methods;
  • Specifying the DefaultResourcePath means every call that would result in a “Resource [xyz] not found” will now be considered as targeting the specified path (of course the request still need to match method signature/decoration);
  • This new functionalities are generally available in MARS so the content of this blog post applies to MARSTemplate (Indy/WebBroker) as well as to MARSTemplateDCS (Delphi-Cross-Socket).

I am very happy MARS is getting used all around the world and I also want to mention my third sponsor, recently arrived: thanks https://github.com/ckranich 🙂

That’s all, thanks everybody!

Andrea

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.