7.0 KiB
Member Service: User Account Management System
The Member Service is a comprehensive user account management system designed to handle user authentication, account creation, and profile management. This service provides a robust backend for applications requiring user management functionality, supporting various authentication methods and integrating with external services.
The system is built using a microservices architecture, leveraging technologies such as MongoDB for data storage, Redis for caching, and Etcd for distributed configuration. It offers a wide range of features including user registration, account binding, profile management, and support for third-party authentication providers like Google and LINE.
Key features of the Member Service include:
- User account creation and management
- Multiple authentication methods (email, phone, Google, LINE)
- Account binding for different platforms
- User profile management
- Refresh token generation and verification
- Scalable architecture using microservices
- Integration with MongoDB, Redis, and Etcd
- Docker support for easy deployment and scaling
Repository Structure
.
├── docker-compose.yml
├── etc/
│ ├── member.example.yaml
│ └── member.yaml
├── internal/
│ ├── config/
│ ├── logic/account/
│ ├── server/account/
│ └── svc/
├── member.go
├── pkg/
│ ├── domain/
│ ├── mock/
│ ├── repository/
│ └── usecase/
└── readme.md
Key Files and Directories:
docker-compose.yml
: Defines the multi-container Docker environmentetc/member.yaml
: Configuration file for the Member Serviceinternal/
: Contains internal application codelogic/account/
: Business logic for account-related operationsserver/account/
: gRPC server implementation for account service
pkg/
: Reusable package codedomain/
: Domain models and interfacesrepository/
: Data access layer implementationsusecase/
: Use case implementations
member.go
: Main entry point for the Member Service
Usage Instructions
Prerequisites
- Go 1.16 or later
- Docker and Docker Compose
- MongoDB 8.0
- Etcd 3.5.5
- Redis 7.0
Installation
-
Clone the repository:
git clone <repository_url> cd <repository_directory>
-
Start the required services using Docker Compose:
docker-compose up -d
-
Copy the example configuration file and modify it as needed:
cp etc/member.example.yaml etc/member.yaml
-
Build and run the Member Service:
go build ./member
Configuration
The etc/member.yaml
file contains the main configuration for the Member Service. Key configuration options include:
ListenOn
: The address and port the service listens onEtcd
: Configuration for Etcd connectionCache
: Redis cache configurationMongo
: MongoDB connection detailsBcrypt
: Password hashing costGoogleAuth
: Google authentication settingsLineAuth
: LINE authentication settings
Ensure all configuration values are set correctly before starting the service.
Common Use Cases
-
Creating a new user account:
resp, err := accountService.CreateUserAccount(ctx, &account.CreateUserAccountReq{ Email: "user@example.com", Password: "securepassword", })
-
Authenticating a user:
resp, err := accountService.VerifyPlatformAuthResult(ctx, &account.VerifyPlatformAuthResultReq{ Platform: member.Platform_EMAIL, Email: "user@example.com", Password: "securepassword", })
-
Updating user information:
resp, err := accountService.UpdateUserInfo(ctx, &account.UpdateUserInfoReq{ Uid: "user123", Nickname: "New Nickname", })
Testing & Quality
To run the test suite:
go test ./...
Troubleshooting
-
Connection issues with MongoDB:
- Ensure MongoDB is running and accessible
- Check the MongoDB connection string in
etc/member.yaml
- Verify network connectivity between the service and MongoDB
-
Authentication failures:
- Double-check the configuration for Google and LINE authentication in
etc/member.yaml
- Ensure the correct client IDs and secrets are set
- Double-check the configuration for Google and LINE authentication in
-
Performance issues:
- Monitor Redis cache usage and adjust cache settings if necessary
- Check MongoDB query performance and add indexes if needed
- Use Go's pprof tool to profile the application:
go tool pprof http://localhost:8080/debug/pprof/profile
Data Flow
The Member Service follows a typical request-response flow:
- Client sends a request to the gRPC server
- Server routes the request to the appropriate logic handler
- Logic handler processes the request, interacting with repositories as needed
- Repositories interact with the database (MongoDB) or cache (Redis)
- Results are returned through the logic handler back to the client
Client -> gRPC Server -> Logic Handler -> Repository -> Database/Cache
^ |
| |
+---------------------------------------------------------+
Important technical considerations:
- Use of caching to improve performance for frequently accessed data
- Proper error handling and logging throughout the flow
- Secure handling of sensitive information (e.g., passwords, tokens)
Deployment
The Member Service can be deployed using Docker containers. The docker-compose.yml
file provides a template for deploying the service along with its dependencies (MongoDB, Etcd, and Redis).
To deploy:
-
Ensure Docker and Docker Compose are installed on the target system
-
Modify the
docker-compose.yml
file if necessary (e.g., to change ports or add volumes) -
Run the following command in the project root:
docker-compose up -d
-
Monitor the logs to ensure all services start correctly:
docker-compose logs -f
For production deployments, consider using orchestration tools like Kubernetes for better scalability and management.
Infrastructure
The Member Service relies on the following infrastructure components:
-
MongoDB (mongo):
- Image: mongo:8.0
- Purpose: Primary data storage for user accounts and related information
- Configuration:
- Root username and password set via environment variables
-
Etcd (etcd):
- Image: quay.io/coreos/etcd:v3.5.5
- Purpose: Distributed key-value store for service discovery and configuration
- Configuration:
- Listens on port 2379 for client connections
- Data directory set to /etcd-data
-
Redis (redis):
- Image: redis:7.0
- Purpose: Caching layer for improved performance
- Configuration:
- Default configuration with no password
These infrastructure components are defined in the docker-compose.yml
file, allowing for easy local development and testing. For production deployments, these services should be properly secured and scaled according to the application's needs.