Prerequisites:
git
go 1.16.3
docker
docker hub account
Commands required for testing, building the project and for serving swagger client as added to the Makefile
.
Commands | Tasks |
---|---|
make test |
To execute all the test in the project. |
make run |
To run the server. The server will be listening to port 8000 . |
swagger-gen |
To build swagger spec yaml and serve it. |
Alternatively, the application can also be run using docker
.
Use the following command to pull the docker image.
docker image pull ash822/goweb
Run the application using the following command.
docker run -p 8000:8000 ash822/goweb:latest
This repository showcases a simple HTTP server serving REST endpoints, built using the Golang net/http
package and gorilla/mux
as http router.
It supports the following endpoints.
The POST endpoints take a json with the text
that needs to be checked as palindrome.
Request body:
{
"Text": "kayak"
}
When the resource gets created, the service performs check whether the given string is a palindrome and adds a boolean flag indicating it. Also, a random UUID is generated as id
of the document in order to fetch it.
Success Response:
{
"id": "df6711c5-d061-4e7b-b27f-548fac86fa4f",
"text": "kayak",
"palindrome": true
}
The id
can be used to retrieve the message back.
For more information on API documentation refer the API documentation subsection.
A simple middleware is added to authenticate the requests based on API keys. Currently, the API key is hardcoded to topgun
.
Add the API key header X-API-Key
to authenticate the requests.
- The palindrome check is case-insensitive. Both
Kayak
andkaYAK
will be a valid palindrome. - The message resources are stored in-memory in a map data structure, there is no persistence connection to store the data. Hence, if the service is restarted, all the data gets lost.
- To simplify the development, the get all messages endpoint
/messages
does not support filter, sorting and pagination. - The deployment has been configured to push the docker images to my personal docker hub account. However, I made the repository public and anyone can pull images from the repository if they have a valid docker hub account.
The data flow is designed to better manage the dependencies as follows.
The router links the path to the handler functions defined in the controller. The service controls the business logic and persist the data using the repository.
Gomega
is used as the matcher library for assertions. In order to test the service, GoMock
is used for mocking the repository.
The repository interface is decorated with mockgen
annotations. The mock repository code can be generated using make mock-gen
command.
The REST endpoints are tested using net/http/httptest
package.
To execute the tests, run make test
command.
The project is setup with Continuous Integration and Continuous Deployment (to Docker hub) using Github Actions.
On every pull request to master
branch and on every push to master
branch, the workflow gets executed. The workflow checks out the latest, build and run tests.
As the last step, the application builds and tags a docker image, and pushes the image to Docker hub registry.
Visit Actions tab to check the jobs.
To generate the swagger file and to serve the swagger client, run make swagger-run
command.
Refer: swagger.yaml