# Beem Smart Taxi Microservices

This directory contains the microservices architecture for the Beem Smart Taxi application, converted from the original monolithic Spring Boot application.

## Architecture Overview

The microservices architecture consists of:

- **Discovery Service** (Eureka) - Service discovery and registration
- **Config Service** (Spring Cloud Config) - Centralized configuration management
- **API Gateway** (Spring Cloud Gateway) - Single entry point with routing and authentication
- **Auth Service** - Authentication and authorization
- **Shared Module** - Common DTOs, responses, and utilities

## Services

### 1. Discovery Service (Port: 8761)
- Eureka server for service discovery
- All microservices register with this service

### 2. Config Service (Port: 8888)
- Spring Cloud Config server
- Centralized configuration management
- Supports Git-based configuration

### 3. API Gateway (Port: 8080)
- Single entry point for all client requests
- JWT authentication and authorization
- Route forwarding to appropriate microservices
- WebSocket support

### 4. Auth Service (Port: 8081)
- User authentication and authorization
- JWT token generation and validation
- Password reset functionality
- Email verification

## Prerequisites

- Java 17
- Maven 3.6+
- Docker and Docker Compose
- PostgreSQL 15
- Redis 7
- RabbitMQ 3

## Quick Start

### 1. Build All Services

```bash
# Build the entire project
mvn clean install -DskipTests

# Or build individual services
cd beem-shared && mvn clean install
cd ../discovery-service && mvn clean install
cd ../config-service && mvn clean install
cd ../api-gateway && mvn clean install
cd ../auth-service && mvn clean install
```

### 2. Start Infrastructure Services

```bash
# Start PostgreSQL, Redis, and RabbitMQ
docker-compose up -d postgres redis rabbitmq
```

### 3. Start Microservices

```bash
# Start all services
docker-compose up -d

# Or start services individually
docker-compose up -d eureka
docker-compose up -d config
docker-compose up -d gateway
docker-compose up -d auth-service
```

### 4. Access Services

- **Eureka Dashboard**: http://localhost:8761
- **API Gateway**: http://localhost:8080
- **Auth Service**: http://localhost:8081
- **RabbitMQ Management**: http://localhost:15672 (guest/guest)

## API Endpoints

### Authentication Endpoints (via Gateway)

All authentication endpoints are available through the API Gateway at `http://localhost:8080`:

- `POST /api/v1/auth/signin` - User sign in
- `POST /api/v1/auth/refreshtoken` - Refresh JWT token
- `POST /api/v1/auth/resend_code` - Resend verification code
- `POST /api/v1/auth/reset_password_first_step` - Initiate password reset
- `POST /api/v1/auth/email_check_first_step` - Email verification
- `POST /api/v1/auth/validate_reset_code_second_step` - Validate reset code
- `POST /api/v1/auth/change_password_final_step` - Change password

## Configuration

### Environment Variables

Key configuration can be overridden via environment variables:

```bash
# Database
SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/beem
SPRING_DATASOURCE_USERNAME=beem
SPRING_DATASOURCE_PASSWORD=beemSecret

# JWT
JWT_SECRET=your-jwt-secret
JWT_EXPIRATION=31536000000

# Service Discovery
EUREKA_CLIENT_SERVICE_URL_DEFAULTZONE=http://localhost:8761/eureka/
```

### Service-Specific Configuration

Each service has its own configuration in `src/main/resources/application.yml`:

- **Discovery Service**: Eureka server configuration
- **Config Service**: Git repository and search paths
- **API Gateway**: Route definitions and JWT settings
- **Auth Service**: Database and JWT configuration

## Development

### Adding New Services

1. Create a new service directory
2. Add the service to the parent `pom.xml` modules
3. Create the service POM with appropriate dependencies
4. Add the service to `docker-compose.yml`
5. Update API Gateway routes if needed

### Database Migrations

The Auth Service uses Flyway for database migrations. Migration scripts should be placed in:
```
auth-service/src/main/resources/db/migration/
```

### Testing

```bash
# Run tests for all services
mvn test

# Run tests for specific service
cd auth-service && mvn test
```

## Monitoring and Health Checks

All services expose health check endpoints:

- `GET /actuator/health` - Service health status
- `GET /actuator/info` - Service information

## Troubleshooting

### Common Issues

1. **Service Discovery Issues**
   - Ensure Eureka is running before starting other services
   - Check service registration in Eureka dashboard

2. **Database Connection Issues**
   - Verify PostgreSQL is running and accessible
   - Check database credentials in configuration

3. **JWT Authentication Issues**
   - Ensure JWT secret is consistent across services
   - Check token expiration settings

### Logs

View service logs:

```bash
# View all service logs
docker-compose logs -f

# View specific service logs
docker-compose logs -f auth-service
```

## Migration Strategy

This microservices architecture is designed for gradual migration from the monolithic application:

1. **Phase 1**: Infrastructure setup (Discovery, Config, Gateway)
2. **Phase 2**: Auth Service extraction
3. **Phase 3**: Other services extraction
4. **Phase 4**: Monolith retirement

The API Gateway routes can be gradually updated to point to new microservices while maintaining backward compatibility.

## Security

- JWT-based authentication
- Role-based authorization
- Secure communication between services
- Input validation and sanitization

## Performance

- Service discovery for load balancing
- Caching with Redis
- Message queuing with RabbitMQ
- Horizontal scaling capability

## Contributing

1. Follow the existing code structure
2. Maintain API compatibility
3. Add appropriate tests
4. Update documentation
5. Follow security best practices
