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)
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 dataadminโ 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
testingpackage only - Manual mocks (no mocking libraries)
- Table-driven tests
- Arrange โ Act โ Assert
- Context cancellation covered
- Deterministic & fast
Run all tests:
go test ./... -cover