It is a RESTful API using Go, gorilla/mux, Basic Authentication, JWT Authentication.
I have built a fully-fledged REST API with Go that exposes GET, POST, DELETE and PUT endpoints which allows to perform the full range of CRUD operations. A handler function accepts http response and request in json format. Then, the request is decoded and written to response according to the called function. This handler function is wrapped by the authentication middleware to perform the security check.
URL | Function | Method | Description | Authentication Type |
---|---|---|---|---|
https://localhost:8000/api/# | Login | POST | Return JWT token in response for successful authentication | Basic |
https://localhost:8000/api/getBooks | getBooks | GET | Returns the details of all the books | JWT |
https://localhost:8000/api/getBook/{id} | getBook | GET | Returns the details of the book with the valid requested book id | JWT |
https://localhost:8000/api/createBook | createBook | POST | Creates a new book | JWT |
https://localhost:8000/api/updateBooks/{id} | updateBooks | PUT | Updates the details of the requested book id | JWT |
https://localhost:8000/api/deleteBooks/{id} | deleteBooks | DELETE | Deletes the book specified by id | JWT |
- Basic Authentication
- JWT Authentication
type Book struct {
ID string `json:"id"`
Isbn string `json:"isbn"`
Title string `json:"title"`
Author *Author `json:"author"`
}
type Author struct {
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
}
- go install github.com/Ahasannn/book-server-api@b687963
Set Environment variables for Basic Authentication.
export username=Ahasan
export password=ak4747
Testing the API endpoints
- Primary api endpoints can be tested with Postman
Server Run
go build -o bin/book-server-api .
./bin/book-server