2eff52877b
retrieve document by id and tests * working * missing files
queryAPI Package
This directory contains the REST API implementation for the Query Orchestration platform. The code here serves as the HTTP interface layer between external clients and the internal business logic.
Architecture Overview
Controllers Struct (controllers.go)
The Controllers struct is the central architectural component that implements the Service Layer/Facade pattern for the entire REST API.
Purpose
- OpenAPI Contract Implementation: Implements the
ServerInterfaceinterface generated fromserviceAPIs/queryAPI.yaml - Service Aggregation: Consolidates 11 different business services into a single, cohesive API layer
- Dependency Injection Container: Provides clean separation between HTTP handlers and business logic
Structure
type Controllers struct {
svc *Services // Aggregated business services
cfg ConfigProvider // Combined auth & object storage configuration
}
Pattern Benefits
- Single Responsibility: Each handler method focuses solely on HTTP↔Service translation
- Testability: Services can be easily mocked for unit testing
- Modularity: New services can be added without changing the controller pattern
- Type Safety: Ensures compile-time checking against OpenAPI specification
Key Architectural Components
1. Generated Code (api.gen.go)
- DO NOT EDIT: This file is auto-generated from
serviceAPIs/queryAPI.yaml - Contains all request/response types and the
ServerInterfaceinterface - Regenerated via
task generatecommand - Ensures API contract consistency
2. Handler Files
Each file implements specific domain endpoints:
client.go- Client management operationscollector.go- Query collector configurationdocuments.go- Document upload and managementexport.go- Export operationsquery.go- Query execution and managementstatus.go- Health check and status endpointsauthHandlers.go- Authentication flow handlers (login, logout, callbacks)
3. Service Dependencies
The Controllers struct aggregates these internal services:
- Export Service - Manages export processes
- Collector Service - Handles query collections
- Query Service - Query execution logic
- Client Service - Client entity management
- Document Service - Document processing
- DocumentUpload Service - S3 upload handling
- DocumentBatch Service - Batch document operations
4. Authentication Integration
- Uses AWS Cognito for JWT-based authentication
- Permit.io integration for RBAC (Role-Based Access Control)
- Authentication middleware validates tokens before reaching handlers
ConfigProviderinterface provides auth configuration access
Handler Pattern
All handlers follow a consistent pattern:
func (s *Controllers) HandlerName(ctx echo.Context, params...) error {
// 1. Bind/validate request data
req := RequestType{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
// 2. Call appropriate service method
result, err := s.svc.ServiceName.Method(ctx.Request().Context(), ...)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Error message")
}
// 3. Return formatted response
return ctx.JSON(http.StatusOK, ResponseType{...})
}
Testing Structure
*_test.gofiles provide unit tests for each handler grouptest_helpers.gocontains shared test utilities and fixtures- Tests use mocked services to validate handler logic in isolation
- Integration tests exist at the service layer level
Important Notes for Maintenance
- OpenAPI-First Development: Always update
serviceAPIs/queryAPI.yamlfirst, then regenerate code - Service Layer Separation: Business logic belongs in
internal/services, not in handlers - Error Handling: Use Echo's
HTTPErrorfor consistent error responses - Context Propagation: Always pass
ctx.Request().Context()to service methods for proper cancellation - No Direct Database Access: All data operations must go through service layer
- Consistent Status Codes: Follow REST conventions (201 for creation, 200 for success, 400 for client errors)
Development Workflow
- Modify OpenAPI spec in
serviceAPIs/queryAPI.yaml - Run
task generateto update generated code - Implement/update handler methods in Controllers
- Add/update tests for new functionality
- Run
task lintto ensure code quality - Run
task test:functionalto validate changes
Configuration
The ConfigProvider interface combines:
- Authentication configuration (Cognito settings, JWT validation)
- Object storage configuration (S3 bucket settings)
- Base service configuration (ports, logging, database)
This unified configuration is injected into Controllers at startup, providing all necessary runtime settings.