
This is a URL_Shortener app.
You can find the demo of the app in this YouTube video.
.env variables. Each developer in the team should individually set up .env file and MUST NOT upload it to the GitHub. However, for this project and for the simplicity, this secret key is exposed to GitHub. Therefore, you can run the server.build version since it is faster and lighter. I’ve assumed I can deploy the front-end using dev resources.Each long url requires a unique key associated to it.
Q: The natural question is how we can generate this key and make sure it is unique?
A: When a pair of (long url,short url) is added to the database, a unique identifier named as id is assigned to the entry. id is also a default primary key. We can leverage this value to generate the unique key for each long url.
To generate the unique key, we can use Base62. As you may know, + and / are reserved characters in URL. Base62 is considered as a safe way to generate unique key for short URL since Base62 doesn’t produce + or /.
unique_key = base62(id)
short_url = "localhost:3000/" + unique_keyEach entry in the database is somehow a tuple.
# entry to be stored in database
entry = (id, long_url, unique_key)let’s see how many urls the database can store using the base62 approach.
The number of characters for the SMS is limited to 67. We know that 15 out of 67 chars are occupied by the domain (domain is localhost:3000/ according to the assumptions). So, we have 52 characters available for the unique key.
Since we are working with base62, the last item in the database (after that, the database is full) has a unique key with the size of 52 chars.
The largest number with the size of 52 chars in base62 can be approximately calculated to decimal using the following approach.
62 ^ 52 ~= 3.67 x 10^78As we can observe, this is a very big number (much larger than the number of atoms in the observable universe). Therefore, the short URL can be expanded up to 52 characters as well as the number of generated short URLs can be up to 3.67 * 10^78.
For running the app, you need to have Docker installed.
Please note that there is a docker client and docker server that needs to be installed.
Once the docker server is running, you can simply enter the command below to build and run the container.
docker-compose up --buildNote: Running front-end server can take up to 10 mins. The reasons are mainly:
create-react-app is a popular environment for setting up React apps. Despite the popularity, running dev server using create-react-app can be slow.dev version is being deployed to the docker. As a future work, instead of dev, we can deploy the build version which is faster and lightweight. Then, we can setup a nginx docker image for serving the front-end build release.I’ve leveraged React to develope the front-end application.
The front-end follows MVC architecture. This architecture separates Model from View from Controller for easier maintenance.
Components and are stored within Components folder.I’ve used .gitignore for preventing redundant files to be uploaded to GitHub.
The used dependencies are as follows:
I’ve used Django-Rest-Framework as the back-end web framework for developing the server.
Django requires the developer to follow MVC architecture.
I’ve used Sqllite3 for the sake of simplicity.
| id | URL | unique key |
|---|---|---|
| PK | required | required |
id = models.BigAutoField(primary_key=True)
long_url = models.URLField(max_length=2083)
unique_key = models.CharField(max_length=20)