How go-http-server/core works
Main Components
• API Layer: The API endpoints and middleware reside in the "api" directory (files like auth_user.go, middleware.go, and server.go) – responsible for handling HTTP requests.
• Command-Line Server: Located in "cmd" (cmd/server.go), this is the application’s entry point that initializes and starts the HTTP server.
• Database Layer: Found in the "internal/database" directory (including sqlc generated code under "sqlc" and raw SQL queries). This layer handles all database interactions with PostgreSQL along with transaction management.
• Worker Processes: In the "worker" directory, these modules handle background processes such as the verification email distribution (distributor, processor, etc.).
• Plugin System: In "plugin/pkg" there are subcomponents for mailing service (mailer) and token generation/verification (token), which encapsulate the core business logic related to sending emails and creating/verifying tokens.
• Utility Layer: Under "utils" (files for configuration, error handling, password management, etc.) which supports common tasks across the system.
• Infrastructure and Configuration: Dockerfile, docker-compose.yml, environment files (.env.example, docker.env) provide deployment configuration and system environment details.
Relationships and Interactions
• The HTTP Server (cmd/server.go) receives user requests routed through the API (api folder) where middleware (authentication) intercepts requests.
• API endpoints trigger business logic:
• For registration, after validating user details a new user is created via a transactional database call. A message is enqueued (likely pushed to Redis as implied by the README) for asynchronous processing.
• For login, after user validation, a token is generated using the token plugin (PASETO).
• The Database Layer (internal/database) interacts with the PostgreSQL database (using connection details provided in environment variables, e.g., DB_SOURCE) – executed via sqlc generated methods.
• The Worker Processes (worker directory) pick up tasks from the Redis queue, process email sending (invoking the mailer plugin), and perform logging/reporting functions.
• The plugins for mailer and token are used across the service – these abstract external dependencies and provide a clear separation of concerns.
Architectural Patterns & Important Principles
• Modular Design: The separation into directories such as “api”, “internal”, “plugin”, “worker”, and “utils” indicates a decoupled system where each module has a specific responsibility.
• Middleware and Layered Architecture: HTTP request flow is intercepted by middleware that performs tasks like authentication before reaching the business logic.
• Asynchronous Processing: The use of a redis queue to decouple heavy tasks (like sending emails) from the immediate HTTP response, ensuring a responsive user experience.
• Transaction Management: The database interaction (with commit/rollback) is explicitly designed for resilience and data integrity during operations such as registration.
• Extensibility: The plugin approach allows for easy replacement or modification of services like email sending and token generation.
Final Touches
• Interaction flows: for instance, illustrating how a registration request triggers a database transaction that, upon success, enqueues a verification email request into Redis, which is later handled by the Worker through the Mailer Plugin.
• Given that this project is designed as a simple backend foundation, the focus should be on clarity around these core components rather than overly detailed internal class/module representations.