A simple RESTful API for managing a collection of books, built with Go.
- CRUD operations for books (Create, Read, Update, Delete)
- JSON data format
- In-memory data store
- RESTful API design
- Docker containerization
- Kubernetes deployment support
- Go 1.16 or higher
Run the application:
go run main.go
The server will start on port 8080 by default.
You can use Fresh, a tool that automatically rebuilds and restarts your Go application when files change.
go install github.com/pilu/fresh@latestNavigate to your project directory and run:
freshThe project includes unit tests for all API endpoints.
go get github.com/stretchr/testify/assert
go get github.com/gorilla/muxTo run all tests in the project:
go test ./...To run tests in a specific file:
go test ./tests/book_test.goThe tests are organized in the tests directory:
book_test.go: Tests for CRUD operations on bookssearch_test.go: Tests for book search functionality
- Docker installed on your machine
Build the Docker image:
docker build -t book-api:latest .Run the Docker container:
docker run -p 8080:8080 book-api:latest- Access the API at http://localhost:8080/books
- Windows OS
- Minikube running with Docker driver
- Docker
- Minikube
- kubectl
- Install Minikube and start it:
minikube start- Configure Docker to use Minikube's Docker daemon:
minikube docker-env | Invoke-Expression- Build the Docker image:
docker build -t book-api:latest .- Apply Kubernetes configurations:
kubectl apply -f kubernetes/deployment.yaml
kubectl apply -f kubernetes/service.yaml- Access the API:
minikube service book-api-service- Use the tunnel URL (e.g., http://127.0.0.1:10137) to access the API endpoints
- Deployment: Manages 2 replicas of the book API application
- Service: NodePort service that exposes the application
| Method | Endpoint | Description |
|---|---|---|
| GET | /books | Get all books |
| GET | /books/{id} | Get a book by ID |
| POST | /books | Create a new book |
| PUT | /books/{id} | Update a book |
| DELETE | /books/{id} | Delete a book |
Books are represented as JSON objects with the following structure:
{
"bookId": "8k7j6i5h-4g3f-2e1d-0c9b-8a7z6y5x4w3v",
"authorId": "5t4s3r2q-1p0o-9n8m-7l6k-5j4i3h2g1f0e",
"publisherId": "2c3d4e5f-6g7h-8i9j-0k1l-2m3n4o5p6q7r",
"title": "Brave New World",
"publicationDate": "1932-10-27",
"isbn": "9780060850524",
"pages": 288,
"genre": "Dystopian",
"description": "A futuristic society that has replaced the pain and drama of human life with social stability at the cost of individuality.",
"price": 15.25,
"quantity": 19
}{
"bookId": "8k7j6i5h-4g3f-2e1d-0c9b-8a7z6y5x4w3v",
"authorId": "5t4s3r2q-1p0o-9n8m-7l6k-5j4i3h2g1f0e",
"publisherId": "2c3d4e5f-6g7h-8i9j-0k1l-2m3n4o5p6q7r",
"title": "Brave New World",
"publicationDate": "1932-10-27",
"isbn": "9780060850524",
"pages": 288,
"genre": "Dystopian",
"description": "A futuristic society that has replaced the pain and drama of human life with social stability at the cost of individuality.",
"price": 15.25,
"quantity": 19
}curl -X GET http://localhost:8080/bookscurl -X GET http://localhost:8080/books/1curl -X POST http://localhost:8080/books \
-H "Content-Type: application/json" \
-d ' {
"bookId": "8k7j6i5h-4g3f-2e1d-0c9b-8a7z6y5x4w3v",
"authorId": "5t4s3r2q-1p0o-9n8m-7l6k-5j4i3h2g1f0e",
"publisherId": "2c3d4e5f-6g7h-8i9j-0k1l-2m3n4o5p6q7r",
"title": "Brave New World",
"publicationDate": "1932-10-27",
"isbn": "9780060850524",
"pages": 288,
"genre": "Dystopian",
"description": "A futuristic society that has replaced the pain and drama of human life with social stability at the cost of individuality.",
"price": 15.25,
"quantity": 19
}'curl -X PUT http://localhost:8080/books/4 \
-H "Content-Type: application/json" \
-d ' {
"bookId": "8k7j6i5h-4g3f-2e1d-0c9b-8a7z6y5x4w3v",
"authorId": "5t4s3r2q-1p0o-9n8m-7l6k-5j4i3h2g1f0e",
"publisherId": "2c3d4e5f-6g7h-8i9j-0k1l-2m3n4o5p6q7r",
"title": "Brave New World - Updated",
"publicationDate": "1932-10-27",
"isbn": "9780060850524",
"pages": 288,
"genre": "Dystopian",
"description": "A futuristic society that has replaced the pain and drama of human life with social stability at the cost of individuality.",
"price": 15.25,
"quantity": 19
}'curl -X DELETE http://localhost:8080/books/k7j6i5h-4g3f-2e1d-0c9b-8a7z6y5x4w3vbook-api/
βββ main.go # Entry point with router setup
βββ controllers/ # Request handlers for each endpoint
β βββ books.go # Book controller functions
β βββ search.go # Book search function
βββ models/ # Data models
β βββ book.go # Book struct definition
βββ Dockerfile # Docker configuration
βββ docker-compose.yml # Docker Compose configuration
βββ kubernetes/ # Kubernetes manifests
β βββ deployment.yaml # Deployment configuration
β βββ service.yaml # Service configuration
βββ README.md # Documentation




