This is a URL Shortener service built with Go (Gin framework) and MongoDB. The API allows users to create short URLs, redirect to original URLs, and update stored URLs. The project is fully Dockerized for easy installation and deployment.
- Features
- Prerequisites
- Installation
- Environment Variables (.env)
- Running the Project
- API Endpoints
- Testing with Postman
- Project Structure
- License
✅ Generate short URLs from long URLs
✅ Redirect users from short URLs to original URLs
✅ Update existing short URLs with new destinations
✅ MongoDB integration for persistent data storage
✅ Dockerized for easy deployment
✅ Fast and efficient API using the Gin framework
Ensure you have the following installed before proceeding:
- Docker - Download & Install
- Docker Compose - Install Docker Compose
- (Optional) Go 1.22+ (if you want to run the project without Docker) - Download Go
git clone https://github.com/sunilyadav97/url-shortener-api.git
cd url-shortener-apiIf you haven't initialized a Git repository yet, you can create a new folder and add all the files manually.
The .env file contains environment variables that configure the application. Create a .env file in the project's root directory:
touch .envNow, open the .env file and add the following configuration:
# Server Configuration
PORT=8080 # Port where the API will run
# MongoDB Configuration
MONGO_URI=mongodb://mongo:27017 # MongoDB connection URI
MONGO_DB_NAME=url_shortener_db # Database name
MONGO_COLLECTION_NAME=urls # Collection nameIf you want to run the project without Docker, you need to install dependencies manually:
go mod tidyUse Docker Compose to start the project:
docker-compose up --buildThis will:
- Build the Go application as a Docker container
- Start the Go API on
localhost:8080 - Start MongoDB as a separate container
To stop the containers, press CTRL + C or run:
docker-compose downIf you prefer running the project without Docker:
Make sure you have MongoDB installed and running. If not, start it using:
mongod --dbpath <your-db-path>Execute the following command:
go run main.goThe server should now be running at:
👉 http://localhost:8080
- Endpoint:
POST /api/v1/createShortUrl - Request Body:
{ "url": "https://example.com/very/long/url" } - Response:
{ "shortUrl": "abcdefg" }
- Endpoint:
GET /api/v1/{shortUrl} - Response:
A301redirect to the original URL.
- Endpoint:
PUT /api/v1/updateShortUrl/{shortUrl} - Request Body:
{ "url": "https://example.com/new-destination" } - Response:
{ "status": "Successful", "message": "Short URL updated successfully" }
-
Create a Short URL:
- Method:
POST - URL:
http://localhost:8080/api/v1/createShortUrl - Body (raw, JSON):
{ "url": "https://example.com/very/long/url" } - Expected Response:
{ "shortUrl": "abcdefg" }
- Method:
-
Redirect to Original URL:
- Method:
GET - URL:
http://localhost:8080/api/v1/abcdefg - Expected Behavior: Redirects to
https://example.com/very/long/url.
- Method:
-
Update a Short URL:
- Method:
PUT - URL:
http://localhost:8080/api/v1/updateShortUrl/abcdefg - Body (raw, JSON):
{ "url": "https://example.com/new-destination" } - Expected Response:
{ "status": "Successful", "message": "Short URL updated successfully" }
- Method:
url-shortener-api/
├── .env # Environment variables
├── .gitignore # Git ignore file
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose for Go & MongoDB
├── go.mod # Go module file
├── go.sum # Go dependencies checksum
├── main.go # Entry point (Gin API)
├── README.md # Project documentation
├── database/ # MongoDB connection
│ └── database.go
├── models/ # Data models
│ └── models.go
├── routes/ # API routes and handlers
│ └── routes.go
└── utils/ # Utility functions
└── shortener.go
This project is licensed under the MIT License.
✅ .env file contains important configurations like MongoDB URI and server port.
✅ Use docker-compose up --build to run the project with Docker.
✅ If running locally, install dependencies with go mod tidy and run go run main.go.
✅ Test API with Postman or curl commands.