Skip to content

renaldid/book-management

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gRPC Book Management API

Overview

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.

Features

  • 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.

Technologies Used

  • 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.

Getting Started

Prerequisites

Ensure the following tools are installed:

Setup MySQL

You can set up the MySQL database using Docker by running the following commands:

  1. Start MySQL container:
    make setup-db
    
    This command will:
    • Set up MySQL in a Docker container.
    • Create a database named books.
  2. Run database migration:
    make migrate-db
    
    This command will:
    • Apply the initialize.sql migration file to create the books table with columns (id, title, author).
  3. Clear the database (reset and recreate):
    make clear-db
    
    This command will:
    • Drop and recreate the books database to reset your data.

Run the gRPC Server

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.

Using the API

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:

Create Book

  • Request:
message CreateBookRequest {
  string title = 1;
  string author = 2;
}
  • Response:
message CreateBookResponse {
  string id = 1;
  string title = 2;
  string author = 3;
}
  • Example Use grpcurl to call this API:
grpcurl -plaintext -d '{"title":"The Go Programming Language","author":"Alan A. A. Donovan"}' localhost:50051 book.BookService/CreateBook

Get Book

  • Request:
message GetBookRequest {
  string id = 1;
}
  • Response:
message GetBookResponse {
  string id = 1;
  string title = 2;
  string author = 3;
}
  • Example Use grpcurl to call this API:
grpcurl -plaintext -d '{"id":"1234"}' localhost:50051 book.BookService/GetBook

List Books

  • Request:
message ListBooksRequest {}
  • Response:
message ListBooksResponse {
  repeated Book books = 1;
}
  • Example Use grpcurl to call this API:
grpcurl -plaintext -d '{}' localhost:50051 book.BookService/ListBooks

File Structure

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

Makefile Commands

  • 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.

Contributing

Feel free to fork this project, make improvements, and create pull requests. Contributions are welcome!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors