Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hmsayem/personnel-api
An Implementation of REST API server following clean architecture principles using Golang
https://github.com/hmsayem/personnel-api
clean-architechture docker firestore golang kubernetes redis rest-api
Last synced: about 1 month ago
JSON representation
An Implementation of REST API server following clean architecture principles using Golang
- Host: GitHub
- URL: https://github.com/hmsayem/personnel-api
- Owner: hmsayem
- Created: 2021-12-07T13:24:46.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-10T14:49:03.000Z (almost 1 year ago)
- Last Synced: 2024-06-21T00:07:05.805Z (7 months ago)
- Topics: clean-architechture, docker, firestore, golang, kubernetes, redis, rest-api
- Language: Go
- Homepage:
- Size: 3.11 MB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
### Personnel API
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.
### Features:
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#### Overview:
#### Control Flow:
### API Reference
##### Get all employees
```http
GET /employees
```##### Get an employee
```http
GET /employees/${id}
```##### Add an employee
```http
POST /employees
```### Run Locally
Install and start Redis server for caching.
```bash
sudo apt install redis-server
sudo systemctl status redis-server
```
Clone the project.```bash
git clone https://github.com/hmsayem/clean-architecture-implementation.git
```
Go to the project directory.```bash
cd clean-architecture-implementation
```Copy all third-party dependencies to vendor folder.
```bash
go mod vendor
```Export environment variables.
```bash
GOOGLE_APPLICATION_CREDENTIALS=/path/to/project-private-key.jsonSERVER_PORT=:8000
REDIS_SERVER_HOST=localhost:6379
```Start the server.
```bash
go run .
```### Run with Docker
#### Run Redis Server
```bash
docker run --name redis --net=host -d redis
```
#### Run API server
Build image.```bash
docker build -t employee-server .
```
Run container.```bash
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
```### Deploy on Kubernetes
Create secret from `project-private-key.json`
```bash
kubectl create secret generic firestore-secret --from-file=/path/to/project-private-key.json
```
Create Configmaps
```bash
kubectl apply -f k8s/redis-server-cm.yaml
kubectl apply -f k8s/employee-server-cm.yaml
```
Create Pods
```bash
kubectl apply -f k8s/redis-server.yaml
kubectl apply -f k8s/employee-server.yaml
```
Create Services
```bash
kubectl apply -f k8s/redis-server-svc.yaml
kubectl apply -f k8s/employee-server-svc.yaml
```
Port Forward
```bash
kubectl port-forward svc/employee 8000
```### Examples of API Requests
##### Get all employees
```bash
❯ 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]"
}
]
```
##### Get an employee
```bash
❯ curl -X GET "http://localhost:8000/employees/81" | jq
{
"id": 81,
"name": "Piyush Kanti Das",
"title": "Software Engineer",
"team": "A",
"email": "[email protected]"
}
```##### Update an employee
```bash
❯ curl -X PUT "http://localhost:8000/employees/81" -d '{"title": "Senior Software Engineer"}'
```##### Add an employee
```bash
❯ 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]"}
```
### Run Unit Tests
##### Test Service Layer using Mock Repository
```bash
go test service/employee-service.go service/employee-service_test.go
```
### Acknowledgements
- [Pragmatic Reviews](https://www.youtube.com/c/PragmaticReviews)