A Flask-based web application that shortens URL's. The application uses a MongoDB database to store the original URLs and their corresponding short URLs, and has multiple endpoints for shortening and redirecting URLs. It supports both JSON and HTML.
- URL Shortening
- URL Redirection
- JSON and HTML Request Support
- Error Handling
- Flask
- Python 3
- Pymongo
- HTML, CSS
The application requires a MongoDB database to store the original and short URLs. One way to set this up is by using Docker with the following command:
$ docker run --name mongo-url -d -p 27017:27017 mongo:latest
Note: MongoDB 5.0+ requires a CPU with AVX support, if your cpu does not have support use mongo:4
-
Clone the repository:
$ git clone https://github.com/<repo-name>.git
(or extract a copy somebody sent you) -
Change the working directory to the cloned repository:
$ cd url-shortner
-
Install the dependencies:
$ pip install -r requirements.txt
-
Replace
db_address = "<host>:<port>"
in app.py with the address of your MongoDB database setup. -
Start the Flask server with
$ python app.py
-
(Optional) Access the web interface at
http://localhost:3000
-
/
- A GET endpoint that returns the main site page in HTML format. -
/shorten
- A POST endpoint that accepts a url parameter in the request body and returns a shortened URL in either JSON or HTML format depending on the request type.- JSON Example:
$ curl -X POST -H 'Content-Type: application/json' -d '{"url":"https://www.github.com/"}' http://localhost:3000/shorten
- JSON Example:
-
/<short_url>
- A GET endpoint that accepts a short URL as a path parameter and redirects the user to the original URL. If the short URL is not found in the database, it returns an error message in either JSON or HTML format.- JSON Example:
$ curl -X GET -H 'Content-Type: application/json' http://localhost:3000/23c73f
- JSON Example:
Note: Currently, the return URL is for localhost even if the server is remote.
.
├── app.py
├── templates
│ ├── base.html
│ └── error.html
└── static
└── style.css