This project implements a gRPC API for managing books using Go and MySQL. The API supports operations like creating a book, retrieving a book by its ID, and listing all books. The database operations are done using database/sql (without using an ORM like GORM) to interact with MySQL.
- gRPC server to handle book creation, retrieval, and listing.
- MySQL database for storing book records.
- Database setup and migration with Docker and SQL migration files.
- Clean Architecture pattern to separate concerns (Domain, Usecase, Repository, Delivery).
- Replay Option: After the game ends, players can choose to play again.
- Go: Programming language.
- gRPC: Communication protocol for handling requests and responses.
- MySQL: Database for storing book information.
- Docker: To set up MySQL as a container.
- Makefile: To automate setup, migration, and running the application.
Ensure the following tools are installed:
- Go (version 1.23 or higher): https://go.dev/dl/
- Docker: https://www.docker.com/
- Make: Make sure
makeis installed (usually comes with UNIX-based systems).
You can set up the MySQL database using Docker by running the following commands:
- Start MySQL container:
This command will:
make setup-db- Set up MySQL in a Docker container.
- Create a database named
books.
- Run database migration:
This command will:
make migrate-db- Apply the
initialize.sqlmigration file to create thebookstable with columns (id,title,author).
- Apply the
- Clear the database (reset and recreate):
This command will:
make clear-db- Drop and recreate the
booksdatabase to reset your data.
- Drop and recreate the
Once the database is set up and migrated, you can run the gRPC server with the following command:
make run
This will:
- Start the gRPC server on port
50051. - The server is ready to accept requests.
Once the server is running, you can use tools like grpcurl or Postman to interact with the gRPC API. Below are the supported gRPC methods:
- Request:
message CreateBookRequest {
string title = 1;
string author = 2;
}
- Response:
message CreateBookResponse {
string id = 1;
string title = 2;
string author = 3;
}
- Example
Use
grpcurlto call this API:
grpcurl -plaintext -d '{"title":"The Go Programming Language","author":"Alan A. A. Donovan"}' localhost:50051 book.BookService/CreateBook
- Request:
message GetBookRequest {
string id = 1;
}
- Response:
message GetBookResponse {
string id = 1;
string title = 2;
string author = 3;
}
- Example
Use
grpcurlto call this API:
grpcurl -plaintext -d '{"id":"1234"}' localhost:50051 book.BookService/GetBook
- Request:
message ListBooksRequest {}
- Response:
message ListBooksResponse {
repeated Book books = 1;
}
- Example
Use
grpcurlto call this API:
grpcurl -plaintext -d '{}' localhost:50051 book.BookService/ListBooks
The project follows Clean Architecture, with clear separation between layers:
grpc-books-clean/
├── cmd/
│ └── server/
│ └── main.go # Main entry point for the gRPC server
├── domain/
│ └── book.go # Domain model and repository interface
├── usecase/
│ └── book_usecase.go # Use case logic for book operations
├── repository/
│ └── mysql/
│ └── book_repo.go # MySQL repository implementation
├── pkg/
│ └── mysql/
│ └── mysql.go # MySQL connection and setup
├── migrations/
│ └── initialize.sql # SQL migration for creating the database and table
├── go.mod
├── go.sum
├── Makefile # Makefile for automating setup, migration, and running the app
└── README.md # This file
make setup-db: Set up MySQL database using Docker.make migrate-db: Run the SQL migration to create tables and columns.make clear-db: Clear and recreate the database (useful for resetting).make run: Run the gRPC server.
Feel free to fork this project, make improvements, and create pull requests. Contributions are welcome!