Golang-fizzbuzz
Golang Fizzbuzz. This README is made up of two parts: the first one will give you basic instructions to easily install this application on your local device and be able to take part in the project. The other part is focused on our exercise.
Project README
Dependencies
Docker 20.10
Docker-compose 2.1
(Golang 1.17)
Project architecture
The project is built on top of this structure:
- Application: Business actions layer
- Command: DTO struct to verify the command required data
- Handler: Service which handle command
- Core: Application entry points
- Driver: Connector interfaces
- Infrastructure: External services layer
- Presentation: External system interaction layer, here API entry points
First installation
Every needed environment variables are located in the .env file. The file is commited for the exercise so you can easily run the project, but I usually ignore it and provide a .env.example or .env.local commited file.
You can install project with:
$ make install
Access to your local service with the following ports:
- HTTP API: 3000
Technical informations
You can update container for dev with:
$ make update
You can run test with:
$ make test || make test-cover
You can display app logs with:
$ make logs
You can have a bash command line interface with:
$ make cli
To get list of all command on your terminal:
$ make || make help
Simple API DOC
GET /fizzbuzz
5 query parameters accepted: int1 (int), int2 (int), limit (int), str1 (string), str2 (string)
Curl request example with query parameters:
$ curl --location --request GET 'localhost:3000/fizzbuzz?int1=3&int2=5&limit=100&str1=fizz&str2=buzz'
GET /statistic
No query parameters accepted
Curl request example:
$ curl --location --request GET 'localhost:3000/statistic'
Exam
The problematic of the exercise is technically focused on making a Golang API which provides JSON endpoints and can store data. I made for this project a simple architecture which allows you to easily maintain and expand it.
I chose to use net/http over gin-gonic/gin (or other) to run a vanilla http server and set up my own architecture (but I often use gin notably in a professional environment, which provides useful tools to bind JSON content for example).
I opted for redis to store our needed data which is a good and simple database caching (free / open source / fast and high performance) for our case. We can easily switch to another database storage by creating a new store infrastructure file (for PostgresSQL, MongoDB, Algolia, whatever we want) and updating the implementation on the concerned presentation.
I provided an alpine-golang Docker image with required test tools for development and a minimal Docker image that we can use for production. For development mode, notice that the make update
command can be triggered at any moment or automatically by nodemon, air, hotswap or others.
To facilitate the environment variables management for the exercise I used the joho/godotenv dependency.