Skip to content

zeroth/micro-service-test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-Tenant Microservices Backend

A multi-tenant microservices architecture built with Express.js, PostgreSQL, and Docker. The system provides isolated tenant environments with a central gateway for authentication and routing.

Architecture

┌─────────────┐
│   Client    │
└──────┬──────┘
       │
       ▼ :4000
┌─────────────┐
│   Gateway   │──── OAuth2 Authentication
│   Service   │──── JWT Token Generation
│             │──── Tenant Routing
└──────┬──────┘
       │
  ┌────┴────┐
  │         │
  ▼ :4001   ▼ :4002
┌──────┐  ┌──────┐
│Tenant│  │Tenant│
│  1   │  │  2   │
└──────┘  └──────┘

Services

  • Gateway Service (Port 4000): Entry point with OAuth2 authentication, user management, and request routing
  • Tenant Services (Ports 4001, 4002): Business logic and resources for each tenant
  • PostgreSQL Databases: Separate database for gateway and each tenant

Features

  • OAuth2 authentication with JWT tokens
  • Multi-tenant architecture with complete isolation
  • Tenant-based request routing
  • User and API client management
  • RESTful API for products and categories
  • Health check endpoints
  • Comprehensive logging
  • Docker containerization

Prerequisites

  • Docker and Docker Compose
  • Git

Quick Start

  1. Clone the repository:
git clone <repository-url>
cd micro_tenants_test
  1. Start all services:
docker-compose up -d
  1. Check service health:
curl http://localhost:4000/health
curl http://localhost:4001/health
curl http://localhost:4002/health

API Endpoints

Authentication (Gateway - Port 4000)

Register User

POST /auth/register
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "securepassword",
  "firstName": "John",
  "lastName": "Doe",
  "tenantId": "tenant1"
}

Login

POST /auth/login
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "securepassword"
}

Refresh Token

POST /auth/refresh
Content-Type: application/json

{
  "refreshToken": "your-refresh-token"
}

Register API Client

POST /auth/client/register
Content-Type: application/json

{
  "name": "My API Client",
  "tenantId": "tenant1",
  "redirectUris": ["http://localhost:3000/callback"]
}

# Response includes clientId and clientSecret (save these securely!)
{
  "clientId": "client_1234567890_abc123",
  "clientSecret": "your-generated-secret",
  "name": "My API Client",
  "tenantId": "tenant1"
}

Get Client Access Token (OAuth2 Client Credentials)

POST /auth/oauth/token
Content-Type: application/json

{
  "grant_type": "client_credentials",
  "client_id": "client_1234567890_abc123",
  "client_secret": "your-generated-secret"
}

# Response
{
  "access_token": "jwt-token-for-client",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "api"
}

Products API (Through Gateway)

All product endpoints are accessed through the gateway at /api/products. The gateway routes to the appropriate tenant based on the JWT token.

Get Products

GET /api/products
Authorization: Bearer <jwt-token>

Get Product by ID

GET /api/products/:id
Authorization: Bearer <jwt-token>

Create Product

POST /api/products
Authorization: Bearer <jwt-token>
Content-Type: application/json

{
  "name": "Product Name",
  "description": "Product description",
  "price": 29.99,
  "stock": 100,
  "sku": "PROD-001",
  "categoryId": "category-uuid"
}

Update Product

PUT /api/products/:id
Authorization: Bearer <jwt-token>
Content-Type: application/json

{
  "name": "Updated Product Name",
  "price": 39.99
}

Delete Product

DELETE /api/products/:id
Authorization: Bearer <jwt-token>

Get Categories

GET /api/products/categories
Authorization: Bearer <jwt-token>

Create Category

POST /api/products/categories
Authorization: Bearer <jwt-token>
Content-Type: application/json

{
  "name": "Electronics",
  "description": "Electronic products"
}

Docker Commands

# Start all services
docker-compose up -d

# View logs
docker-compose logs -f

# Stop all services
docker-compose down

# Rebuild services
docker-compose up --build

# Access service shell
docker-compose exec gateway sh
docker-compose exec tenant1 sh
docker-compose exec tenant2 sh

# Run database migrations
docker-compose exec gateway npm run migrate
docker-compose exec tenant1 npm run migrate
docker-compose exec tenant2 npm run migrate

Environment Variables

Each service can be configured through environment variables. See .env.example files in each service directory for available options.

Testing

Run tests for each service:

docker-compose exec gateway npm test
docker-compose exec tenant1 npm test
docker-compose exec tenant2 npm test

Development

The system is fully containerized. To make changes:

  1. Modify the code in the respective service directory
  2. Rebuild the service: docker-compose up --build <service-name>
  3. Check logs: docker-compose logs -f <service-name>

Security Considerations

  • Change JWT_SECRET in production
  • Use strong passwords for database
  • Enable HTTPS in production
  • Implement rate limiting
  • Add input validation
  • Use environment-specific configurations

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •