A scalable, secure chat application backend built with Go, featuring real-time messaging, file attachments, end-to-end encryption, and modern architecture patterns.
- User Management: Registration, authentication, profile management
- Real-time Chat: Direct messages and group conversations
- File Attachments: Secure file upload with S3-compatible storage
- End-to-End Encryption: AES encryption for message security
- Message Status: Read receipts, delivery confirmations
- Session Management: JWT-based authentication with refresh tokens
- Scalable Architecture: Clean separation of concerns with repository pattern
βββ cmd/server/ # Application entry point
βββ internal/
β βββ api/ # HTTP routing and middleware
β βββ user/ # User management (auth, profiles)
β βββ chatroom/ # Chat room operations
β βββ message/ # Message handling and encryption
β βββ models/ # Database models
β βββ repository/ # Data access layer
β βββ config/ # Configuration management
βββ pkg/
β βββ auth/ # JWT and password utilities
β βββ encryption/ # AES encryption service
β βββ middleware/ # HTTP middleware (CORS, auth)
β βββ s3/ # File storage service
βββ migrations/ # Database schema migrations
βββ scripts/ # Database migration scripts
- Language: Go 1.23
- Framework: Gin (HTTP web framework)
- Database: PostgreSQL with GORM
- Cache: Redis
- Storage: S3-compatible (MinIO)
- Authentication: JWT tokens
- Encryption: AES-256
- Migration: golang-migrate
- Go 1.23+
- PostgreSQL 16+
- Redis
- MinIO (or AWS S3)
- golang-migrate CLI
git clone <repository-url>
cd mozho_chat
go mod downloadCreate a .env file in the project root:
# Database
POSTGRES_URL=postgres://username:password@localhost:5432/mozho_chat?sslmode=disable
POSTGRES_USER=your_username
POSTGRES_PASSWORD=your_password
POSTGRES_DB=mozho_chat
POSTGRES_PORT=5432
# Redis
REDIS_ADDR=localhost:6379
REDIS_PASS=
REDIS_DB=0
# AWS S3 (or MinIO)
AWS_REGION=us-east-1
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
S3_BUCKET_NAME=mozho-chat-files
S3_ENDPOINT=http://localhost:9000 # For MinIO# Start PostgreSQL and Redis using Docker Compose
docker-compose up -d postgres redis minio
# Run database migrations
chmod +x scripts/migrate-up.sh
./scripts/migrate-up.shgo run cmd/server/main.goThe server will start on http://localhost:8080
http://localhost:8080/api/v1
Most endpoints require authentication. Include the JWT token in the Authorization header:
Authorization: Bearer <your_jwt_token>
POST /users/register
Content-Type: application/json
{
"username": "johndoe",
"email": "john@example.com",
"password": "securepassword"
}POST /users/login
Content-Type: application/json
{
"email": "john@example.com",
"password": "securepassword"
}GET /users/me
Authorization: Bearer <token>PATCH /users/me
Authorization: Bearer <token>
Content-Type: application/json
{
"username": "newusername",
"profile": {
"full_name": "John Doe",
"bio": "Software Developer",
"avatar_url": "https://example.com/avatar.jpg"
}
}POST /chatrooms
Authorization: Bearer <token>
Content-Type: application/json
{
"other_user_id": "uuid-of-other-user"
}GET /chatrooms/{room_id}
Authorization: Bearer <token>GET /chatrooms
Authorization: Bearer <token>POST /chatrooms/{room_id}/join
Authorization: Bearer <token>POST /chatrooms/{room_id}/leave
Authorization: Bearer <token>POST /messages/send
Authorization: Bearer <token>
Content-Type: multipart/form-data
receiver_id: uuid-of-receiver
content: Hello, this is a test message
algorithm: aes-256-gcm
encryption_key: base64-encoded-key
attachments: [file1, file2, ...]GET /messages/{chat_room_id}?limit=20&offset=0
Authorization: Bearer <token>POST /messages/{message_id}/read
Authorization: Bearer <token>POST /messages/{message_id}/delivered
Authorization: Bearer <token>POST /messages/generate-key
Authorization: Bearer <token>- JWT-based authentication
- Password hashing with bcrypt
- Session management with refresh tokens
- End-to-end message encryption using AES-256-GCM
- Client-side key generation
- Secure key exchange
- Secure file upload to S3-compatible storage
- File metadata stored in database
- Access control for attachments
The application uses PostgreSQL with the following main entities:
- Users: User accounts with profile information
- Chat Rooms: Conversation containers (direct messages or groups)
- Chat Room Members: User-room relationships
- Messages: Chat messages with encryption support
- Message Status: Read/delivery status tracking
- Sessions: User authentication sessions
- User Public Keys: Encryption key management
- Message Attachments: File attachment metadata
# Run tests
go test ./...
# Run specific test packages
go test ./tests/The project includes Docker Compose configuration for development:
# Start all services
docker-compose up -d
# Stop services
docker-compose down
# View logs
docker-compose logs -fServices included:
- PostgreSQL 16
- Redis
- MinIO (S3-compatible storage)
# Apply migrations
./scripts/migrate-up.sh
# Rollback migrations
./scripts/migrate-down.sh
# Create new migration
migrate create -ext sql -dir migrations -seq <migration_name>- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Check the API documentation above
- Review the code comments for implementation details