A small RESTful CRUD JSON API microservice in Go for managing products in a store.
- Go 1.23
- docker
- docker-compose
This folder contains the main application code, including handlers, repositories, and data access objects (DAOs).
handler
: Contains the HTTP handlers that define the API endpoints and their logic.router
: Contains the API endpoints of the Application.repository
: Contains the repository layer that interacts with the database.dao
: Contains the data access objects that represent the database models.docs
: Contains the API docs that are generated for swagger.db
: Contains the database connection for the application.tests
: Contains the test files for the application. It includes integration tests to ensure the functionality of the application.
This project is implemented using the following technologies:
postgres(v15)
as the databasegin-gonic/gin
for the web frameworkgorm.io/gorm
for the ORMswaggo/swag
for the API documentation ({baseUrl}/swagger/index.html)stretchr/testify
&&gin-gonic/gin
for the tests
The id of the products are UUIDs
Method | Endpoint | Data accepted | Description |
---|---|---|---|
GET | /api/v1/products | query parameter page and limit (default: page=1 limit=10) | Get all products |
GET | /api/v1/products/{id} | path parameter id | Get a product |
POST | /api/v1/products/create | json payload | Create product/s |
PUT | /api/v1/products/update/{id} | path parameter id and json payload | Update a product |
DELETE | /api/v1/products/delete/{id} | path parameter id | Delete a product |
DELETE | /api/v1/products/bulk/delete | json payload | Delete a list of products |
GET | /swagger/index.html | - | API documentation |
In the Swagger documentation, you will find all the endpoints along with the data they accept. Additionally, examples of the data that the endpoints accept and return are provided. You can also test the endpoints directly within the Swagger documentation. For more details, please refer to the API Swagger documentation available locally at API SWAGGER documentation .
- Make sure that in the .env file the
DB_HOST
is set with the value pgsql
You can manually change the variable in the .env file or run the following command:
sed -i 's/^DB_HOST=.*/DB_HOST=pgsql/' .env
- Run the following command to build the go-app into a docker image:
docker build -t phrp/simple-crud:1.0 .
- Then run the following command to run the docker-compose file that contains the go-app and the postgres database:
docker-compose up
- Make sure that in the .env file the
DB_HOST
is set with the value localhost
You can manually change the variable in the .env file or run the following command:
sed -i 's/^DB_HOST=.*/DB_HOST=localhost/' .env
- Run the following command to create a postgres database:
docker-compose up pgsql ## to run only the pgsql service
- Run the following command to download and install the dependencies:
go mod tidy
- Run the following command to start the go-app:
go run main.go
To run the tests, run the following command:
go test ./tests
The tests are individual, and they are using an in-memory database (SQLite3). After each test, we flush the in-memory database so we can have individual tests. Each test verifies if the endpoints work correctly.