URL shortener: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
mNo edit summary
Line 1: Line 1:
{{draft task}}[[Category:Networking and Web Interaction]]
{{draft task}}[[Category:Networking and Web Interaction]]


The job of a [https://en.wikipedia.org/wiki/URL_shortening URL shortener] is to take a long URL (e.g. "https://www.cockroachlabs.com/docs/stable/build-a-go-app-with-cockroachdb.html") and turn it into something shorter (e.g. "https://bit.ly/2QLUQIu").
Create a simple URL shortener to demonstrate your programming language's web API capabilities with the following endpoints:

A simple URL shortener with no special rules is very simple and consists of 2 endpoints:

* One to generate a short version of a URL given a long version of a URL.
* One to handle a call to the short version of a URL and redirect the user to the long (original) version of the URL.
<br />
Create a simple URL shortening API with the following endpoints:


'''POST /'''
'''POST /'''


A POST endpoint that accepts a JSON body describing the URL to shorten. Your URL shortener should generate a short version of the URL (like https://bitly.com/), keep track of the mapping between short and long URLs, and return the short version to the user. For example:
A POST endpoint that accepts a JSON body describing the URL to shorten. Your URL shortener should generate a short version of the URL and keep track of the mapping between short and long URLs. For example:


$ curl --location --request POST 'localhost:3000' \
$ curl -X POST 'localhost:8080' \
-H 'Content-Type: application/json' \
-H 'Content-Type: application/json' \
-d '{
-d '{
"long": "https://google.com"
"long": "https://www.cockroachlabs.com/docs/stable/build-a-go-app-with-cockroachdb.html"
}'
}'

http://localhost:3000/9eXmFnuj
Should returning something similar to:

http://localhost:8080/9eXmFnuj


'''GET /:short'''
'''GET /:short'''
Line 19: Line 29:


$ curl -L http://localhost:3000/9eXmFnuj
$ curl -L http://localhost:3000/9eXmFnuj

<!doctype html><html...
Should redirect the user to the original URL:

<!DOCTYPE html>
<html lang="en">
...


'''Rules''':
'''Rules''':

Revision as of 12:35, 5 January 2020

URL shortener is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The job of a URL shortener is to take a long URL (e.g. "https://www.cockroachlabs.com/docs/stable/build-a-go-app-with-cockroachdb.html") and turn it into something shorter (e.g. "https://bit.ly/2QLUQIu").

A simple URL shortener with no special rules is very simple and consists of 2 endpoints:

  • One to generate a short version of a URL given a long version of a URL.
  • One to handle a call to the short version of a URL and redirect the user to the long (original) version of the URL.


Create a simple URL shortening API with the following endpoints:

POST /

A POST endpoint that accepts a JSON body describing the URL to shorten. Your URL shortener should generate a short version of the URL and keep track of the mapping between short and long URLs. For example:

   $ curl -X POST 'localhost:8080' \
   -H 'Content-Type: application/json' \
   -d '{
       "long": "https://www.cockroachlabs.com/docs/stable/build-a-go-app-with-cockroachdb.html"
   }'

Should returning something similar to:

   http://localhost:8080/9eXmFnuj

GET /:short

A GET endpoint that accepts a short version of the URL in the URL path and redirects the user to the original URL. For example:

   $ curl -L http://localhost:3000/9eXmFnuj

Should redirect the user to the original URL:

   <!DOCTYPE html>
   <html lang="en">
   ...

Rules:

  • Store the short -> long mappings in any way you like. In-memory is fine.
  • There are no auth requirements. Your API can be completely open.

Crystal

<lang ruby>require "kemal"

CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".chars entries = Hash(String, String).new

post "/" do |env|

 short = Random::Secure.random_bytes(8).map{|b| CHARS[b % CHARS.size]}.join
 entries[short] = env.params.json["long"].as(String)
 "http://localhost:3000/#{short}"

end

get "/:short" do |env|

 if long = entries[env.params.url["short"]]?
   env.redirect long
 else
   env.response.status_code = 404
 end

end

error 404 do

 "invalid short url"

end

Kemal.run</lang>