This is a RESTful API server implemented in Go that provides employee information of an organization. It follows the clean architecture principles which makes it testable and allows for easy integration with other frameworks, caches or databases without modifying the existing implementation.
This project utilizes mux and chi as the router, and Firestore as the database to store and retrieve employee information. Redis is also used as the cache to provide fast and efficient data retrieval.
The server supports the following CRUD operations on employee information:
- Create new employee
- Retrieve employee information by ID
- Retrieve all employees
- Update employee information
- Delete employee information
GET /employeesGET /employees/${id}POST /employeesInstall and start Redis server for caching.
sudo apt install redis-server
sudo systemctl status redis-serverClone the project.
git clone https://github.com/hmsayem/clean-architecture-implementation.gitGo to the project directory.
cd clean-architecture-implementationCopy all third-party dependencies to vendor folder.
go mod vendorExport environment variables.
GOOGLE_APPLICATION_CREDENTIALS=/path/to/project-private-key.json
SERVER_PORT=:8000
REDIS_SERVER_HOST=localhost:6379Start the server.
go run .docker run --name redis --net=host -d redisBuild image.
docker build -t employee-server .Run container.
docker run --mount type=bind,source=/path/to/project-private-key.json,target=/run/secrets/project-private-key.json,readonly --env GOOGLE_APPLICATION_CREDENTIALS='/run/secrets/project-private-key.json' --env SERVER_PORT=':8000' --env REDIS_SERVER_HOST='localhost:6379' --net host employee-serverCreate secret from project-private-key.json
kubectl create secret generic firestore-secret --from-file=/path/to/project-private-key.jsonCreate Configmaps
kubectl apply -f k8s/redis-server-cm.yaml
kubectl apply -f k8s/employee-server-cm.yamlCreate Pods
kubectl apply -f k8s/redis-server.yaml
kubectl apply -f k8s/employee-server.yamlCreate Services
kubectl apply -f k8s/redis-server-svc.yaml
kubectl apply -f k8s/employee-server-svc.yamlPort Forward
kubectl port-forward svc/employee 8000❯ curl -X GET "http://localhost:8000/employees" | jq
[
{
"id": 50,
"name": "Kamol Hasan",
"title": "Senior Software Engineer",
"team": "B",
"email": "[email protected]"
},
{
"id": 81,
"name": "Piyush Kanti Das",
"title": "Software Engineer",
"team": "A",
"email": "[email protected]"
}
]❯ curl -X GET "http://localhost:8000/employees/81" | jq
{
"id": 81,
"name": "Piyush Kanti Das",
"title": "Software Engineer",
"team": "A",
"email": "[email protected]"
} ❯ curl -X PUT "http://localhost:8000/employees/81" -d '{"title": "Senior Software Engineer"}' ❯ curl -X POST "http://localhost:8000/employees" -d '{"name": "Hossain Mahmud","title": "Software Engineer","team": "A","email": "[email protected]"}'
{"id":89,"name":"Hossain Mahmud","title":"Software Engineer","team":"A","email":"[email protected]"}
go test service/employee-service.go service/employee-service_test.go