Microservice responsible for user management, profiles, interests, and user-related operations.
The Users Service handles all user-related functionality including:
- User registration and profile management
- User search and discovery
- Interest management
- User synchronization with Firebase
- Profile picture management
API Gateway β Composite Service β Users Service β User Database (Cloud SQL)
- Port: 8001
- Database: MySQL (Cloud SQL or local)
- Authentication: Trusts
x-firebase-uidheader from API Gateway
- Python 3.9+
- MySQL 8.0+
- Firebase service account key
-
Install dependencies
pip install -r requirements.txt
-
Set up database
mysql -u root -p user_db < ../DB-Service/initUser.sql -
Configure environment variables Create a
.envfile:DB_HOST=127.0.0.1 DB_USER=root DB_PASS=your_password DB_NAME=user_db FIREBASE_SERVICE_ACCOUNT_PATH=./serviceAccountKey.json
-
Add Firebase service account key
- Download from Firebase Console
- Place as
serviceAccountKey.jsonin service directory
-
Run the service
uvicorn main:app --host 0.0.0.0 --port 8001
| Variable | Description | Default | Required |
|---|---|---|---|
DB_HOST |
Database host address | 127.0.0.1 |
Yes |
DB_USER |
Database username | root |
Yes |
DB_PASS |
Database password | - | Yes |
DB_NAME |
Database name | user_db |
Yes |
FIREBASE_SERVICE_ACCOUNT_PATH |
Path to Firebase service account JSON | ./serviceAccountKey.json |
No |
Get all users (excluding sensitive information)
Headers:
x-firebase-uid: Firebase user ID (injected by API Gateway)
Response:
[
{
"user_id": 1,
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"email": "john@example.com",
"profile_picture": "https://...",
"created_at": "2024-01-01T00:00:00"
}
]Get current authenticated user's profile
Headers:
x-firebase-uid: Firebase user ID
Response:
{
"user_id": 1,
"firebase_uid": "abc123",
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"email": "john@example.com",
"profile_picture": "https://...",
"created_at": "2024-01-01T00:00:00"
}ETag Support: Returns ETag header for caching
Get user by ID
Get user by username
Create new user (sync from Firebase)
Request Body:
{
"firebase_uid": "abc123",
"email": "john@example.com",
"first_name": "John",
"last_name": "Doe",
"username": "johndoe"
}Update current user's profile
Request Body:
{
"first_name": "John",
"last_name": "Doe",
"username": "johndoe",
"profile_picture": "https://..."
}Search users by query string
Query Parameters:
q: Search query (required)skip: Pagination offset (default: 0)limit: Results per page (default: 10, max: 100)
Get all available interests
Get current user's interests
Add interests to current user
Request Body:
{
"interest_ids": [1, 2, 3]
}Remove interest from current user
Sync user from Firebase (internal endpoint)
Request Body:
{
"firebase_uid": "abc123",
"email": "john@example.com",
"first_name": "John",
"last_name": "Doe"
}This service does not perform Firebase authentication directly. It trusts the x-firebase-uid header injected by the API Gateway middleware.
Authentication Flow:
- Client sends request with
Authorization: Bearer <firebase-token>to API Gateway - API Gateway validates token and extracts
firebase_uid - API Gateway forwards request with
x-firebase-uidheader - Users Service uses
x-firebase-uidto identify the user
user_id(INT, PRIMARY KEY, AUTO_INCREMENT)firebase_uid(VARCHAR, UNIQUE)email(VARCHAR, UNIQUE)username(VARCHAR, UNIQUE)first_name(VARCHAR)last_name(VARCHAR)profile_picture(TEXT)created_at(TIMESTAMP)
interest_id(INT, PRIMARY KEY)interest_name(VARCHAR, UNIQUE)
user_id(INT, FOREIGN KEY)interest_id(INT, FOREIGN KEY)
docker build -t users-service .docker run -p 8001:8001 \
-e DB_HOST=your_db_host \
-e DB_USER=your_db_user \
-e DB_PASS=your_db_password \
-e DB_NAME=user_db \
users-serviceThe service is deployed to Cloud Run with:
- VPC Connector for database access
- Private IP connection to Cloud SQL
- Environment variables configured via deployment script
See ../GCP_DEPLOYMENT_GUIDE.md for details.
curl http://localhost:8001/curl -H "x-firebase-uid: your-firebase-uid" \
http://localhost:8001/users/mecurl -H "x-firebase-uid: your-firebase-uid" \
"http://localhost:8001/users/search?q=john"Interactive API documentation available at:
- Swagger UI:
http://localhost:8001/docs - ReDoc:
http://localhost:8001/redoc - OpenAPI JSON:
http://localhost:8001/openapi.json
The service returns standard HTTP status codes:
200 OK: Successful request201 Created: Resource created400 Bad Request: Invalid request data401 Unauthorized: Missing or invalidx-firebase-uidheader404 Not Found: Resource not found409 Conflict: Duplicate resource (e.g., username already exists)500 Internal Server Error: Server error
- The service uses MySQL connector with dictionary cursor for JSON-like responses
- ETag support is implemented for
/users/meendpoint for caching - All user data is validated before database operations
- Username and email must be unique
When adding new endpoints:
- Add route to
routers/users.py - Use
get_firebase_uid_from_header()helper for authentication - Add proper error handling
- Update this README with endpoint documentation