A microservices-based backend system for the SquidPong application, built with Node.js, TypeScript, and Fastify.
- Architecture
- Services
- Prerequisites
- Quick Start
- Development
- API Documentation
- Database
- Message Queue & Caching
- File Uploads
- Environment Variables
- Docker
- Contributing
SquidPong Backend follows a microservices architecture with the following components:
βββββββββββββββββββ βββββββββββββββ
β Frontend ββββββ Caddy β
β (React/Vue) β β (Reverse β
βββββββββββββββββββ β Proxy) β
βββββββββββββββ
β
βββββββββββββββ
β Gateway β
β (Fastify) β
βββββββββββββββ
β
βββββββββββββββββββββββΌββββββββββββββββββββββ
β β β
βββββββββββ βββββββββββββββ βββββββββββββββ
β Auth β β User β β Chat β
βService β β Service β β Service β
βββββββββββ βββββββββββββββ βββββββββββββββ
β β β
βββββββββββ βββββββββββββββ βββββββββββββββ
βTournamentβ β Notify β β Game β
βService β β Service β β Service β
βββββββββββ βββββββββββββββ βββββββββββββββ
β β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Shared Services β
β βββββββββββ βββββββββββ βββββββββββββββββββββββ β
β β Redis β βRabbitMQ β β SQLite DBs β β
β β(Cache) β β (Queue) β β(Auth/User/Chat/etc) β β
β βββββββββββ βββββββββββ βββββββββββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- Port: 4000
- Role: API Gateway, routing, authentication, and request proxying
- Tech Stack: Fastify, TypeScript
- Features:
- Request routing to microservices
- Authentication middleware
- WebSocket support
- CORS handling
- Port: 5001 (Prisma Studio)
- Role: User authentication and authorization
- Features:
- JWT token management
- OAuth2 integration
- 2FA support (TOTP)
- Session management
- Port: 5002 (Prisma Studio)
- Role: User profile and account management
- Features:
- User profile CRUD operations
- Avatar uploads
- User preferences
- Port: 5004 (Prisma Studio)
- Role: Real-time messaging
- Features:
- Direct messaging
- Group chat
- File sharing
- Message history
- Port: 5003 (Prisma Studio)
- Role: Push notifications and alerts
- Features:
- Real-time notifications
- Email notifications
- Push notifications
- Role: Tournament and competition management
- Features:
- Tournament creation and management
- Bracket generation
- Scoring system
- Port: 3000 (Game Server), 5005 (Prisma Studio)
- Role: Real-time game logic and state management
- Tech Stack: Colyseus (Real-time multiplayer framework)
- Features:
- Game room management
- Real-time game state synchronization
- Player matchmaking
- Docker (v20.10+)
- Docker Compose (v2.0+)
- Make (usually pre-installed on Linux/macOS)
That's it! π No need to install Node.js, npm, databases, or any other dependencies - everything runs in Docker containers.
Note: Node.js (v18+) and npm are only needed if you want to develop outside of Docker containers.
-
Clone the repository
git clone https://github.com/TRAN5PONG/SquidPong-Backend.git cd SquidPong-Backend -
Set up environment variables (optional - services work with defaults)
# Copy example env files if you need custom configuration cp backend/gateway/.env.example backend/gateway/.env cp backend/services/auth/.env.example backend/services/auth/.env # ... other services if needed
-
Start everything with one command β‘
make up
That's it! This single command will:
- Build all Docker images
- Start all microservices
- Initialize databases with migrations
- Set up Redis and RabbitMQ
- Configure the Gateway and Caddy proxy
-
Access the application
- Frontend: http://localhost:8080
- API Gateway: http://localhost:4000
- Prisma Studio (Auth): http://localhost:5001
- Prisma Studio (User): http://localhost:5002
- Prisma Studio (Chat): http://localhost:5004
- Prisma Studio (Notify): http://localhost:5003
make up # π Start everything (the only command you usually need!)
make down # π Stop all services
make fclean # π§Ή Complete cleanup (removes all data)
make re # π Full restart (clean + start)For most developers, you only need:
# Clone and start everything
git clone https://github.com/TRAN5PONG/SquidPong-Backend.git
cd SquidPong-Backend
make upThat's it! The Makefile handles everything automatically:
- ποΈ Building all Docker images
- ποΈ Database migrations for all services
- π Starting all microservices
- π§ Setting up Redis and RabbitMQ
- π Configuring reverse proxy
Only needed if you want to develop outside Docker or debug individual services:
-
Install dependencies (for each service)
cd backend/gateway && npm install cd ../services/auth && npm install cd ../user && npm install cd ../chat && npm install cd ../notify && npm install cd ../tournament && npm install cd ../game && npm install
-
Run database migrations (only if developing locally)
# Each service with Prisma needs its own migration cd backend/services/auth && npx prisma migrate dev --name init cd ../user && npx prisma migrate dev --name init cd ../chat && npx prisma migrate dev --name init cd ../notify && npx prisma migrate dev --name init cd ../tournament && npx prisma migrate dev --name init cd ../game && npx prisma migrate dev --name init
-
Start services individually (for debugging)
# Gateway cd backend/gateway && npm run dev # Auth Service cd backend/services/auth && npm run dev # Other services...
π‘ TL;DR: Just use
make upto start everything!
make up # π Start everything (builds images, runs migrations, starts all services)
make down # π Stop all services gracefully
make fclean # π§Ή Complete cleanup (β οΈ deletes all data)
make re # π Full restart (fclean + up)Detailed Command Breakdown:
-
make up- The main command you'll use β- Automatically builds all Docker images
- Starts Redis, RabbitMQ, and all databases
- Runs Prisma migrations for each service
- Launches all microservices with hot reload
- Sets up Caddy reverse proxy
- No manual setup required!
-
make down- Clean shutdown- Stops all running containers
- Removes created Docker images to free space
- Keeps database data intact
-
make fclean- Nuclear option (use carefully)- Stops all containers
- Removes all volumes
β οΈ deletes all SQLite databases - Removes all images
- Use when you want a completely fresh start
-
make re- Complete reset- Equivalent to
make fclean && make up - Fresh installation from scratch
- Equivalent to
API documentation is available via Swagger UI:
- Auth Service:
http://localhost:4000/api/auth/docs - User Service:
http://localhost:4000/api/user/docs - Chat Service:
http://localhost:4000/api/chat/docs - Other services follow the same pattern
Each microservice maintains its own SQLite database with Prisma ORM for data isolation and service independence:
- Auth Service:
backend/services/auth/prisma/auth.db- User authentication, tokens, sessions - User Service:
backend/services/user/prisma/user.db- User profiles, preferences, avatars - Chat Service:
backend/services/chat/prisma/chat.db- Messages, conversations, chat groups - Notification Service:
backend/services/notify/prisma/notify.db- Notifications, alerts, push settings - Tournament Service:
backend/services/tournament/prisma/tournament.db- Tournament data, brackets - Game Service:
backend/services/game/prisma/game.db- Game states, matches, scores
Each service manages its own database independently:
# Navigate to specific service directory
cd backend/services/[service-name]
# Generate Prisma client for the service
npx prisma generate
# Run migrations for the service
npx prisma migrate dev
# Access Prisma Studio for the service (runs on service-specific port)
npx prisma studioService-specific Prisma Studio ports:
- Auth: http://localhost:5001
- User: http://localhost:5002
- Notify: http://localhost:5003
- Chat: http://localhost:5004
- Game: http://localhost:5005
- Usage: Caching, session storage, real-time data
- Container:
redis - Image:
redis/redis-stack-server:latest
- Usage: Asynchronous message processing between services
- Container:
rabbitmq - Image:
rabbitmq:latest
File uploads are organized in the uploads/ directory:
uploads/
βββ user/
β βββ avatar/ # User avatars
β βββ default.png
β βββ [user-files]
βββ chat/
βββ group/ # Group chat files
βββ default.png
βββ [group-files]
Create .env files for each service with the following variables:
PORT=4000
JWT_SECRET=your-jwt-secret
REDIS_URL=redis://redis:6379
RABBITMQ_URL=amqp://rabbitmq:5672PORT=3000
DATABASE_URL="file:./auth.db"
JWT_SECRET=your-jwt-secret
OAUTH_CLIENT_ID=your-oauth-client-id
OAUTH_CLIENT_SECRET=your-oauth-client-secretSimilar pattern with service-specific configurations.
make up # Start everything
make down # Stop everything If you prefer using Docker directly instead of Make:
# Development
docker compose up --build
# Production
docker compose -f docker-compose.prod.yml up --buildWhy use Make instead?
- β
Shorter commands (
make upvsdocker compose up --build) - β Consistent across different environments
- β Built-in cleanup and restart options
- β Handles complex Docker operations automatically
- Runtime: Node.js 18+
- Language: TypeScript
- Web Framework: Fastify
- Database: SQLite with Prisma ORM
- Cache: Redis
- Message Queue: RabbitMQ
- Authentication: JWT, OAuth2, TOTP (2FA)
- Real-time: WebSockets, Colyseus (for games)
- Reverse Proxy: Caddy
- Containerization: Docker & Docker Compose
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the ISC License.
- Port conflicts: Make sure ports 8080, 4000, 5001-5005 are available
- Database migrations: Each service has its own SQLite database - run migrations individually:
cd backend/services/[service-name] && npx prisma migrate dev --name init
- Permission issues: Ensure Docker has proper permissions for volume mounts and SQLite files
- Service startup order: Services wait 6 seconds before starting to ensure dependencies are ready
- Database reset: Use
make fcleancarefully - it will delete all SQLite database files - Individual service debugging: Check specific service logs:
docker compose logs -f [service-name]
View service logs:
docker compose logs -f [service-name]
# Example: docker compose logs -f gatewayFor support, please open an issue on the GitHub repository or contact the development team.