Skip to content

Moeed-ul-Hassan/Ecommerce-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ecommerce API (Go)

A modern, scalable e-commerce REST API built with Go, following clean architecture principles and best practices.

🏗️ Architecture

This project implements clean architecture with clear separation of concerns:

graph TD
    User((User)) -->|HTTP Request| API[API Entry Point - cmd/api]
    API -->|Router| Chi[Chi Router]
    Chi -->|Middleware| MW[Logger/Recoverer/Auth]
    MW -->|Route Handler| Handlers[Internal Handlers]
    Handlers -->|Service Call| Services[Business Logic Services]
    Services -->|Data Access| Store[Data Store/Repository]
    Store -->|Query| DB[(Database)]
Loading

📁 Project Structure

ecommerce-api-go/
├── cmd/
│   └── api/              # Application entry point
│       └── main.go
├── internal/             # Private application code
│   ├── handlers/         # HTTP request handlers (controllers)
│   ├── services/         # Business logic (service layer)
│   ├── repositories/     # Data access (repository layer)
│   ├── models/           # Data models (entities)
│   └── router/           # Route definitions
├── pkg/                  # Shared utilities or helpers
├── config/               # Configuration management
├── .env.example          # Environment variables template
├── go.mod
└── README.md

✨ Features

  • 🔒 JWT Authentication: Secure user authentication and authorization
  • 🛡️ Input Validation: Comprehensive request validation
  • 📊 Structured Logging: Detailed logging for debugging and monitoring
  • 🔄 Middleware Support: Logger, recoverer, and custom middleware
  • 🎯 Clean Architecture: Separation of concerns with handlers, services, and repositories
  • 🚀 Chi Router: Fast and lightweight HTTP router
  • 📝 Structured Responses: Consistent JSON response format

🚀 Getting Started

Prerequisites

  • Go 1.21 or higher
  • PostgreSQL (or your preferred database)
  • Git

Installation

  1. Clone the repository

    git clone https://github.com/Moeed-ul-Hassan/ecommerce-api-go.git
    cd ecommerce-api-go
  2. Install dependencies

    go mod download
  3. Set up environment variables

    cp .env.example .env
    # Edit .env with your configuration
  4. Run the application

    go run cmd/api/main.go

The API will be available at http://localhost:8080 (or your configured port).

🔧 Configuration

Create a .env file in the root directory with the following variables:

# Server Configuration
PORT=8080
ENV=development

# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=your_db_user
DB_PASSWORD=your_db_password
DB_NAME=ecommerce_db

# JWT Configuration
JWT_SECRET=your_secret_key

📚 API Endpoints

Health Check

  • GET /health - Check API health status

User Management (Coming Soon)

  • POST /api/v1/users/register - Register a new user
  • POST /api/v1/users/login - User login
  • GET /api/v1/users/profile - Get user profile (requires auth)

Products (Coming Soon)

  • GET /api/v1/products - List all products
  • GET /api/v1/products/:id - Get product details
  • POST /api/v1/products - Create product (admin only)
  • PUT /api/v1/products/:id - Update product (admin only)
  • DELETE /api/v1/products/:id - Delete product (admin only)

🛠️ Development

Project Rules

  1. Clean Architecture: Separate concerns between handlers, services, and repositories
  2. Dependency Injection: Pass dependencies (database, config) explicitly to structs
  3. Error Handling: Wrap errors with context (e.g., fmt.Errorf("service: %w", err))
  4. Validation: Validate all incoming requests before processing
  5. Consistency: Use a standard JSON response format for all endpoints
  6. Logging: Log important events and errors using a structured logger

Running Tests

go test ./...

Building for Production

go build -o bin/api cmd/api/main.go

🗺️ Roadmap

  • Project initialization
  • Basic server setup with Chi router
  • Health check endpoint
  • Database integration (PostgreSQL)
  • User authentication & authorization (JWT)
  • Product management
  • Order management
  • Payment integration
  • API documentation (Swagger)
  • Unit & integration tests
  • Docker containerization
  • CI/CD pipeline

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

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

📄 License

This project is licensed under the MIT License.

👤 Author

Moeed ul Hassan

🙏 Acknowledgments

  • Chi Router - Lightweight, idiomatic HTTP router
  • godotenv - Go port of Ruby's dotenv library

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages