Skip to content

Codeblitz-Ankit/recipe-sharing-api-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Recipe Sharing API

A production-quality REST API for a recipe-sharing platform built with Go (Gin), PostgreSQL, GORM, and image processing via disintegration/imaging.


📁 Project Structure

recipe-sharing-api-go/
├── cmd/server/main.go          # Entry point — wires everything together
├── configs/config.go           # Env-based configuration
├── internal/
│   ├── handlers/               # HTTP layer (request parsing, response writing)
│   │   ├── recipe_handler.go
│   │   └── rating_handler.go
│   ├── services/               # Business logic
│   │   ├── recipe_service.go
│   │   └── rating_service.go
│   ├── repositories/           # Database access (GORM)
│   │   ├── recipe_repository.go
│   │   ├── ingredient_repository.go
│   │   └── rating_repository.go
│   ├── models/                 # GORM models
│   │   ├── recipe.go
│   │   ├── ingredient.go
│   │   ├── recipe_ingredient.go
│   │   └── rating.go
│   ├── middleware/             # Gin middleware
│   │   ├── logger.go
│   │   ├── cors.go
│   │   └── recovery.go
│   └── utils/                 # Shared utilities
│       ├── image.go            # Upload, resize (800×800), JPEG compress
│       └── response.go         # JSON helpers + pagination
├── migrations/
│   └── 001_create_tables.sql  # Full DDL with indexes & triggers
├── uploads/                   # Stored images (auto-created)
├── docs/
│   └── api.md                 # Endpoint reference + cURL examples
├── .env.example
└── go.mod

⚙️ Prerequisites

Tool Version
Go ≥ 1.21
PostgreSQL ≥ 14

👉 ACTION REQUIRED BY USER

1. Install PostgreSQL (if not installed)

sudo apt update && sudo apt install -y postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresql

2. Create the database and user

sudo -u postgres psql

Inside psql:

CREATE DATABASE recipe_db;
CREATE USER recipe_user WITH ENCRYPTED PASSWORD 'your_password';
GRANT ALL PRIVILEGES ON DATABASE recipe_db TO recipe_user;
\q

3. Configure environment

cp .env.example .env

Edit .env with your actual credentials:

DB_USER=recipe_user
DB_PASSWORD=your_password
DB_NAME=recipe_db

🚀 Setup & Run

1. Install Go dependencies

go mod tidy

2. Run migrations (optional — GORM AutoMigrate runs on startup)

psql -U recipe_user -d recipe_db -f migrations/001_create_tables.sql

3. Start the server

go run ./cmd/server/main.go

The server starts on http://localhost:8080 by default.


🌐 API Endpoints

Base URL: http://localhost:8080/api/v1

Method Endpoint Description
GET /health Health check
POST /recipes Create recipe (multipart)
GET /recipes?page=1&limit=10 List all recipes (paginated)
GET /recipes/:id Get recipe by ID
DELETE /recipes/:id Delete recipe
GET /recipes/search?ingredients=tomato,garlic Search by ingredients
POST /recipes/:id/rate Rate a recipe (1–5)

See docs/api.md for detailed request/response examples.


🖼️ Image Handling

  • Accepts multipart/form-data uploads via the image field
  • Resized to max 800×800 (aspect ratio preserved, no upscaling)
  • Compressed to JPEG quality 85
  • Saved with a UUID filename under ./uploads/
  • Served statically at /uploads/<filename>.jpg

🗄️ Database Design

Tables

Table Description
recipes Core recipe data
ingredients Normalized ingredient catalog
recipe_ingredients JOIN table with quantity
ratings One rating per user per recipe

Key Indexes

Index Purpose
idx_recipes_user_id Filter by user
idx_recipe_ingredients_ingredient_id Ingredient search JOIN
idx_ratings_recipe_id Fast avg rating aggregation
uq_ratings_recipe_user Prevent duplicate ratings

🧪 Quick Test

# Health check
curl http://localhost:8080/health

# Create a recipe
curl -X POST http://localhost:8080/api/v1/recipes \
  -F "title=Spaghetti Carbonara" \
  -F "instructions=Boil pasta. Mix eggs and cheese. Combine." \
  -F "user_id=user-1" \
  -F "ingredient_names[]=pasta" \
  -F "ingredient_qtys[]=200g" \
  -F "ingredient_names[]=eggs" \
  -F "ingredient_qtys[]=3" \
  -F "image=@/path/to/image.jpg"

# List recipes
curl "http://localhost:8080/api/v1/recipes?page=1&limit=5"

# Search by ingredients
curl "http://localhost:8080/api/v1/recipes/search?ingredients=pasta,eggs"

# Rate a recipe
curl -X POST http://localhost:8080/api/v1/recipes/<RECIPE_ID>/rate \
  -H "Content-Type: application/json" \
  -d '{"user_id":"user-2","score":5}'

📋 Response Format

Success:

{ "success": true, "data": { ... } }

Paginated:

{
  "success": true,
  "data": [...],
  "meta": { "current_page": 1, "per_page": 10, "total_items": 42, "total_pages": 5 }
}

Error:

{ "success": false, "error": "description of the error" }

🤖 AI Assistance Disclosure

This project was developed with the assistance of AI tools. All prompts used during development are documented in prompts.md.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors