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 /employees
GET /employees/${id}
POST /employees
Install and start Redis server for caching.
sudo apt install redis-server
sudo systemctl status redis-server
Clone the project.
git clone https://github.com/hmsayem/clean-architecture-implementation.git
Go to the project directory.
cd clean-architecture-implementation
Copy all third-party dependencies to vendor folder.
go mod vendor
Export environment variables.
GOOGLE_APPLICATION_CREDENTIALS=/path/to/project-private-key.json
SERVER_PORT=:8000
REDIS_SERVER_HOST=localhost:6379
Start the server.
go run .
docker run --name redis --net=host -d redis
Build 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-server
Create secret from project-private-key.json
kubectl create secret generic firestore-secret --from-file=/path/to/project-private-key.json
Create Configmaps
kubectl apply -f k8s/redis-server-cm.yaml
kubectl apply -f k8s/employee-server-cm.yaml
Create Pods
kubectl apply -f k8s/redis-server.yaml
kubectl apply -f k8s/employee-server.yaml
Create Services
kubectl apply -f k8s/redis-server-svc.yaml
kubectl apply -f k8s/employee-server-svc.yaml
Port 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": "kamol@gmail.com"
},
{
"id": 81,
"name": "Piyush Kanti Das",
"title": "Software Engineer",
"team": "A",
"email": "piyush@gmail.com"
}
]
❯ curl -X GET "http://localhost:8000/employees/81" | jq
{
"id": 81,
"name": "Piyush Kanti Das",
"title": "Software Engineer",
"team": "A",
"email": "piyush@gmail.com"
}
❯ 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": "hossain@gmail.com"}'
{"id":89,"name":"Hossain Mahmud","title":"Software Engineer","team":"A","email":"hossain@gmail.com"}
go test service/employee-service.go service/employee-service_test.go