GitHunt
RU

Ruseigha/Goecomapi

production-grade e-commerce backend API built in Go, applying Clean Architecture and the Repository Pattern with GitHub Flow. It includes JWT authentication, role-based access control, MongoDB integration, RESTful CRUD endpoints, structured logging, automated testing, Dockerized deployment, and CI via GitHub Actions,

๐Ÿ›’ E-commerce Backend API (Golang)

CI
Go
MongoDB
Docker
License
Coverage

A production-grade e-commerce backend API built with Go, designed using Clean Architecture, the Repository Pattern, and GitHub Flow.
The system implements JWT-based authentication, role-based authorization (admin/user), and full CRUD operations for users, products, and orders.

This project reflects real-world backend engineering practices, including manual dependency injection, context propagation, structured logging, graceful shutdown, testing at multiple layers, Dockerized deployment, and CI enforcement.


๐Ÿ“Œ Key Features

  • Clean Architecture (handlers โ†’ services โ†’ repositories)
  • Manual Dependency Injection (no DI frameworks)
  • JWT Authentication (access tokens)
  • Role-Based Authorization (admin / user)
  • MongoDB using the official Go driver
  • RESTful API with versioning (/api/v1)
  • Pagination for list endpoints
  • Structured logging with Zap
  • Graceful shutdown with context cancellation
  • Unit & integration testing (manual mocks, no mocking libraries)
  • Docker & docker-compose
  • OpenAPI v3 (Swagger) documentation
  • GitHub Flow + CI with GitHub Actions

๐Ÿ— Architecture Over

                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚     Client      โ”‚
                       โ”‚  (Web / App)    โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ HTTP Request (JSON)
                               โ–ผ
                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚   Router        โ”‚
                       โ”‚ (gorilla/mux)   โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚
                               โ–ผ
                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚   Handler       โ”‚
                       โ”‚ (HTTP Layer)    โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ context.Context
                               โ–ผ
                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚   Service       โ”‚
                       โ”‚ (Business       โ”‚
                       โ”‚   Logic)        โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ context.Context
                               โ–ผ
                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚ Repository      โ”‚
                       โ”‚ (Data Access    โ”‚
                       โ”‚ Abstraction)    โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                               โ”‚ context.Context
                               โ–ผ
                       โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                       โ”‚   MongoDB       โ”‚
                       โ”‚ (Persistence)   โ”‚
                       โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Why this architecture?

  • Keeps business logic independent of frameworks
  • Makes the database swappable with minimal changes
  • Enables fast, deterministic unit tests
  • Scales well for real teams and long-term maintenance

๐Ÿ“ Project Structure

   ecommerce-api/
   โ”œโ”€โ”€ cmd/
   โ”‚   โ””โ”€โ”€ api/               # Application entry point (main.go)
   โ”œโ”€โ”€ internal/
   โ”‚   โ”œโ”€โ”€ config/            # Environment & config loading (load .env, app settings)
   โ”‚   โ”œโ”€โ”€ domain/            # Core domain models (User, Product, Order structs)
   โ”‚   โ”œโ”€โ”€ database/          # MongoDB connection setup and client provider
   โ”‚   โ”œโ”€โ”€ repository/        # Data access implementations (interfaces + MongoDB)
   โ”‚   โ”œโ”€โ”€ service/           # Business logic (AuthService, ProductService, etc.)
   โ”‚   โ”œโ”€โ”€ handler/           # HTTP handlers (handle requests & responses)
   โ”‚   โ”œโ”€โ”€ middleware/        # Auth & role middleware (JWT validation, admin-only routes)
   โ”‚   โ””โ”€โ”€ routes/            # API routing (versioned endpoints /api/v1)
   โ”œโ”€โ”€ pkg/                   # Shared utilities (JWT, hashing, JSON response helpers)
   โ”œโ”€โ”€ tests/                 # Tests (unit + integration for handlers, services, repositories)
   โ”œโ”€โ”€ docs/                  # Swagger/OpenAPI documentation
   โ”œโ”€โ”€ Dockerfile             # Docker image build instructions
   โ”œโ”€โ”€ docker-compose.yml     # Compose for API + MongoDB (+ optional Mongo Express)
   โ”œโ”€โ”€ Makefile               # Build, run, test, lint, docker commands
   โ”œโ”€โ”€ README.md              # Project overview, setup, architecture, API docs
   โ””โ”€โ”€ CONTRIBUTING.md        # Contribution guidelines, commit standards, workflow

๐Ÿ” Authentication & Authorization

Authentication

  • Users authenticate using email + password
  • Passwords are hashed with bcrypt
  • JWT access tokens are issued on login

Authorization

  • Every request carries a JWT
  • Middleware validates the token
  • Role-based middleware restricts admin routes

Roles

  • user โ†’ can place orders, view own data
  • admin โ†’ can manage products

๐ŸŒ API Versioning

All endpoints are versioned:

 /api/v1/...  

This allows safe future evolution without breaking clients.

๐Ÿ“š API Documentation (Swagger)

Swagger (OpenAPI v3) documentation is available and secured.

Access locally

http://localhost:8080/swagger 

##Swagger includes:

  • All endpoints
  • Request/response schemas
  • JWT security definitions
  • Example payloads

Testing Principles

  • Go testing package only
  • Manual mocks (no mocking libraries)
  • Table-driven tests
  • Arrange โ†’ Act โ†’ Assert
  • Context cancellation covered
  • Deterministic & fast

Run all tests:

go test ./... -cover