RESTful HTTP API server using Go, Cobra CLI, gorilla mux
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 gorilla mux instead of Go net/http.
git clone https://github.com/shahincsejnu/httpAPIserver.git
cd httpAPIserver
go install httpAPIserver
- start the API in default port : 8080 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 |
---|---|---|---|---|
/api/login |
logIn | GET | StatusOK, StatusUnauthorized | Basic |
/api/articles |
getAllArticles | GET | StatusOK, StatusUnauthorized | JWT |
/api/article |
addNewArticle | POST | StatusCreated, StatusUnauthorized | JWT |
/api/article/{id} |
deleteArticle | DELETE | StatusOK, StatusNoContent, StatusUnauthorized | JWT |
/api/article/{id} |
updateArticle | PUT | StatusCreated, StatusNoContent, StatusUnauthorized | JWT |
/api/article/{id} |
getSingleArticle | GET | StatusOK, StatusNoContent, StatusUnauthorized | JWT |
- Article Model
type Article struct {
ID string `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
Author Author `json:"author"`
}
- Author Model
type Author struct {
ID string `json:"id"`
Name string `json:"name"`
Rating float64 `json:"rating"`
}
- implemented basic authentication middleware
- give username :
admin
and password :admin
for each query to the api endpoint otherwise access will be denied
- implemented JWT authentication
- first of all user need to hit
/api/login
endpoint with basic authentication then a token will be given and with that token for specific time user can do other request
httpAPIserver start --port=8080
curl -X GET http://localhost:8080/api/articles
curl -X GET http://localhost:8080/api/article/1
curl -X POST -H "Content-Type:application/json" -d '{"id":"10","title":"oka","body":"none","author":{"id":"11","name":"nobody","rating":10}}' http://localhost:8080/api/article
curl -X PUT -H "Content-Type:application/json" -d '{"id":"10","title":"update","body":"me","author":{"id":"11","name":"somebody","rating":9}}' http://localhost:8080/api/article/10
curl -X DELETE -H http://localhost:8080/api/article/1
curl commands with Basic Authentication (it is valid for version : v1.0.1 and login function of version : v1.0.2)
curl -X GET --user admin:admin http://localhost:8080/api/articles
curl -X GET --user admin:admin http://localhost:8080/api/article/1
curl -X POST --user admin:admin -H "Conten-Type:application/json" -d '{"id":"10","title":"update","body":"me","author":{"id":"11","name":"somebody","rating":9}}' http://localhost:8080/api/article
curl -X PUT --user admin:admin -H "Conten-Type:application/json" -d '{"id":"10","title":"update","body":"me","author":{"id":"11","name":"somebody","rating":9}}' http://localhost:8080/api/article/1
curl -X DELETE --user admin:admin http://localhost:8080/api/article/1
curl -X GET --user admin:admin http://localhost:8080/api/login
curl -X GET -H "Token: <jwt_token>" http://localhost:8080/api/articles
curl -X GET -H "Token: <jwt_token>" http://localhost:8080/api/article/1
curl -X POST -H "Token: <jwt_token>" -H "Content-Type:application/json" -d '{"id":"10","title":"update","body":"me","author":{"id":"11","name":"somebody","rating":9}}' http://localhost:8080/api/article
curl -X POST -H "Token: <jwt_token>" -H "Content-Type:application/json" -d '{"id":"10","title":"update","body":"me","author":{"id":"11","name":"somebody","rating":9}}' http://localhost:8080/api/article/1
curl -X DELETE -H "Token: <jwt_token>" http://localhost:8080/api/article/1
- Primarily tested the API endpoints by Postman
- E2E Testing.
- added unit testing for this API
- Checked response status code with our expected status code