A simple URL shortener built with Go, Gin, and MongoDB. This project allows users to convert long URLs into shorter, easily shareable links. The project is live and deployed on url.tanelt.com.
- Generate short URLs for any valid long URL
- Retrieve and redirect to the original URL using the short URL
- Prevent duplicate entries for the same long URL
- Simple and responsive HTML form for URL submission
- Go: Backend language
- Gin: Web framework for routing and request handling
- MongoDB: Database for storing URLs and short codes
- Go 1.17+ installed
- MongoDB (local or cloud instance)
- Git (for cloning the repository)
- Docker (optional, for containerization)
-
Clone the repository:
git clone https://github.com/luqmanshaban/go-url-shortener.git cd go-url-shortener
-
Install dependencies:
go mod tidy
-
Create a
.env
file in the root directory to store environment variables (see Environment Variables section). -
Start the server:
go run main.go
The server should now be running on http://localhost:4000
.
To run this project, create a .env
file in the root directory and add the following:
MONGODB_URI=mongodb://your_mongodb_uri_here
- GET / - Renders the HTML form for submitting a long URL.
-
POST /submit
- Description: Accepts a long URL, generates a short URL, and saves it to the database.
- Body:
url=<long_url>
- Response:
{ "ShortUrl": "http://url.tanelt.com/<short_url_code>" }
-
GET /url/:shortUrl
- Description: Redirects to the original URL based on the provided
shortUrl
. - Params:
shortUrl
- The generated short code. - Response: Redirects to the original URL if found, otherwise returns a
404
error.
- Description: Redirects to the original URL based on the provided
.
├── main.go # Main application file
├── go.mod # Go module dependencies
├── static/
│ └── form.html # HTML form for URL input
└── README.md # Project documentation
This project is licensed under the MIT License. See the LICENSE file for more details.