Skip to content

amiosamu/messenger

Repository files navigation

Messenger Backend Service

A microservices-based messenger backend built with Go, gRPC, PostgreSQL, and Redis.

Architecture

The system consists of the following services:

  • Gateway Service (Port 8000): HTTP API gateway that routes requests to appropriate services
  • Auth Service (Ports 50001/8001): Authentication and authorization service
  • User Service (Ports 50002/8002): User management service
  • Chat Service (Ports 50003/8003): Chat and messaging service
  • Subscriber Service (Ports 50004/8004): Subscription and notification service

Prerequisites

  • Docker and Docker Compose
  • Go 1.23.2 or later (for local development)
  • Protocol Buffers compiler (protoc)
  • Make

Quick Start

1. Clone the repository

git clone <repository-url>
cd messenger

2. Start the development environment

# Start all services in development mode
make dev-up

# Or start individual services
make docker-up-user  # Start only user service with dependencies

3. Verify services are running

# Check service status
docker-compose ps

# View logs
make docker-logs

4. Test the services

# Gateway service health check
curl http://localhost:8000/health

# User service health check (direct access)
curl http://localhost:8002/health

# Auth service health check (direct access)
curl http://localhost:8001/health

Available Make Commands

Development Commands

make dev-up       # Start full development environment
make dev-down     # Stop development environment
make dev-logs     # Show logs from all services
make dev-build    # Build all Docker images

Build Commands

make build        # Build all services
make build-user   # Build user service only
make build-auth   # Build auth service only
make proto        # Generate protobuf files

Docker Commands

make docker-build     # Build Docker images
make docker-up        # Start services (production mode)
make docker-down      # Stop all services
make docker-clean     # Clean Docker images and volumes
make docker-migrate   # Run database migrations

Utility Commands

make deps         # Download Go dependencies
make test         # Run tests
make clean        # Clean build artifacts
make setup        # Setup development environment

Database Commands

make db-connect     # Connect to PostgreSQL
make redis-connect  # Connect to Redis

Service Endpoints

Gateway Service (Port 8000)

All API endpoints are versioned under /api/v1:

  • Health: GET /health
  • Authentication:
    • POST /api/v1/auth/register - Register new user
    • POST /api/v1/auth/login - User login
    • POST /api/v1/auth/refresh - Refresh token
    • POST /api/v1/auth/logout - Logout (requires auth)
    • GET /api/v1/auth/validate - Validate token (requires auth)
  • Users:
    • POST /api/v1/users - Create user (public)
    • GET /api/v1/users/:id - Get user by ID (requires auth)
    • PUT /api/v1/users/:id - Update user (requires auth)
    • DELETE /api/v1/users/:id - Delete user (requires auth)
    • GET /api/v1/users/search - Search users (requires auth)
    • POST /api/v1/users/batch - Get users by IDs (requires auth)
  • Chat:
    • POST /api/v1/chats - Create chat (requires auth)
    • GET /api/v1/chats - List user's chats (requires auth)
    • GET /api/v1/chats/:id - Get chat details (requires auth)
    • POST /api/v1/chats/:id/messages - Send message (requires auth)
    • GET /api/v1/chats/:id/messages - Get chat messages (requires auth)
    • POST /api/v1/chats/join - Join chat (requires auth)
    • POST /api/v1/chats/leave - Leave chat (requires auth)
  • Friends/Subscribers:
    • POST /api/v1/friends/requests - Make friend request (requires auth)
    • POST /api/v1/friends/requests/confirm - Confirm friend request (requires auth)
    • POST /api/v1/friends/requests/decline - Decline friend request (requires auth)
    • GET /api/v1/friends/requests - Get friend requests (requires auth)
    • GET /api/v1/friends - Get user's friends (requires auth)
    • DELETE /api/v1/friends - Remove friend (requires auth)

Individual Services

Each service exposes both gRPC and HTTP endpoints:

  • Auth Service: gRPC(50001), HTTP(8001)
  • User Service: gRPC(50002), HTTP(8002)
  • Chat Service: gRPC(50003), HTTP(8003)
  • Subscriber Service: gRPC(50004), HTTP(8004)

Database Access

Configuration

Environment Variables

Copy .env.example to .env and modify as needed:

cp .env.example .env

