A RESTful API for managing household pantry inventory, built with Go and MongoDB.
This API enables users to:
- Create, read, update, and delete pantry items
- Track inventory details including quantity, unit, location (
fridge,freezer, ordry), brand, barcode, and tags - Query all items or retrieve a specific item by ID
The project demonstrates core backend concepts including REST principles, input validation, database integration, and clean code architecture.
- Language: Go 1.25
- Framework: Gin (lightweight HTTP web framework)
- Database: MongoDB v2
- Validation: go-playground/validator
- CORS: gin-contrib/cors
The codebase follows a clean, layered architecture:
├── handlers/ # HTTP request handlers & error handling middleware
├── repository/ # MongoDB data access layer with interfaces
├── models/ # Data models
├── routes/ # Route registration
└── database/ # MongoDB client setup
Key Design Decisions:
- Repository Pattern: Abstracts data access behind interfaces, enabling easy testing and swappable implementations
- Dependency Injection: Controllers receive repositories through constructors
- Centralized Error Handling: Middleware catches errors from handlers and returns consistent JSON responses
- Separation of Concerns: Clear boundaries between the HTTP layer, business logic, and data access
| Method | Endpoint | Description |
|---|---|---|
GET |
/api/pantry |
List all pantry items |
GET |
/api/pantry/:id |
Get a single pantry item by ID |
POST |
/api/pantry |
Create a new pantry item |
PATCH |
/api/pantry/:id |
Update a pantry item |
DELETE |
/api/pantry/:id |
Delete a pantry item |
{
"id": "ObjectID",
"barcode": "string (optional)",
"details": {
"name": "string (required)",
"brand": "string (optional)"
},
"inventory": {
"quantity": "int (optional)",
"unit": "string (required)"
},
"location": "fridge | freezer | dry (required)",
"tags": ["string"]
}- Go 1.25+
- MongoDB Atlas URI or a local MongoDB instance
- Docker / Docker Compose (optional)
Create a .env file in the project root:
MONGO_URI=mongodb+srv://<user>:<password>@<cluster>/?appName=<app>
GIN_MODE=debug
ALLOWED_ORIGINS=http://localhost:5173
go mod download
go run main.godocker compose upThe server listens on port 8080.
- HTTP Status Codes — Appropriate codes throughout:
201 Created,204 No Content,400 Bad Request,404 Not Found,422 Unprocessable Entity,500 Internal Server Error - Input Validation — Binding tags enforce required fields, enum values (
oneof), and minimum lengths; validation errors are returned as structured JSON - CORS — Configurable origin allowlist via environment variable
- Error Middleware — A single Gin middleware centralises error translation, keeping handlers clean
- Interface-Based Repository —
ReadPantry,WritePantry, andDeletePantryinterfaces decouple the HTTP layer from MongoDB, making the data layer straightforward to test or replace
This project was built in Go using the Gin framework to explore language and framework options outside of Node.js. Go's strong typing, concurrency model, and single binary deployment made it an excellent choice for learning API development fundamentals while working with a different ecosystem.