A microservice for user management in a social media application, built with Go and gRPC.
The User Service is responsible for managing user accounts, authentication, and profile information for the social media platform. It provides core functionality such as user registration, login, profile updates, and user data retrieval. The service uses gRPC for communication with other services in the microservices architecture.
- Go 1.23 or later
- PostgreSQL database
- RabbitMQ (for event-driven communication with other services)
Create a .env file in the root directory with the following variables:
PORT=":50053"
DB_URL="postgres://username:password@host:port/database?sslmode=disable"
# DB_URL="postgres://username:password@db:port/database?sslmode=disable" // FOR DOCKER COMPOSE
EMAIL="EMAIL_FOR_SENDING_EMAILS"
EMAIL_SECRET="SECRET_FOR_CONFIRMING_SENDING_EMAILS"
TOKEN_SECRET="YOUR_JWT_SECRET_KEY"
RABBITMQ_URL="amqp://username:password@host:port"This service uses Goose for database migrations:
# Install Goose
go install github.com/pressly/goose/v3/cmd/goose@latest
# Run migrations
goose -dir migrations postgres "YOUR_DB_CONNECTION_STRING" upThe service implements the following gRPC methods:
Registers a new user in the system.
{
"username": "johndoe",
"email": "john.doe@example.com",
"password": "securepassword",
"full_name": "John Doe"
}{
"user": {
"id": "UUID of the created user",
"username": "johndoe",
"email": "john.doe@example.com",
"full_name": "John Doe",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z"
},
"token": "JWT authentication token"
}Authenticates a user and returns a JWT token.
{
"username_or_email": "johndoe OR john.doe@example.com",
"password": "securepassword"
}{
"user": {
"id": "UUID of the user",
"username": "johndoe",
"email": "john.doe@example.com",
"full_name": "John Doe",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z"
},
"token": "JWT authentication token"
}Retrieves a user's profile information.
{
"user_id": "UUID of the user"
}{
"user": {
"id": "UUID of the user",
"username": "johndoe",
"email": "john.doe@example.com",
"full_name": "John Doe",
"bio": "User biography text",
"profile_picture_url": "URL to profile picture",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z"
}
}Updates a user's profile information.
{
"user_id": "UUID of the user",
"full_name": "Updated Name",
"bio": "Updated biography",
"profile_picture_url": "URL to new profile picture"
}{
"user": {
"id": "UUID of the user",
"username": "johndoe",
"email": "john.doe@example.com",
"full_name": "Updated Name",
"bio": "Updated biography",
"profile_picture_url": "URL to new profile picture",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z"
}
}Changes a user's password.
{
"user_id": "UUID of the user",
"current_password": "currentSecurePassword",
"new_password": "newSecurePassword"
}{
"success": true,
"message": "Password successfully changed"
}Validates a JWT token and returns the associated user information.
{
"token": "JWT token string"
}{
"valid": true,
"user_id": "UUID of the user if token is valid",
"username": "Username if token is valid",
"claims": {
"additional": "claims",
"from": "token"
}
}Retrieves a user's information by their ID.
{
"id": "UUID of the user"
}{
"user": {
"id": "UUID of the user",
"username": "johndoe",
"email": "john.doe@example.com",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z",
"subscribers": "Comma-separated list of subscriber IDs",
"subscribed_to": "Comma-separated list of user IDs the user is subscribed to",
"is_premium": false,
"verification_code": 123456,
"is_verified": true
}
}Retrieves a user's information by their email or username.
{
"identifier": "johndoe OR john.doe@example.com"
}{
"user": {
"id": "UUID of the user",
"username": "johndoe",
"email": "john.doe@example.com",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z",
"subscribers": "Comma-separated list of subscriber IDs",
"subscribed_to": "Comma-separated list of user IDs the user is subscribed to",
"is_premium": false,
"verification_code": 123456,
"is_verified": true
}
}Validates a JWT token and returns the associated user information.
{
"token": "JWT token string"
}{
"user": {
"id": "UUID of the user",
"username": "johndoe",
"email": "john.doe@example.com",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z",
"subscribers": "Comma-separated list of subscriber IDs",
"subscribed_to": "Comma-separated list of user IDs the user is subscribed to",
"is_premium": false,
"verification_code": 123456,
"is_verified": true
}
}Retrieves information for all users in the system.
{}{
"users": [
{
"id": "UUID of the user 1",
"username": "johndoe",
"email": "john.doe@example.com",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z",
"subscribers": "Comma-separated list of subscriber IDs",
"subscribed_to": "Comma-separated list of user IDs the user is subscribed to",
"is_premium": false,
"verification_code": 123456,
"is_verified": true
},
{
"id": "UUID of the user 2",
// Other user properties
}
]
}Changes a user's username.
{
"username": "newUsername"
}{
"user": {
"id": "UUID of the user",
"username": "newUsername",
"email": "john.doe@example.com",
"created_at": "2023-01-01T12:00:00Z",
"updated_at": "2023-01-01T12:00:00Z",
"subscribers": "Comma-separated list of subscriber IDs",
"subscribed_to": "Comma-separated list of user IDs the user is subscribed to",
"is_premium": false,
"verification_code": 123456,
"is_verified": true
}
}Changes a user's password.
{
"password": "newSecurePassword"
}{
"status": "Password successfully changed"
}Subscribes a user to another user.
{
"user_id": "UUID of the user to subscribe to"
}{
"status": true
}Unsubscribes a user from another user.
{
"user_id": "UUID of the user to unsubscribe from"
}{
"status": true
}Sends a verification code to a user's email.
{
// Request details would depend on implementation
}{
"status": "Verification code sent successfully"
}Resets a user's password using a verification code.
{
"new_password": "newSecurePassword",
"verification_code": 123456
}{
"status": "Password reset successfully"
}Deletes a user account.
{
"password": "currentPassword",
"verify_message": "I understand this action cannot be undone"
}{
"status": "User account deleted successfully"
}Deletes all user accounts (admin function).
{}{
"status": "All user accounts deleted successfully"
}The User Service publishes events to RabbitMQ when significant user actions occur, enabling other services to react accordingly.
The service publishes events to:
- Exchange:
users.topic(topic exchange) - Routing Keys:
user.registered- When a new user registersuser.updated- When a user updates their profileuser.deleted- When a user account is deleted
{
"event_type": "user.registered",
"user_id": "UUID of the user",
"username": "johndoe",
"timestamp": "2023-01-01T12:00:00Z",
"data": {
"email": "john.doe@example.com",
"full_name": "John Doe"
}
}go run cmd/main.goThe service can be run using Docker:
# Build the Docker image
docker build -t user-service .
# Run the container
docker run -p 50053:50053 user-serviceWhen deploying to different CPU architectures:
docker build --platform=linux/amd64 -t user-service .For more details on Docker deployment, see README.Docker.md.