Key configuration options:

  • Database connections
  • Redis settings
  • JWT secrets
  • Service ports
  • Logging levels

Development vs Production

Development (docker-compose.override.yml):

  • All services enabled by default
  • Hot reload capabilities
  • Debug logging
  • Exposed ports for direct access

Production (docker-compose.yml):

  • Services organized with profiles
  • Optimized for production deployment
  • Health checks enabled
  • Resource limits applied

Database Migrations

Migrations are automatically run when starting the development environment. To run manually:

make docker-migrate

Each service has its own database:

  • messenger_auth - Auth service
  • messenger_user - User service
  • messenger_chat - Chat service
  • messenger_subscriber - Subscriber service

API Documentation

Authentication

# Register a new user
curl -X POST http://localhost:8000/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123"}'

# Login
curl -X POST http://localhost:8000/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{"email": "user@example.com", "password": "password123"}'

# Refresh token
curl -X POST http://localhost:8000/api/v1/auth/refresh \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <refresh_token>" \
  -d '{}'

# Logout
curl -X POST http://localhost:8000/api/v1/auth/logout \
  -H "Authorization: Bearer <access_token>"

# Validate token
curl -X GET http://localhost:8000/api/v1/auth/validate \
  -H "Authorization: Bearer <access_token>"

User Management

# Create user (public endpoint)
curl -X POST http://localhost:8000/api/v1/users \
  -H "Content-Type: application/json" \
  -d '{"name": "John Doe", "email": "john@example.com"}'

# Get user by ID
curl http://localhost:8000/api/v1/users/1 \
  -H "Authorization: Bearer <access_token>"

# Update user
curl -X PUT http://localhost:8000/api/v1/users/1 \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <access_token>" \
  -d '{"name": "John Updated", "email": "john.updated@example.com"}'

# Search users
curl "http://localhost:8000/api/v1/users/search?query=john" \
  -H "Authorization: Bearer <access_token>"

Chat Management

# Create a chat
curl -X POST http://localhost:8000/api/v1/chats \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <access_token>" \
  -d '{"name": "My Chat Room", "type": "group"}'

# List user's chats
curl http://localhost:8000/api/v1/chats \
  -H "Authorization: Bearer <access_token>"

# Send a message
curl -X POST http://localhost:8000/api/v1/chats/1/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <access_token>" \
  -d '{"content": "Hello, world!", "type": "text"}'

# Get chat messages
curl http://localhost:8000/api/v1/chats/1/messages \
  -H "Authorization: Bearer <access_token>"

Friend Management

# Send friend request
curl -X POST http://localhost:8000/api/v1/friends/requests \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <access_token>" \
  -d '{"user_id": 2}'

# Get friend requests
curl http://localhost:8000/api/v1/friends/requests \
  -H "Authorization: Bearer <access_token>"

# Confirm friend request
curl -X POST http://localhost:8000/api/v1/friends/requests/confirm \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer <access_token>" \
  -d '{"request_id": 1}'

# Get friends list
curl http://localhost:8000/api/v1/friends \
  -H "Authorization: Bearer <access_token>"

Development Workflow

  1. Start development environment:

    make dev-up
  2. Make changes to code

  3. Rebuild specific service:

    docker-compose build <service-name>
    docker-compose up -d <service-name>
  4. View logs:

    make docker-logs
  5. Stop environment:

    make docker-down

Troubleshooting

Common Issues

  1. Port conflicts: Ensure ports 5432, 6379, 8000-8004, and 50001-50004 are available
  2. Database connection issues: Wait for PostgreSQL to be fully ready before starting services
  3. Proto generation fails: Ensure protoc and Go protobuf plugins are installed
  4. 404 errors: Make sure to use the correct API path with /api/v1 prefix

Debugging

  1. Check service logs:

    docker logs messenger-<service-name>
  2. Access service containers:

    docker exec -it messenger-<service-name> /bin/sh
  3. Check database:

    make db-connect
  4. Test health endpoints:

    # Gateway health
    curl http://localhost:8000/health
    
    # Individual service health (if exposed)
    curl http://localhost:8002/health  # User service
    curl http://localhost:8001/health  # Auth service

Reset Environment

# Complete reset
make docker-down
make docker-clean
docker-compose up -d

Image