fruits
Service to handle fruits information.
How to build?
from project folder executes commands below, it will generate binary files in the ./bin/
folder
- just build with current operating system
make build
- build for a linux distro operating system
make build-linux
How to run a test environment quickly?
- make sure you have
docker-compose
installed. - run the docker compose.
docker-compose up -d
or run this shortcut
make run-local
or
make test
- once you finished to use the environment, follow these steps
make clean-local
How to test?
from project folder run the following command
go test -race ./...
How to call the fruit API
You can use insomnia api client and use the project insomnia-fruits-service.json
.
Coding Decisions
- The service was built following the hexagonal architecture pattern in order to improve maintainability and extensibility. Most of the logic of the service is related to external resources like loggers, databases and monitoring platforms.
- Loose coupling between packages is very important to increase cohesion, so I avoided referencing another package directly.
- All service methods must receive the context parameter. The idea is to propagate the cancellation of context and other values in the future. e.g. correlation id.
- An in-memory database was built to persist fruits information, this was done thinking that this was an mvp and we don't want to use a well-known database engine yet.
- The adapters folder has all the packages that provide logic to communicate with external components.
- To keep a loose coupling between the
service
package and thememorydb
package, I created therepository
package which provides the logic that both packages need to communicate with each other. -
monitoring
package provides logic to monitor and handle metrics for the fruits service and themetrics
package simulates the integration with an instrumenting platorm where agents will send the metrics. - A worker was created to define the
monitoring
logic. The idea is to process metrics asynchronously to avoid adding latency to the core capabilities of the service. - I think that the web logic of the service is part of the adapters in the hexagonal architecture pattern, that's why the package
web
is within theadapter
folder. - I used the
go-kit
library to build the microservice in order to make things easy to understand rather than easy to do. - In my opinion the main function needs to be very clean, that's why it just calls the application logic to run the service.
- The
application
package is in charge of start the application, instantiate components and make dependency injections. - The
configurations
package provides the logic to load the application setup. - The fruit dataset will be loaded when the application starts, in case only one record is invalid, its status will be invalid.
- Because the status of the dataset is part of the logic of the fruits, the fruit package will take care of indicating whether the dataset is valid or not.
- Assumption: The mandatory fields for fruit are: name, classification, country and vault.
- docker-compose was used to build and run the application, so far this is only one service. The service can be run in a standalone fashion though.
- Please notice that the service has a lot of logs, the idea behind this is to facilitate debugging at production or qa environments.
Improvements for a live production system.
- We should provide an endpoint to verify that the service is running or not. i.e
/health
or/heartbeat
. - Just in case security is an important issue for this service, we should provide a security mechanism to allow only granted users to make changes in the fruits service data. e.g.
JWT
- We should change the current in-memory database for a well known engine. e.g postgresql or mongodb. Data must survive a service interruption.
- The monitoring platform should be changed by a well known engine such as Prometheus.
- It's important to create a pipeline to run unit and integration tests, build and deploy the application in different environments.
- Generate documentation for the fruit service API in order to facilitate its use. For this we can use Swagger.
deploy in kubernetes
- only use this with minikube eval $(minikube docker-env) docker build -t local/fruits:latest .
https://www.baeldung.com/ops/kubernetes-helm
helm install --name fruits ./k8s-v2/fruits
using flux
check project flux-practices