Here's a comprehensive README file that includes all the necessary information:
This project is a User Management API built with Go (Golang) and Echo. It includes CRUD operations for users and uses PostgreSQL for data storage. The project also uses Docker for containerization and Ginkgo for testing.
- Environment Variables
- Installing The Database
- Installing and Setting Up Docker
- Working with the Makefile
- Running the Ginkgo Tests
- Postman Collection
Create a .env file in the root directory of your project and add the following variables:
SERVER_ADDRESS=:8080
POSTGRES_USER=root
POSTGRES_PASSWORD=admin
POSTGRES_DB=userapi
POSTGRES_HOST=db
DATABASE_URL=postgres://root:admin@db:5432/userapi?sslmode=disablemake migration-up
- Download Docker Desktop: Download and install Docker Desktop from the Docker website.
- Install Docker Desktop: Open the downloaded
.dmgfile and drag the Docker icon to your Applications folder. - Run Docker: Open Docker from your Applications folder. You should see the Docker icon in your menu bar indicating Docker is running.
-
Update Package Information: Open a terminal and run the following commands:
sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg \ lsb-release -
Add Docker’s Official GPG Key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg -
Set Up the Stable Repository:
echo \ "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
-
Install Docker Engine:
sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
-
Verify Docker Installation:
sudo docker run hello-world
-
Build and Start the Containers: Run the following command to build and start the Docker containers:
docker compose up --build
-
Stop the Containers: To stop the containers, press
Ctrl+Cin the terminal wheredocker-compose upis running or run:docker compose down
The Makefile includes various commands to build, run, test, and clean the project.
-
Build the Application:
make build
-
Run the Application:
make run
-
Run the Ginkgo Tests:
make ginkgo
-
Generate Swagger Documentation:
make generate-docs
-
Serve Swagger Documentation:
make serve-docs
-
Clean the Built Files:
make clean
To run the tests using Ginkgo, first ensure you have the Ginkgo CLI installed globally:
go install github.com/onsi/ginkgo/v2/ginkgo@latestRun the tests using the following command:
ginkgo -r -vThis command will recursively find and run all the tests in your project directories with verbose output.
You can use the provided Postman collection to test the API endpoints. Import the collection into Postman to get started.
{
"info": {
"name": "User Management API",
"description": "Collection of API requests for the User Management API",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Get All Users",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/v1/users",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"v1",
"users"
]
}
},
"response": []
},
{
"name": "Get User by ID",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:8080/v1/users/1",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"v1",
"users",
"1"
]
}
},
"response": []
},
{
"name": "Create User",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\"name\":\"John Doe\",\"email\":\"john.doe@example.com\"}"
},
"url": {
"raw": "http://localhost:8080/v1/users",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"v1",
"users"
]
}
},
"response": []
},
{
"name": "Update User",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\"name\":\"John Doe Updated\",\"email\":\"john.doe.updated@example.com\"}"
},
"url": {
"raw": "http://localhost:8080/v1/users/1",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"v1",
"users",
"1"
]
}
},
"response": []
},
{
"name": "Delete User",
"request": {
"method": "DELETE",
"header": [],
"url": {
"raw": "http://localhost:8080/v1/users/1",
"protocol": "http",
"host": [
"localhost"
],
"port": "8080",
"path": [
"v1",
"users",
"1"
]
}
},
"response": []
}
]
}To import the collection into Postman:
- Open Postman.
- Click on "Import" in the top left corner.
- Select the "Raw Text" option.
- Paste the JSON data above.
- Click "Continue" and then "Import".
This README file provides comprehensive instructions for setting up and working with the User Management API project, including environment variables, Docker setup, Makefile usage, running tests with Ginkgo, and using the Postman collection for API testing.