The Perfect Canadian

As I thought about my grandmother’s life, I was struck by what a perfect Nova Scotian and Canadian she was. Not in a cultural sense, but rather as a person who loved and served her community.

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




A URL shortener service in Deno

URL shortening is one of the commonly used web app. Shortening a URL & then redirecting the caller fully suits the async model of runtimes like Deno. This is because shortening & redirecting isn’t a CPU intensive operation.

The features of the URL shortener service are:

The URL shortener service has two endpoints:

To shorten a URL, POST /shorten?target=<normal-url> need to be used. As shown in the URL, a mandatory query param target needs to be passed.

The shorten URL API returns the shortened URL in the response body:

To redirect from a shortened URL to original URL, GET /short-url need to be used. This is the URL that was returned in the response of the POST /shorten API call.

The response to the GET /short-url is redirection using 302 to the original URL.

The URL shortener service is structured in the usual way:

For shortening a URL, the unique ID & original URL mapping is stored in the localStorage instead of a database. The localStorage is a simple KV storage that internally uses SQLite db. This is sufficient for our needs. For more details on localStorage, visit the article here.

The server checks the sandbox access, initializes the listener, and accepts the HTTP connection. It uses native HTTP server (hyper based).

The router is very simple. As there are two simple endpoints, there is no need for a router framework. The router checks the request URL and method, and then directs the request to the appropriate function of the controller. For no match, the router responds with either 404 or 405.

For shortening a URL, the controller gets the target URL and calls the add function provided by the service. For processing a short URL, the controller gets the short URL and calls the get function provided by the service. Upon receiving a response from the service, the controller sends an appropriate HTTP response to the caller.

The service provides two functions: get and add. The add function generates an ID and stores ID -> target mapping in the localStorage. The get function retrieves the target from the localStorage using ID. There is no delete function (usually there isn’t).

To support all the above, there are some utilities & constants.

The URL shortener service can be started with the following command:

It needs permission to allow serving sockets on port 80, and it also needs a mandatory command line parameter — location. The location is useful in keying the localStorage.

Here are some curl commands to test the service: Some are about shortening a target, while others are about accessing a shortened URL.

Let’s see it in action in the browser:

That’s all about URL shortener service. Here are some other services built in Deno:

Add a comment

Related posts:

Colendi Announces Tech Paper

Colendi proudly to present the first version of Colendi Tech Paper, which encompasses the problem at hand and the Colendi vision as well as the basic technological features with detailed…

Reality

Twenty nine years in alleged reality with nothing to show for it but what I can see in front of me, And a bunch of stories I can’t even repeat or can’t be bothered trying to complete, Because I’m…

Traxalt y bitfoliex unen fuerzas para ayudar a fortalecer su comunidad

Traxalt ha creado un Programa de Referidos para regalar Traxalt y fortalecer su comunidad antes de que Traxalt sea lanzado al mercado. Con este programa, podrás recomendar e invitar a tus amigos a…