Skip to content

Book-API is a Go-based project that provides an API for managing books, with endpoints for creating, reading, updating, and deleting book records. It includes a Dockerfile for easy containerized deployment, Suitable for developers who need to learn RESTful API development in Go. This might be your next intern task answer πŸ€ͺ

License

Notifications You must be signed in to change notification settings

foverokavindz/Book-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Book API

A simple RESTful API for managing a collection of books, built with Go.

Features

  • CRUD operations for books (Create, Read, Update, Delete)
  • JSON data format
  • In-memory data store
  • RESTful API design
  • Docker containerization
  • Kubernetes deployment support

Installation

Prerequisites

  • Go 1.16 or higher

Setup

Run the application:

go run main.go

The server will start on port 8080 by default.

Development with Hot-Reload

You can use Fresh, a tool that automatically rebuilds and restarts your Go application when files change.

Installing Fresh

go install github.com/pilu/fresh@latest

Running the Application with Fresh

Navigate to your project directory and run:

fresh

Running Tests

The project includes unit tests for all API endpoints.

Installing Test Dependencies

go get github.com/stretchr/testify/assert
go get github.com/gorilla/mux

Running All Tests

To run all tests in the project:

go test ./...

Running Specific Test Files

To run tests in a specific file:

go test ./tests/book_test.go

Test Structure

The tests are organized in the tests directory:

  • book_test.go: Tests for CRUD operations on books
  • search_test.go: Tests for book search functionality

Docker Containerization

Prerequisites

  • Docker installed on your machine

Steps to Containerize

Build the Docker image:

docker build -t book-api:latest .

Run the Docker container:

docker run -p 8080:8080 book-api:latest
  1. Access the API at http://localhost:8080/books

Kubernetes Deployment

Environment

  • Windows OS
  • Minikube running with Docker driver

Prerequisites

  • Docker
  • Minikube
  • kubectl

Setup Instructions

  1. Install Minikube and start it:
minikube start
  1. Configure Docker to use Minikube's Docker daemon:
minikube docker-env | Invoke-Expression
  1. Build the Docker image:
docker build -t book-api:latest .
  1. Apply Kubernetes configurations:
kubectl apply -f kubernetes/deployment.yaml
kubectl apply -f kubernetes/service.yaml
  1. Access the API:
minikube service book-api-service
  1. Use the tunnel URL (e.g., http://127.0.0.1:10137) to access the API endpoints

Kubernetes Objects Used

  • Deployment: Manages 2 replicas of the book API application
  • Service: NodePort service that exposes the application

API Endpoints

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

Data Format

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
}

Example Book Creation Payload

{
  "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
}

Usage Examples

Get all books

curl -X GET http://localhost:8080/books

Get a specific book

curl -X GET http://localhost:8080/books/1

Create a new book

curl -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
  }'

Update a book

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
  }'

Delete a book

curl -X DELETE http://localhost:8080/books/k7j6i5h-4g3f-2e1d-0c9b-8a7z6y5x4w3v

Project Structure

book-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

Screenshots

API Testing in Postman

API Response in Postman

Docker Build Process

Docker Build

Docker Container Running

Docker Container

Kubernetes Deployment

Kubernetes Deployment

Minikube Service

Minikube Service

About

Book-API is a Go-based project that provides an API for managing books, with endpoints for creating, reading, updating, and deleting book records. It includes a Dockerfile for easy containerized deployment, Suitable for developers who need to learn RESTful API development in Go. This might be your next intern task answer πŸ€ͺ

Topics

Resources

License

Stars

Watchers

Forks