/+ (link shortener)

The link shortener allows sending an URL to FYLR and get a short url back. The short URL can be used to access the resource of the longer URL.

Subrouter: /+/

The returned short codes do never contain one of the following letters for preventing typos: ‘o’,‘O’,‘i’,‘I’. Whenever you see a letter looking like that consider it being number ‘1’ or number ‘0’

fylr.yml

link_shortener:
  domain:                   // protocol + domain for the short_url
  default_expire_days: 200  //  amount of days after a link expires
  min_length: 4             // minimum lenght of the short url, default: 0 (none)
  max_length: 10            // max length of the short url, default: 0 (none)
  db: "shortLinkStorage.db" // path to the sqlite database (relative to binary or absolute)
  allowed_urls: ["",""]     // the urls that are allowed for the redirect_urls. Wildcard '*' supported

PUT /+

This creates a shortened URL for the given url. The short URL will be valid for the given amount of days. After that, a “Not found” will be responded for that URL.

Request

Content-Type: application/json; charset=utf-8

Body

{
    // expire_days is optional
    "expire_days": 5,
    // Lenght is optional -  if not given the server will use max_length from the config
    "length": 5,
    "redirect_url": <url>
}

Response

Header
Status: 200
Content-Type: application/json; charset=utf-8
Body
{
    "status": "created",
    "expires_time": "<date time of expiration>",
    "created_time": "<time created>",
    "short_url": "<short url>", // 5 characters
    "redirect_url": "<redirect_url>"
}

Response Errors

This error occurs if the redirect_url is empty.

Header
Status: 406
Body
{
    "status": "fylr.link_shortener.error.redirect_url_is_empty"
}

This error occurs if the redirect_url has an invalid prefix (if prefix is not in allowed_urls).

Header
Status: 406
Body
{
    "status": "fylr.link_shortener.error.redirect_url_is_invalid"
}

**This error occurs if no unique code could get be generated (max tries 200 times) **

Header
Status: 417
Body
{
    "status": "fylr.link_shortener.error.no_unique_key_available"
}

This error occurs if the given length is invalid (< min_lenght or > max_length)

Header
Status: 406
Body
{
    "status": "fylr.link_shortener.error.invalid_length_given"
}

GET /+/shortened url

This returns a HTTP header redirecting the browser to the stored URL, if it has not expired.

Header

Location: <redirect_url>
// No Cache headers needed here!

Body

{
    "expires_timestamp": "<date time of expiration>",
    "created_timestamp": "<time created>",
    "short_url": "<short url>"
    "redirect_url": "<redirect_url>"
}

If the URL was not found or is expired.

Response Error

Not found
Header
Status: 404 Not found
Body

Fylr Error Page

Expired
Status: 410 Gone
Body

Fylr Error Page

OPTIONS /+

The OPTIONS request returns an unlimited origin header.

Header

Access-Control-Allow-Methods: GET, PUT, OPTIONS
Access-Control-Allow-Origin: *

Body

The body of this response is empty.

Implementation Details

The sqlite database looks like follows:

CREATE TABLE short_links (
    expires_time INTEGER NOT NULL,  -- UNIX time in seconds
    redirect_url TEXT NOT NULL,     -- Fully qualified URL
    short_code TEXT NOT NULL,       -- short path after domain+~
    UNIQUE(short_path),
    UNIQUE(short_code),
    PRIMARY KEY(short_code)
);

Gorm is used or database handling