- About The Project
- β¨ Features
- π οΈ Tech Stack
- π Getting Started
- π API Documentation
- π§ͺ Running Tests
- π Project Structure
This project implements a complete CRUD (Create, Read, Update, Delete) system for managing a product catalog. It is structured using a Clean Architecture approach on the backend and a Component-Based architecture on the frontend. The front-end structure is together with the backend only to facilitate the evaluation of the project.
The application is fully containerized with Docker Compose, creating an isolated network for the services to communicate. The user interacts with the React application, which in turn communicates with the Go API.
graph TD
subgraph Browser
A[React Frontend App]
end
subgraph "Your Machine (localhost)"
B[NGINX Container <br> Port 3000]
C[Go API Container <br> Port 8080]
end
subgraph "Internal Docker Network"
D[PostgreSQL Container <br> Port 5432]
end
A -- Interacts with --> B;
B -- Proxies API calls to --> C;
C -- Connects to --> D;
- β Full-Stack Application: A Go backend connected to a responsive React frontend.
- π³ Fully Containerized: Docker Compose setup for one-command startup of the API, Frontend, and Database.
- π Interactive API Documentation: Auto-generated via Swagger/OpenAPI from Go code comments.
- π§ͺ End-to-End Testing: A robust E2E test suite for the Go API that runs in an isolated environment.
- ποΈ Clean Architecture: Scalable and maintainable code structure on both backend and frontend.
- βοΈ Environment-based Configuration: Simple setup using
.envfiles.
- Backend: Go, Chi (Router), PostgreSQL
- Frontend: React, Vite, Axios
- Containerization: Docker, Docker Compose
- Testing: Go's native testing package, Testcontainers-go
- API Documentation: Swag (OpenAPI)
Follow these steps to get a local copy of the project up and running.
- Go (version 1.21 or higher)
- Docker & Docker Compose
- A local installation of PostgreSQL if you choose not to use Docker for the database.
-
Clone the repository:
git clone https://github.com/Maria-Leiliane/go-ecommerce-base.git cd go-ecommerce-base -
Configure Environment Variables: Copy the example environment file. The default values are configured for the local development method.
cp env.example .env
-
Install Frontend Dependencies:
cd ecommerce-frontend npm install cd ..
Choose one of the following methods to run the application.
This method builds and runs the entire full-stack application (Frontend, API, and Database) in isolated containers.
-
Build and run all services:
docker compose up --build -d
-
To view the real-time logs of the API:
docker compose logs -f api
-
Access the application:
- Frontend: Open your browser to
http://localhost:3000 - Backend API: Is accessible at
http://localhost:8080
- Frontend: Open your browser to
-
To stop and remove all containers:
docker compose down
This method is ideal for active development, allowing you to run the Go code directly on your machine.
Step 1: Prepare the PostgreSQL Database You need a running PostgreSQL server. Choose one of the sub-options below.
-
Sub-option A: Run PostgreSQL in Docker (Recommended for consistency)
# This command starts a PostgreSQL container using the default credentials docker compose up -d postgres -
Sub-option B: Use a Native PostgreSQL Installation This assumes you have PostgreSQL server already installed on your operating system.
-
Open
psqlwith a superuser (likepostgres):sudo -u postgres psql
-
Run the following SQL commands to create a dedicated user and database. Replace
myappuserandmypasswordwith your own secure credentials.CREATE DATABASE "products-db"; CREATE USER myappuser WITH ENCRYPTED PASSWORD 'mypassword'; GRANT ALL PRIVILEGES ON DATABASE "products-db" TO myappuser; \q
-
Step 2: Configure .env and Run the Application
-
Open the
.envfile you created earlier. -
Ensure the variables match your database setup (either the Docker defaults or the ones you created in Sub-option B).
DB_HOSTmust belocalhost.DB_HOST=localhost DB_PORT=5432 DB_USER=admin # Or 'myappuser' if you used the native setup DB_PASSWORD=admin # Or 'mypassword' DB_NAME=products-db
-
Install dependencies and run the Go application:
go mod tidy go run .
Your API will be available at http://localhost:8080.
Run the Frontend Application without docker
Open a third terminal in the frontend directory (go-ecommerce-base/ecommerce-frontend):
cd ecommerce-frontend
npm run devYou can now access the frontend at http://localhost:5173.
The Go API is documented using OpenAPI (Swagger).
-
To view the interactive documentation: With the backend running, navigate to: http://localhost:8080/swagger/index.html
-
To generate/update the documentation files:
swag init -g main.go
-
To test with Insomnia: Import the pre-exported Insomnia collection located at
collection/collections-openapi.yaml.
The project includes an E2E test suite for the backend.
-
To run all backend tests:
# This command will start a temporary database container for the test go test -v -timeout 60s ./...
.
βββ collection/ # Insomnia collection file for API testing.
βββ docs/ # Auto-generated Swagger/OpenAPI documentation files.
βββ ecommerce-frontend/ # React frontend application source code.
β βββ public/ # Static assets for the frontend (like logos, favicons).
β βββ src/ # Main source code for the React app.
β β βββ components/ # Reusable React components (Form, List, Header, etc.).
β β βββ services/ # Centralized API communication logic (axios).
β β βββ types/ # Component typing
β βββ Dockerfile # Instructions to build the production frontend container.
β βββ nginx.conf # Nginx configuration to serve the React app.
βββ internal/ # Private Go application code (not importable by other projects).
β βββ domain/ # Core business entities and repository interfaces.
β βββ handler/http/ # HTTP handlers that manage requests and responses.
β βββ storage/ # Database repository implementation.
βββ Dockerfile # The blueprint for building the Go backend Docker image.
βββ docker-compose.yml # The orchestration file to run the full-stack application.
βββ e2e_test.go # The end-to-end test suite for the Go API.
βββ go.mod / go.sum # Go module files defining the backend's dependencies.
βββ main.go # The entry point for the Go backend application.
βββ README.md # This documentation file.
βββ README.pt-br.md # Portuguese version of this file