Skip to content

BenJECole/pantry-api

Repository files navigation

Pantry API

A RESTful API for managing household pantry inventory, built with Go and MongoDB.

Project Overview

This API enables users to:

  • Create, read, update, and delete pantry items
  • Track inventory details including quantity, unit, location (fridge, freezer, or dry), 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.

Tech Stack

  • Language: Go 1.25
  • Framework: Gin (lightweight HTTP web framework)
  • Database: MongoDB v2
  • Validation: go-playground/validator
  • CORS: gin-contrib/cors

Architecture

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

API Endpoints

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

Pantry Item Schema

{
  "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"]
}

Setup & Running

Prerequisites

  • Go 1.25+
  • MongoDB Atlas URI or a local MongoDB instance
  • Docker / Docker Compose (optional)

Environment Variables

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

Local Development

go mod download
go run main.go

Docker

docker compose up

The server listens on port 8080.

Highlights

  • 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 RepositoryReadPantry, WritePantry, and DeletePantry interfaces decouple the HTTP layer from MongoDB, making the data layer straightforward to test or replace

Why Go and Gin?

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.

About

RESTful API for household pantry inventory management. Built with Go, Gin, and MongoDB. Demonstrates clean architecture, dependency injection, and interface-based design.

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors