-
Golang: https://go.dev/
-
gRPC: https://grpc.io/docs/
- Docker
- grpcurl
The application follows a clean architecture pattern to keep the code simple, organizable, and maintainable. The main components are:
-
Transport Layer: Handles gRPC requests and responses.
-
Business Layer: Contains the business logic.
-
Repository Layer: Interacts with the database using GORM.
PG_USER=postgres
PG_PASSWORD=Admin123
PG_DATABASE=todo
PG_PORT=5432
PG_HOST=todo-db
PG_CONNECT_TIMEOUT=300
PG_TIMEZOME=UTCStart the server and database containers using Docker Compose:
docker-compose --profile dev build
docker-compose --profile dev up -dproduction:Build the production image and run the database migrations:
docker-compose --profile prod build
docker-compose run --rm api-prod migrateif you want to migrate a single table
docker-compose run --rm api-prod migrate -table table_namedev:Build the production image and run the database migrations:
docker cp ./data_init.sql todo-db:/docker-entrypoint-initdb.d/data_init.sql
docker exec -i todo-db psql -U postgres -d todo -f /docker-entrypoint-initdb.d/data_init.sqlgrpcurl -d '{ "name": "Nguyen Van A", "password": "123456" }' -plaintext localhost:50051 user.TodoService/SignUpgrpcurl -d '{ "name": "ducpv", "password": "123456" }' -plaintext localhost:50051 user.TodoService/Logingrpcurl -H "Authorization: Bearer <your-token>" -d '{"user_id":"1", "title": "New Task", "description": "Task description", "progress": "do", "priority":"1"}' -plaintext localhost:50051 task.TodoService/CreateTaskExample:
grpcurl -H "Authorization: Bearer <eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXlsb2FkIjp7InVzZXJfaWQiOjF9LCJleHAiOjE4MzQ5Mjc0NDMsImlhdCI6MTczNDkyNzQ0M30.N0OkuJzmP3_pNwk5urtKwSH8__vKwglKGbHmDTTeu6kJQDPSpoLYcmUKC8dXCOTnnTRwTYeBwCNwaLPRjKxeZw>" -d '{"user_id":"1", "title": "New Task", "description": "Task description", "progress": "do", "priority":"1"}' -plaintext localhost:50051 task.TodoService/CreateTaskgrpcurl -d '{"user_id":"1"}' -H 'Authorization: Bearer <token>' -plaintext localhost:50051 task.TodoService/ReadTaskgrpcurl -d '{"id":3,"title":"Update Title"}' -H 'Authorization: Bearer <token>' -plaintext localhost:50051 task.TodoService/UpdateTaskgrpcurl -d '{"id":6}' -H 'Authorization: Bearer <token>' -plaintext localhost:50051 task.TodoService/DeleteTask- The clean architecture pattern makes the codebase easy to understand and maintain.
- Using Docker ensures that the application can be easily run locally and in different environments.
- gRPC provides a high-performance and scalable way to handle communication between services.
- Bổ sung, tổ chức database
- Xác thực người dùng có quyền thực hiện với data của họ Cấu trúc transportation/grpc thành các file theo chức năng Example: transportation |---grpcuser |---create.go |---update.go ...
- Định nghĩa lại response của gRPC: message CommonResponse { int32 code = 1; // Mã trạng thái (0: thành công, >0: lỗi) string message = 2; // Thông báo lỗi hoặc thành công google.protobuf.Any data = 3; // Dữ liệu kết quả (nếu thành công) }