How chaihaobo/go-backend-template works
Main system components
• Infrastructure Layer: This includes all foundational services such as database and cache implementations. Under "infrastructure" you have subdirectories for caching (with Redis clients and options) and data storage (with separate implementations for database clients and repositories that work with your data entities).
• Application Layer: Located in the "application" directory, this is where core business logic resides. Here, you have modules like “health” and “user” that encapsulate the primary service logic.
• Transport Layer: Found in "transport", this layer handles communication protocols such as HTTP and gRPC. It contains controllers for different service endpoints (e.g., hello, health, user) along with middleware for tasks like authentication.
• Resource Layer: In "resource", near runtime the project configures and provides essential services such as configuration management, logging, metrics (with Prometheus), tracing, and input validation.
• Model Layer: The "model" directory defines data structures, including entity models that represent database tables and DTOs (data transfer objects) that facilitate data exchange across layers.
• Additional Components:
• The "cmd" directory contains entry points and command-line utilities for initiating the different types of services (e.g., GRPC, HTTP).
• Tools such as JWT and crypto utilities in "tools", support authentication and secure processing.
• The "proto" directory holds protocol buffer definitions and generated code that support the gRPC interfaces.
Relationships and interactions
• The Transport layer receives external requests (HTTP/gRPC) and forwards them to the Application layer.
• The Application layer processes these requests by invoking business logic (services for user and health) and then interacts with the Infrastructure layer’s repositories and caches.
• The Infrastructure layer directly interacts with the underlying data stores and cache systems.
• The Resource layer is used across the application to initialize and manage configurations, logging, metrics, and tracing, ensuring that every component has access to shared runtime facilities.
• The Model layer acts as a shared contract between layers by defining entities for persistence and DTOs for communication.
Important architectural patterns and design principles
• Layered Architecture: The project is cleanly segmented into layers (infrastructure, application, transport, resource, model), each with its clear responsibility.
• Separation of Concerns: Business logic is isolated from protocol handling, data persistence, and resource management.
• Dependency Direction: Dependencies flow from the transport layer to application, and from the application layer to infrastructure (and from lower layers to higher layers, like resource being a common utility).
• Modular Design: With clear directories for different services (e.g., user, health) and separate handling for different protocols, the design promotes extensibility and maintainability.
Convey relevant technologies and components
• Go language serves as the primary implementation.
• GRPC, HTTP, and possibly other protocols are supported in separate subcomponents under the transport layer.
• Redis (or similar) for caching and a relational or NoSQL datastore for persistence are indicated in the infrastructure layer.
• Supplementary libraries and tools for logging, metrics (Prometheus), tracing, and validation in the resource layer are included.
• The use of Protocol Buffers (in the proto directory) for gRPC defines clear API contracts.