How quangdangfit/goshop works
Project Overview
• This is an e-commerce backend built with Go, using the Gin framework. It exposes both RESTful APIs and gRPC endpoints.
• Its purpose is to serve as the server for an e-commerce website, handling domains like carts, orders, products, and users.
• Key features include DDD (Domain-Driven Design) architecture, integration with Postgres (database), Redis (cache), JWT-based authentication, and API documentation via Swagger.
Main Components
a. Client Layer:
– External Consumers: Clients (e.g., web or mobile apps) interacting through HTTP (REST API) and possibly gRPC.
b. API and Service Layers:
– HTTP Server (cmd/api and internal/server/http): Handles RESTful interactions, routes (found under internal/order/port/http, internal/product/port/http, internal/user/port/http).
– gRPC Server (internal/server/grpc and grpc handlers under internal/cart/port/grpc, internal/user/port/grpc): Handles gRPC calls.
c. Domain Modules:
– Cart, Order, Product, User functionalities are separated by domain. Each domain has its own packages:
▪ DTOs (Data Transfer Objects)
▪ Models
▪ Repositories (with mocks for testing)
▪ Services (business logic, again with mocks)
▪ Ports (interface adapters for both HTTP and gRPC for Order, Product, and User).
d. Ancillary and Shared Components:
– pkg/config: Handles configuration loading (parameters such as ports, database, and redis URIs).
– pkg/dbs: For database connectivity abstracting Postgres.
– pkg/redis: For Redis interaction.
– pkg/middleware: Handles authentication and other cross-cutting concerns.
– pkg/jtoken, pkg/paging, pkg/response, pkg/utils: Utilities and common functions.
– proto: Contains gRPC proto definitions for cart and user (and auto-generated code under its “gen” subdirectories).
Relationships and Interactions
– Clients (web/mobile) interface with the HTTP API (via REST) and gRPC endpoints. These flows are distinct:
▪ REST: Primarily used for orders, products, and users.
▪ gRPC: Used for cart operations and additional user endpoints.
– The API layers invoke domain services defined in internal/*/service packages.
– Services manipulate data through repositories (internal/*/repository) following DDD principles.
– All major business domains share access to a common configuration (from pkg/config) and persistence layers:
▪ Data persistence through Gorm interacting with a PostgreSQL database (see pkg/dbs).
▪ Caching and session handling using Redis (see pkg/redis).
– Middleware (in pkg/middleware) secures requests using JWT (pkg/jtoken).
Architectural Patterns & Design Principles
– Domain Driven Design (DDD): Each subdirectory under “internal” represents a bounded context (e.g., cart, order, product, user).
– Separation of Concerns: Clear decoupling of API layer (HTTP & gRPC) from business logic (services) and data access (repositories).
– Testability: Presence of dedicated mocks for repositories and services indicates an emphasis on unit testing and modular design.
– Use of code generation: The proto folder and associated generated code showcase a structured approach to managing gRPC contracts.