How neko-dream/api works
Main Components
• API Server & CLI Tools
• The “cmd/server” directory contains the entry point for the API server, which listens for incoming HTTP requests.
• The “cmd/migration” directory holds a migration tool (for database encryption of demographics data) to assist in database setup and migrations.
• API Specification (OpenAPI)
• The “api” directory and subdirectories (especially “analysis.openapi.json”, “apidog.openapi.yaml”, etc.) indicate the project’s API specification. This is crucial to understand the endpoints and operations.
• Presentation Layer
• Found in “internal/presentation/handler” and “internal/presentation/oas”, this layer is responsible for routing HTTP requests, handling response formatting, and integrating with middleware (observed in “internal/infrastructure/http/middleware”).
• Use Case Layer
• “internal/usecase” organizes command and query operations. This layer orchestrates business logic by coordinating between the presentation layer and the domain layer.
• Domain Layer
• “internal/domain” is the heart of the business logic including domain models (e.g., user, opinion, talksession, vote) and domain services. This helps encapsulate the core rules and behaviors of the system.
• Infrastructure Layer
• This layer (inside “internal/infrastructure”) handles concerns such as persistence (PostgreSQL via “persistence”), authentication (JWT, OAuth in “auth”), cryptography, telemetries (sentry, otelinji) and external integrations.
• The “internal/infrastructure/di” directory indicates the use of dependency injection, uniting the domains, use cases, and presentations with their respective infrastructure implementations.
• Database connectivity and management is split between “internal/infrastructure/persistence” (repositories, query building, and SQLC generated files) and migration scripts are in the “migrations” folder.
• External client communication modules (e.g., “internal/infrastructure/external/analysis”) represent integration with third-party services.
• Deployment & DevOps
• Docker- and air-related files (“compose.yaml”, “container/Dockerfile”, .air.toml, GitHub workflows) help define the environment and deployment automation.
• The project uses Docker Compose to manage the PostgreSQL database (noting that it requires PostgreSQL 16 with PostGIS).
Component Interactions
• Directional Data Flow:
• External clients (via REST/OpenAPI) send HTTP requests to the API Server.
• The entry point in “cmd/server” directs these requests to the presentation layer (HTTP handlers).
• Handlers trigger appropriate use case logic in “internal/usecase” (commands and queries).
• The use cases invoke domain services/logic (located in “internal/domain”).
• Persistence/repository calls in “internal/infrastructure/persistence” fetch or modify data in the PostgreSQL database.
• Authentication components based on JWT and OAuth (in “internal/infrastructure/auth”) protect and validate requests.
• Other external services (e.g., analysis via “internal/infrastructure/external/analysis”) are invoked as needed.
• Additional Flows:
• Middleware (in “internal/infrastructure/http/middleware”) intercepts requests to handle authentication, logging, or error management.
• The DI setup in “internal/infrastructure/di” wires together all the above layers.
• The generated OpenAPI specifications and scripts (in “scripts/”) support automated code generation and testing.
Architectural Patterns & Design Principles
• Clean/Onion Architecture:
• Presentation, use case, domain, and infrastructure layers are cleanly separated.
• Business rules reside in the domain layer to ensure that infrastructure concerns (such as DB access and HTTP handling) remain independent.
• Dependency Injection:
• The DI folders link the layers while promoting testability and loose coupling.
• Repository & Query Patterns:
• Persistence code uses SQLC-generated files and query-specific modules to abstract database operations.
Final Elements
• Each component must be clearly labeled (e.g., “API Server – cmd/server”, “HTTP Handlers”, “Use Cases”, “Domain Models”, “Repositories & Persistence”, “Authentication (JWT/OAuth)”, “External Services”, “Database (PostgreSQL with PostGIS)”).
• Rectangles for application layers/components.
• Optionally, include the build/deployment pipeline showing integration with the GitHub workflows and Docker Compose.
This structured approach will enable the stakeholders to quickly understand the architecture and the separation of concerns that ensures maintainability, testability, and scalability of the project.