How codergogoi/go-ecommerce-series works
Main Components
• Entry Point & Configuration: The main.go file acts as the entry point, with additional configuration support provided in the config/ directory and docker-compose.yml for container orchestration.
• API Layer (HTTP & REST): Under internal/api/rest and its handlers (catalogHandler.go, transactionHandler.go, userHandler.go), this layer handles incoming HTTP requests. Include the httpHandler.go and response.go for request routing and response formulation.
• Domain Models: Found in internal/domain, these files (Address.go, BankAccount.go, Cart.go, Category.go, Order.go, OrderItem.go, Payment.go, Product.go, User.go) represent core business entities.
• Data Transfer Objects (DTOs): Located in internal/dto, this layer abstracts request and response data (for example, ShoppingRequestDto.go, categoryRequestDto.go, produstRequestDto.go, etc.).
• Repositories: Under internal/repository, these files (catalogRepository.go, transactionRepository.go, userRepository.go) handle data persistence and retrieval; they abstract the interactions with the database or any storage.
• Services: In internal/service, business logic is implemented (e.g., catalogService.go, transactionService.go, userService.go) to process the business use cases.
• Helper/Utility Functions: The internal/helper directory (including auth.go and utility.go) provides support functions such as authentication and common utilities.
• External Integration: The pkg/notification directory (sendSms.go) indicates integration with external services, such as sending SMS notifications (e.g., via Twilio as referenced in the README).
Establish Relationships and Data Flow
• The API server (internal/api/server.go along with rest/httpHandler.go) listens for incoming HTTP requests. Each request is directed to a specific REST handler (catalogHandler, transactionHandler, userHandler).
• Each handler delegates work to the respective Service layer (catalogService, transactionService, userService), which contains the business logic.
• The Service layer calls upon the Repository layer to manage data persistence and retrieval.
• Domain models are used both within the Repository and Service layers to represent business entities. Data sent over the API is encapsulated in DTOs.
• Auxiliary helper functions (internal/helper) support these processes—such as handling authentication.
• On the notifications front, the pkg/notification component suggests integration points for external communication (e.g., sending SMS after certain actions).
Architectural Patterns and Design Principles to Emphasize
• Clean Separation of Concerns: There’s a clear separation between the API (HTTP), business logic (Service), domain models, data access (Repository), and auxiliary functionality.
• Layered Architecture: The structure follows a layered approach where high-level components (API) rely on mid-tier services and lower-level data repositories.
• Modularity & Extensibility: The use of DTOs, modular handlers, and service components allows for clean extensions (e.g., additional endpoints, services, or integrations).