A RESTful API built with Go and MySQL for managing a bookstore's inventory. This project demonstrates clean architecture, CRUD operations, and modern backend development practices.
- Complete CRUD Operations - Create, Read, Update, Delete books
- RESTful Architecture - Clean and intuitive API endpoints
- MySQL Integration - Persistent data storage with GORM ORM
- JSON API - Structured request/response handling
- Modular Design - Well-organized package structure
- Auto-Migration - Database schema management
Watch a screen recording demonstration of the API in action:
2025-06-13.10-23-58.mp4
- Language: Go (Golang) 1.16
- Database: MySQL
- ORM: GORM
- HTTP Router: Gorilla Mux
- Data Format: JSON
Before running this project, make sure you have:
- Go 1.16 or higher installed
- MySQL server running
- Git (for cloning the repository)
-
Clone the repository
git clone https://github.com/abhinav-phi/bookstore-api.git cd bookstore-api
-
Install dependencies
go mod tidy
-
Configure MySQL Database
- Create a MySQL database named
bookstore
- Update the database connection string in
packages/config/app.go
:
d, err := gorm.Open("mysql", "username:password@tcp(127.0.0.1:3306)/bookstore?charset=utf8&parseTime=True&loc=Local")
- Create a MySQL database named
-
Run the application
go run main.go
The server will start on http://localhost:9000
Method | Endpoint | Description |
---|---|---|
GET | /book/ |
Get all books |
GET | /book/{id} |
Get book by ID |
POST | /book/ |
Create a new book |
PUT | /book/{id} |
Update an existing book |
DELETE | /book/{id} |
Delete a book |
curl -X POST http://localhost:9000/book/ \
-H "Content-Type: application/json" \
-d '{
"name": "The Go Programming Language",
"author": "Alan Donovan",
"publication": "Addison-Wesley"
}'
curl -X GET http://localhost:9000/book/
curl -X GET http://localhost:9000/book/1
curl -X PUT http://localhost:9000/book/1 \
-H "Content-Type: application/json" \
-d '{
"name": "Updated Book Title",
"author": "Updated Author"
}'
curl -X DELETE http://localhost:9000/book/1
bookstore-api/
โโโ main.go # Application entry point
โโโ go.mod # Go module dependencies
โโโ go.sum # Dependency checksums
โโโ packages/
โโโ config/
โ โโโ app.go # Database configuration
โโโ controllers/
โ โโโ book-controller.go # HTTP handlers
โโโ models/
โ โโโ book.go # Database models
โโโ routes/
โ โโโ bookstore-routes.go # Route definitions
โโโ utils/
โโโ utils.go # Utility functions
The Book model includes:
type Book struct {
gorm.Model
Name string `json:"name"`
Author string `json:"author"`
Publication string `json:"publication"`
}
- ID: Auto-generated primary key
- Name: Book title
- Author: Book author
- Publication: Publisher name
- CreatedAt: Timestamp of creation
- UpdatedAt: Timestamp of last update
- DeletedAt: Soft delete timestamp
- Separation of Concerns: Each package has a specific responsibility
- Dependency Injection: Database connection managed centrally
- Error Handling: Proper error management throughout the application
- GORM ORM: Simplified database operations
- Auto-Migration: Automatic schema updates
- Connection Pooling: Efficient database connections
- Gorilla Mux: Powerful HTTP router
- JSON Serialization: Automatic request/response handling
- RESTful Design: Standard HTTP methods and status codes
You can test the API using:
- Postman: Import the endpoints and test manually
- cURL: Use the command-line examples above
- Go Tests: Write unit tests for individual components
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Authentication and authorization
- Pagination for book listings
- Search and filtering capabilities
- Unit and integration tests
- Docker containerization
- API documentation with Swagger
- Rate limiting
- Logging middleware
GitHub : abhinav-phi
Project Link : click here
โญ If you found this project helpful, please give it a star!