Skip to content

Imtiaz246/Book-Server

Repository files navigation

Rest Book API Server in golang

  • The Book API Server provides endpoints to create, show, read, update & delete users and books.

Run the server on local machine

$ git clone [email protected]:Imtiaz246/Book-Server.git

$ cd Book-Server

$ go mod download

& echo -e "ADMIN_USERNAME=\nADMIN_PASSWORD=\nJWT_SECRET_KEY=\n" > .env

Setup environment variables in .env file

ADMIN_USERNAME=
ADMIN_PASSWORD=
JWT_SECRET_KEY=

$ go build -o main

$ ./main --port=3000

To Test the API endpoints

$ go test ./... [test the API endpoints]

To check the version

./main version

Run the server in Docker

$ docker volume create bs-backup [creates a volume for backup]

$ docker build -t book-server-img .

$ docker run -p 3000:3000 -v bs-backup:/root/BackupFiles --name bookserver book-server-img

Get the Docker image

$ docker pull imtiazcho/book-server:latest

Available API Endpoints

Method API Endpoint Authentication Type Payload Description
POST /api/v1/ping Checks the server health
POST /api/v1/users/token payload Returns jwt token in response
POST /api/v1/users payload Creates a user if the user information is valid
GET /api/v1/users Returns the list of all user
GET /api/v1/users/{username}/books Returns the book list of a specific user
GET /api/v1/users/{username} Returns the user specified by {username}
DELETE /api/v1/users/{username} JWT payload Deletes the user specified by {username}
PUT /api/v1/users/{username} JWT payload Updates the user information specified by {username}
POST /api/v1/books JWT payload Creates a books if the book information is valid
GET /api/v1/books Returns all the book list
GET /api/v1/books/{id} Returns the book specified by the {id}
DELETE /api/v1/books/{id} JWT Deletes the book specified by the {id}
PUT /api/v1/books/{id} JWT payload Updates the book information specified by {id}

Basic curl commands

  1. Create a user
curl -v -H "Content-type: application/json" -X POST -d '{"username":"lakkas", "password":"1234", "organization":"Appscode Ltd", "email": "[email protected]"}' https://localhost:3000/api/v1/users
  1. Get all user list
curl -v -H "Content-type: application/json" https://localhost:3000/api/v1/users
  1. Get book list of a user
 curl -v -H "Content-type: application/json" https://localhost:3000/api/v1/users/{usrname}/books
  1. Get a user by username
curl -v -H "Content-type: application/json" https://localhost:3000/api/v1/users/{username}
  1. Delete a user by username
curl -v -H "Content-type: application/json" -H "Authorization: Bearer <jwt token>" -X DELETE https://localhost:3000/api/v1/users/{username}
  1. Update a user by username
curl -v -H "Content-type: application/json" -H "Authorization: Bearer <jwt token>" -d '{ "username" : "lakkas updated", "password" : "1234", "organization" : "Appscode Ltd", "email": "[email protected]" }' -X PUT https://localhost:3000/api/v1/users/{username}
  1. Create a book
curl -v -H "Content-type: application/json" -H "Authorization: Bearer <jwt token>" -X POST -d '{ "book-name": "new book", "price": 200, "isbn": "4323-6456-4756-4564", "authors": [ { "username": "admin" } ], "book-content": { "over-view": "overview", "chapters": [ { "chapter-title": "chapter 1", "chapter-content": "chapter 1 content" }, { "chapter-title": "chapter 2", "chapter-content": "chapter 2 content" } ] } }' https://localhost:3000/api/v1/books`
  1. Get all book list
curl -v -H "Content-type: application/json" https://localhost:3000/api/v1/books
  1. Get a book by book-id
curl -v -H "Content-type: application/json" https://localhost:3000/api/v1/books/{id}
  1. Delete a book by book-id
curl -v -H "Content-type: application/json" -H "Authorization: Bearer <jwt token>" -X DELETE https://localhost:3000/api/v1/books/{id}
  1. Update a book by book-id
curl -v -H "Content-type: application/json" -H "Authorization: Bearer <jwt token>" -X PUT -d '{ "book-name": "updated book curl", "price": 200, "isbn": "4323-6456-4756-4564", "authors": [ { "username": "admin" } ], "book-content": { "over-view": "overview", "chapters": [ { "chapter-title": "chapter 1", "chapter-content": "chapter 1 content" }, { "chapter-title": "chapter 2", "chapter-content": "chapter 2 content" } ] } }' https://localhost:3000/api/v1/books/{id}
  1. Get a jwt token
curl -v -H "Content-type: application/json" -d '{ "username": "admin", "password": "1234" }' https://localhost:3000/api/v1/users/get-token`
  1. Check the server health
curl -v -H "Content-type: application/json" https://localhost:3000/api/v1/ping

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages