hmsayem / personnel-api

An Implementation of REST API server following clean architecture principles using Golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:

Application Structure:

Control Flow:

Request Flow

API Reference

Get all employees
GET /employees
Get an employee
GET /employees/${id}
Add an employee
POST /employees

Run Locally

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 .

Run with Docker

Run Redis Server

docker run --name redis --net=host -d redis

Run API server

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

Deploy on Kubernetes

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

Examples of API Requests

Get all employees
❯ 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"
  }
]
Get an employee
❯ curl -X GET  "http://localhost:8000/employees/81" | jq
{
  "id": 81,
  "name": "Piyush Kanti Das",
  "title": "Software Engineer",
  "team": "A",
  "email": "piyush@gmail.com"
}
Update an employee
 ❯ curl -X PUT "http://localhost:8000/employees/81" -d '{"title": "Senior Software Engineer"}'
Add an employee
 ❯ 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"}

Run Unit Tests

Test Service Layer using Mock Repository
go test service/employee-service.go  service/employee-service_test.go

Acknowledgements

About

An Implementation of REST API server following clean architecture principles using Golang


Languages

Language:Go 94.7%Language:Dockerfile 3.9%Language:Makefile 1.4%