API aggregation and orchestration gateway for the xshopai e-commerce platform
Getting Started β’ Documentation β’ API Reference β’ Contributing
The Web BFF provides a unified API layer for web clients, aggregating data from multiple microservices and handling client-specific logic. It reduces chatty frontend calls, transforms backend responses for UI needs, manages authentication gateway concerns, and implements caching, rate limiting, and circuit breaker patterns for resilient service orchestration.
|
|
|
|
BFF Pattern (Backend for Frontend):
βββββββββββββββ βββββββββββ ββββββββββββββββββββ
β customer-ui ββββββΆβ Web BFF ββββββΆβ auth-service β
β admin-ui β β β β user-service β
βββββββββββββββ β Dapr β β product-service β
β Sidecar β β order-service β
βββββββββββ β cart-service β
β review-service β
ββββββββββββββββββββ
Key Responsibilities:
- Aggregate data from auth, user, product, order, cart, review services
- Transform responses for web client requirements
- Handle session management and authentication flow
- Implement client-specific business logic
- Node.js 20+
- Docker & Docker Compose (optional)
- Dapr CLI (for production-like setup)
# Clone the repository
git clone https://github.com/xshopai/web-bff.git
cd web-bff
# Start all services
docker-compose up -d
# Verify the service is healthy
curl http://localhost:8014/health/readyπ§ Without Dapr (Simple Setup)
# Install dependencies
npm install
# Set up environment variables
cp .env.example .env
# Edit .env with your configuration
# Start the service
npm run devπ See Local Development Guide for detailed instructions.
β‘ With Dapr (Production-like)
# Ensure Dapr is initialized
dapr init
# Start with Dapr sidecar
npm run dev:dapr
# Or use platform-specific scripts
./run.sh # Linux/Mac
.\run.ps1 # WindowsNote: All services now use the standard Dapr ports (3500 for HTTP, 50001 for gRPC).
π See Dapr Development Guide for detailed instructions.
| Document | Description |
|---|---|
| π Local Development | Step-by-step local setup without Dapr |
| β‘ Local Development with Dapr | Local setup with full Dapr integration |
| π Technical Reference | Architecture, security, monitoring |
API Documentation: See src/routes/ for endpoint definitions and tests/integration/ for API contract examples.
# Run all tests
npm test
# Run with coverage report
npm run test:coverage
# Lint code
npm run lint
# Type check
npm run type-check
# Build TypeScript
npm run build| Metric | Status |
|---|---|
| Unit Tests | β Passing |
| Code Coverage | β Target 80%+ |
| Security Scan | β 0 vulnerabilities |
web-bff/
βββ π src/ # Application source code
β βββ π controllers/ # Route handlers
β βββ π services/ # Service aggregation logic
β βββ π routes/ # Route definitions
β βββ π middlewares/ # Auth, logging, rate limiting
β βββ π clients/ # Service client wrappers (Dapr)
β βββ π core/ # Config, logger, errors
β βββ π app.ts # Express app setup
β βββ π server.ts # Entry point
βββ π tests/ # Test suite
β βββ π unit/ # Unit tests
β βββ π integration/ # Integration tests
βββ π .dapr/ # Dapr configuration
β βββ π components/ # Service invocation, state
β βββ π config.yaml # Dapr runtime configuration
βββ π docs/ # Documentation
βββ π docker-compose.yml # Local containerized environment
βββ π Dockerfile # Production container image
βββ π package.json # Node.js dependencies
| Category | Technology |
|---|---|
| π’ Runtime | Node.js 20+ with TypeScript 5.0+ |
| π Framework | Express 4.18+ |
| π‘ Service Mesh | Dapr Service Invocation |
| π Authentication | JWT Tokens + Cookie-based sessions |
| β‘ Resilience | Circuit breakers, retries, rate limiting |
| π§ͺ Testing | Jest with coverage reporting |
| π Observability | Structured logging + correlation IDs |
| Variable | Description | Default |
|---|---|---|
PORT |
HTTP server port | 8014 |
NODE_ENV |
Environment | development |
JWT_SECRET |
JWT signing secret | (required) |
CORS_ORIGIN |
Allowed CORS origins | http://localhost:3000 |
AUTH_SERVICE_APP_ID |
Dapr app ID | auth-service |
USER_SERVICE_APP_ID |
Dapr app ID | user-service |
PRODUCT_SERVICE_APP_ID |
Dapr app ID | product-service |
ORDER_SERVICE_APP_ID |
Dapr app ID | order-service |
CART_SERVICE_APP_ID |
Dapr app ID | cart-service |
REVIEW_SERVICE_APP_ID |
Dapr app ID | review-service |
DAPR_HTTP_PORT |
Dapr sidecar HTTP | 3500 |
DAPR_GRPC_PORT |
Dapr sidecar gRPC | 50001 |
# π³ Docker Compose
docker-compose up -d # Start all services
docker-compose down # Stop all services
docker-compose logs -f web-bff # View logs
# π’ Local Development
npm run dev # Run without Dapr
npm run dev:dapr # Run with Dapr sidecar
npm run build # Build TypeScript
# π§ͺ Testing
npm test # Run all tests
npm run test:coverage # Run with coverage
npm run lint # Lint code
npm run type-check # TypeScript check
# π Health Check
curl http://localhost:8014/health/ready
curl http://localhost:8014/health/liveWe welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch
git checkout -b feature/amazing-feature
- Write tests for your changes
- Run the test suite
npm test && npm run lint
- Commit your changes
git commit -m 'feat: add amazing feature' - Push to your branch
git push origin feature/amazing-feature
- Open a Pull Request
Please ensure your PR:
- β Passes all existing tests
- β Includes tests for new functionality
- β Follows the existing code style
- β Updates documentation as needed
| Resource | Link |
|---|---|
| π Bug Reports | GitHub Issues |
| π Documentation | docs/ |
| π¬ Discussions | GitHub Discussions |
This project is part of the xshopai e-commerce platform. Licensed under the MIT License - see LICENSE for details.
Made with β€οΈ by the xshopai team