This is a basic RESTful API server, build with Golang. In this API server I have implemented Cobra CLI for running the API from the CLI and also used go-chi instead of Go net/http.
git clone https://github.com/AshrafulHaqueToni/httpAPIserver.git
cd httpAPIserver
go install ./httpAPIserver
- start the API in default port : 8081 by
httpAPIserver start
- start the API in your given port by
httpAPIserver start -p=8088
, give your port number in the place of 8088
docker build -t <image_name> .
- or
docker build -t <docker_hub_username>/<image_name>:<tag> .
(if your do this then don't need to give tag before dockerhub push)
-
docker run -p 8088:8080 <image_name>
(valid when usedCMD ["start", "-p", "8080"]
in Dockerfile) -
docker run -p 8081:8088 <image_name> start -p "8088"
(valid for current version of Dockerfile, when did not used CMD in Dockerfile)
upload the image to Docker Hub
docker login --username=<docker_hub_username>
docker tag <id_of_the_created_image> <docker_hub_username>/<name_of_the_image>:<tag>
docker push <docker_hub_username>/<name_of_the_image>:<tag>
docker run -v <absolute_host_path/.env>:<container_path/.env> -p 8088:8089 <image_name> start -p 8089
Endpoint | Function | Method | StatusCode | Authentication |
---|---|---|---|---|
/login |
logIn | GET | StatusOK, StatusUnauthorized | Basic |
/products |
ShowAllProducts | GET | StatusOK, StatusUnauthorized | JWT |
/brands |
ShowAllBrands | GET | StatusOK, StatusUnauthorized | JWT |
/products/{id} |
GetProducts | GET | StatusOK, StatusNoContent, StatusUnauthorized | JWT |
/brands/{id} |
GetBrands | GET | StatusOK, StatusNoContent, StatusUnauthorized | JWT |
/products/{id} |
DeleteProduct | DELETE | StatusOK, StatusNoContent, StatusUnauthorized | JWT |
/products/addproduct |
AddProducts | POST | StatusOK, StatusUnauthorized | JWT |
/products/update/{id} |
UpdateProducts | POST | StatusOK, StatusNoContent,StatusUnauthorized | JWT |
/brands/delete/{id} |
DeleteBrand | DELETE | StatusOK, StatusUnauthorized | JWT |
/brands/addbrand |
AddBrands | POST | StatusOK, StatusUnauthorized | JWT |
/logout |
Logout | POST | StatusOK, StatusUnauthorized | JWT |
- Brand Model
type Brands struct {
BrandId int `json:"brand_id"`
BrandName string `json:"brand_name"`
BrandProduct []int `json:"brand_product"`
}
- Product Model
type Products struct {
ProductId int `json:"product_id"`
ProductName string `json:"product_name"`
ProductBrand Brands `json:"product_brand"`
ProductPrice int `json:"product_price"`
ProductStatus bool `json:"product_status"`
}
- Credentials Model
type Credentials struct {
Username string `json:"username"`
Password string `json:"password"`
}
- implemented JWT authentication
- first of all user need to hit
/login
endpoint with basic authentication then a token will be given and with that token for specific time user can do other request
curl -X POST -H "Content-Type:application/json" -d '{"username":"Ashraful","password":"12345"}' http://localhost:8081/login
curl -X GET -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/products
curl -X GET -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/products/1
curl -X GET -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/brands
curl -X GET -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/brands/1
curl -X POST -H "Content-Type:application/json" -d '{"product_id":5,"product_name":"keyboard","product_brand":{"brand_id":3,"brand_name":"A4tech","brand_product":[]}}' -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/products/addproduct
curl -X POST -H "Content-Type:application/json" -d '{"brand_id":4,"brand_name":"Dell","brand_product":[]}' -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/brands/addbrand
curl -X POST -H "Content-Type:application/json" -d '{"product_id":4,"product_name":"mobile","product_brand":{"brand_id":2,"brand_name":"apple","brand_product":[3,4]}}' -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/products/update/4
curl -X DELETE -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/products/delete/1`
curl -X DELETE -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" http://localhost:8081/brands/delete/1`
- Primarily tested the API endpoints by Postman
- E2E Testing.
- Checked response status code with our expected status code