The code in this repository is a microservice written in Go. The microservice exposes a REST API for managing a list of books.
The microservice provides the following REST API:
Returns a JSON array of all the books in the database.
URL : /api/books
Method : GET
Auth required : NO
Permissions required : None
Returns a JSON object for the book with the given ID.
URL : /api/books/:id
Method : GET
Auth required : NO
Permissions required : None
URL Parameters
Name | Type | Description |
---|---|---|
id |
string |
The ID of the book to retrieve |
Creates a new book in the database.
URL : /api/books
Method : POST
Auth required : NO
Permissions required : None
Data constraints
{
"isbn": "[valid ISBN]",
"title": "[book title]",
"author": {
"firstname": "[author first name]",
"lastname": "[author last name]"
}
}
Data example
{
"isbn": "1234567890",
"title": "My Book",
"author": {
"firstname": "John",
"lastname": "Doe"
}
}
Updates the book with the given ID.
URL : /api/books/:id
Method : PUT
Auth required : NO
Permissions required : None
URL Parameters
Name | Type | Description |
---|---|---|
id |
string |
The ID of the book to update |
Data constraints
{
"isbn": "[valid ISBN]",
"title": "[book title]",
"author": {
"firstname": "[author first name]",
"lastname": "[author last name]"
}
}
Data example
{
"isbn": "1234567890",
"title": "My Book",
"author": {
"firstname": "John",
"lastname": "Doe"
}
}
Deletes the book with the given ID.
URL : /api/books/:id
Method : DELETE
Auth required : NO
Permissions required : None
URL Parameters
Name | Type | Description |
---|---|---|
id |
string |
The ID of the book to delete |
You can run the code using the following command:
go run main.go
Use the following command to build and push the Docker Image:
docker buildx build --platform linux/amd64,linux/arm64 --push --tag hub.narasimman.xyz/go-microservice:latest .
Start the container using:
docker compose up -d
The code will start a server on http://localhost:8000
. You can then access the following endpoints:
http://localhost:8000/api/books
- Returns a list of bookshttp://localhost:8000/api/books/{id}
- Returns a single book with the givenid
http://localhost:8000/api/books
- Creates a new bookhttp://localhost:8000/api/books/{id}
- Updates a book with the givenid
http://localhost:8000/api/books/{id}
- Deletes a book with the givenid