Skip to content

shihabkandil/eatsy-backend

Repository files navigation

Eatsy Backend API

A RESTful API for a food delivery application built with Go, Gin, and Firestore (NoSQL).

Features

  • User authentication with JWT
  • Product management (CRUD operations)
  • Category management
  • Shopping cart functionality
  • Swagger API documentation
  • Firestore NoSQL database
  • Environment-based configuration

Tech Stack

  • Go 1.21+
  • Gin Web Framework
  • Firestore (NoSQL)
  • JWT for authentication
  • Swagger for API documentation

Prerequisites

  • Go 1.21 or higher
  • Google Cloud Project with Firestore enabled (Native mode)
  • Service account JSON key with Firestore permissions
  • Make sure you have the following environment variables set:
    • GCP_PROJECT_ID (your GCP project ID)
    • GOOGLE_APPLICATION_CREDENTIALS (path to your service account JSON key)
    • JWT_SECRET (required for JWT signing)
    • PORT (default: 8080)

Installation

  1. Clone the repository:
git clone https://github.com/shihabkandil/eatsy-backend.git
cd eatsy-backend
  1. Install dependencies:
go mod download
  1. Set up environment variables:
export GCP_PROJECT_ID=your-gcp-project-id
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/serviceAccount.json
export JWT_SECRET=your-very-long-random-secret-key
export PORT=8080 # optional
  1. Run the application:
go run main.go

API Documentation

The API documentation is available at http://localhost:8080/swagger/index.html when the server is running.

Authentication

All protected endpoints require a JWT token in the Authorization header:

Authorization: Bearer <your-token>

Endpoints

Authentication

  • POST /auth/register - Register a new user
  • POST /auth/login - Login and get JWT token

Products

  • GET /products - Get all products
  • GET /products/:id - Get a specific product
  • POST /products - Create a new product
  • PUT /products/:id - Update a product
  • DELETE /products/:id - Delete a product

Cart

  • GET /cart - Get user's cart
  • POST /cart/items - Add item to cart
  • PUT /cart/items/:id - Update cart item quantity
  • DELETE /cart/items/:id - Remove item from cart

Response Format

All responses follow a consistent format:

Success Response

{
    "status_code": 200,
    "message": "Operation completed successfully",
    "data": { ... }
}

Error Response

{
    "status_code": 400,
    "message": "Invalid input",
    "error": "Detailed error message"
}

Firestore Data Model

Users

  • id (int64)
  • email (string)
  • password (string)
  • name (string)
  • role (string)
  • created_at (timestamp)
  • updated_at (timestamp)

Products

  • id (int64)
  • name (string)
  • description (string)
  • price (float64)
  • image_url (string)
  • category_id (int64)
  • created_at (timestamp)
  • updated_at (timestamp)

Categories

  • id (int64)
  • name (string)
  • description (string)
  • created_at (timestamp)
  • updated_at (timestamp)

Carts

  • id (int64)
  • user_id (int64)
  • created_at (timestamp)
  • updated_at (timestamp)
  • items (subcollection: Cart Items)

Cart Items (subcollection under each Cart)

  • id (int64)
  • cart_id (int64)
  • product_id (int64)
  • quantity (int)
  • price (float64)

Development

Running Tests

go test ./...

Code Generation

# Generate Swagger documentation
swag init

Best Practices

  1. Always use environment variables for configuration
  2. Keep sensitive information in environment variables
  3. Use proper error handling and logging
  4. Follow REST API best practices
  5. Use proper HTTP status codes
  6. Implement proper input validation
  7. Use proper authentication and authorization
  8. Keep the code modular and maintainable

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Golang backend food delivery application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages