This project is a production-ready Go HTTP server that connects to MongoDB, featuring a modular folder structure and a scalable architecture. The server includes two example routes: one for fetching records (GET) and another for adding records (POST).
- RESTful HTTP server built with Go.
- MongoDB integration for persistent data storage.
- Clean, modular folder structure for scalability and maintainability.
- Graceful shutdown handling.
- Configurable via environment variables.
- Go (>= 1.20)
- MongoDB (running instance)
git clone https://github.com/aditipolkam/go-url-shortner.git
cd go-url-shortner
Create a .env
file in the root directory with the following variables:
DB_URL=mongodb://localhost:27017
SERVER_ADDRESS=:8080
Install dependencies and start the server:
go mod tidy
go run cmd/server/main.go
The server will run on http://localhost:8080
.
For redirecting shortened url to the original url
curl -X GET http://localhost:8080/{{shortcode}}
Redirected to the original URL.
Add a new record to the database.
curl -X POST http://localhost:8080/ \
-H "Content-Type: application/json" \
-d '{"url": "https://blog.aditipolkam.me/efficient-database-indexes"}'
{
"short_url": "http://localhost:8080/xvlixG"
}
The server supports graceful shutdown on receiving SIGINT
(Ctrl+C) to ensure cleanup tasks, such as database disconnection, are performed.
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
- Commit your changes and push:
git push origin feature-name
- Open a pull request.
This project is licensed under the MIT License.