220 lines
7.0 KiB
Markdown
220 lines
7.0 KiB
Markdown
# 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 environment
|
|
- `etc/member.yaml`: Configuration file for the Member Service
|
|
- `internal/`: Contains internal application code
|
|
- `logic/account/`: Business logic for account-related operations
|
|
- `server/account/`: gRPC server implementation for account service
|
|
- `pkg/`: Reusable package code
|
|
- `domain/`: Domain models and interfaces
|
|
- `repository/`: Data access layer implementations
|
|
- `usecase/`: 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
|
|
|
|
1. Clone the repository:
|
|
```
|
|
git clone <repository_url>
|
|
cd <repository_directory>
|
|
```
|
|
|
|
2. Start the required services using Docker Compose:
|
|
```
|
|
docker-compose up -d
|
|
```
|
|
|
|
3. Copy the example configuration file and modify it as needed:
|
|
```
|
|
cp etc/member.example.yaml etc/member.yaml
|
|
```
|
|
|
|
4. 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 on
|
|
- `Etcd`: Configuration for Etcd connection
|
|
- `Cache`: Redis cache configuration
|
|
- `Mongo`: MongoDB connection details
|
|
- `Bcrypt`: Password hashing cost
|
|
- `GoogleAuth`: Google authentication settings
|
|
- `LineAuth`: LINE authentication settings
|
|
|
|
Ensure all configuration values are set correctly before starting the service.
|
|
|
|
### Common Use Cases
|
|
|
|
1. Creating a new user account:
|
|
```go
|
|
resp, err := accountService.CreateUserAccount(ctx, &account.CreateUserAccountReq{
|
|
Email: "user@example.com",
|
|
Password: "securepassword",
|
|
})
|
|
```
|
|
|
|
2. Authenticating a user:
|
|
```go
|
|
resp, err := accountService.VerifyPlatformAuthResult(ctx, &account.VerifyPlatformAuthResultReq{
|
|
Platform: member.Platform_EMAIL,
|
|
Email: "user@example.com",
|
|
Password: "securepassword",
|
|
})
|
|
```
|
|
|
|
3. Updating user information:
|
|
```go
|
|
resp, err := accountService.UpdateUserInfo(ctx, &account.UpdateUserInfoReq{
|
|
Uid: "user123",
|
|
Nickname: "New Nickname",
|
|
})
|
|
```
|
|
|
|
### Testing & Quality
|
|
|
|
To run the test suite:
|
|
|
|
```
|
|
go test ./...
|
|
```
|
|
|
|
### Troubleshooting
|
|
|
|
1. 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
|
|
|
|
2. Authentication failures:
|
|
- Double-check the configuration for Google and LINE authentication in `etc/member.yaml`
|
|
- Ensure the correct client IDs and secrets are set
|
|
|
|
3. 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:
|
|
|
|
1. Client sends a request to the gRPC server
|
|
2. Server routes the request to the appropriate logic handler
|
|
3. Logic handler processes the request, interacting with repositories as needed
|
|
4. Repositories interact with the database (MongoDB) or cache (Redis)
|
|
5. 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:
|
|
|
|
1. Ensure Docker and Docker Compose are installed on the target system
|
|
2. Modify the `docker-compose.yml` file if necessary (e.g., to change ports or add volumes)
|
|
3. Run the following command in the project root:
|
|
```
|
|
docker-compose up -d
|
|
```
|
|
|
|
4. 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. |