How henvic/pgxtutorial works
Project Type & Purpose
• This is a tutorial application demonstrating how to build a backend service in Go that interacts with a PostgreSQL database.
• It includes examples of API (both HTTP and gRPC), database interactions, domain logic in the “inventory” package, and telemetry for monitoring.
• The project also includes infrastructure for CI/CD (GitHub workflows) and migration scripts to set up the PostgreSQL schema.
Key Components
a. API Layer(s):
• HTTP API: Represent this with the HTTP handler code located in “internal/api/http.go”.
• gRPC API: Represent this with the gRPC server code in “internal/api/grpc.go” along with the protobuf definitions in “internal/apiv1/api.proto” and generated code.
b. Database Layer:
• Migrations folder which is used to create the initial database schema.
c. Domain/Business Logic:
d. Telemetry:
• “internal/telemetry” package. This module, along with its test utility located in “internal/telemetry/telemetrytest”, is responsible for exporting metrics and traces.
e. Main Application:
• The “main.go” serves as the bootstrap of the application, tying together the HTTP and gRPC servers, database initialization, and telemetry.
Relationships and Interactions
• That both API endpoints invoke the Inventory domain/service, which subsequently communicates with the database layer.
• That the database layer communicates with PostgreSQL and that migration scripts are used during the setup or deployment phase.
• Interactions from various layers (particularly API and inventory) with the telemetry component for logging/tracing.
Architectural Patterns and Design Principles
• Separation of concerns: API concerns (network communication) are separated from the business logic (inventory services) and further separated from data access (database & postgres).
• Use of gRPC and Protobuf for versioned APIs, which implies a clear boundary between internal service communication and external integration.
• Modularity: each internal package (api, database, inventory, telemetry) is built to be independently testable, as seen by their accompanying test files.
Relevant Technologies and Tools
• Programming Language: Go
• Database: PostgreSQL
• API Communication: HTTP for REST endpoints and gRPC for RPC-based communication (with Protocol Buffers)
• Observability: Telemetry (with configurable exporters via environment variables)
• CI/CD: GitHub Actions (workflows are defined under .github/workflows)
• Build and Dependency Management: go.mod, go.sum