This is an URL shortener service written using Python / Flask.
- Language: Python
- Framework: Flask
- IDE: PyCharm
- Created on Windows 10
The setup instructions are for Windows OS.
- Navigate to the directory of the downloaded project
- Make sure Python is installed (version >= 3.7.4)
- Open command line from the directory, and run the following commands:
- pip install virtualenv
- python -m virtualenv ./
- pip install -r requirements.txt
- python app.py
Then, open a browser of your choice and go to http://127.0.0.1:7777/
.
- Given a long URL argument as e.g.
http://127.0.0.1:7777/encode?url=https://wikipedia.org
, it returns a JSON containing a short URL of the formhttp://127.0.0.1:7777/6M0qc
corresponding to the long link provided, or an error message if the conversion could not be performed.
- Given a short URL argument as e.g.
http://127.0.0.1:7777/decode?url=http://127.0.0.1:7777/6M0qc
, it returns a JSON containing the corresponding decoded long URL, if it exists, or an error message otherwise.
- This endpoints reads in a short_url as e.g.
http://127.0.0.1:7777/redirect?url=http://127.0.0.1:7777/6M0qc
, then, similarly to the decode endpoint, it finds the corresponding long_url if has already been encoded. Then instead of just returning it, the endpoint will redirect the page to the given long_url.
The generator algorithms simply generates a sequence of random characters (lowercase letters + uppercase letters + numbers) with length specified by the num_chars variable, and then creates a string via concatenating the base_url with the encoding. This string will be stored as the encoded URL in two dictionaries (to ensure an average lookup time of O(1))