Architecture
Overview
The IBW Backend Service follows a layered architecture pattern with clear separation of concerns. This design ensures maintainability, testability, and scalability.
Architecture Layers
1. Handler Layer (internal/handlers/)
The handler layer is responsible for:
- HTTP request/response handling
- Request validation
- Response formatting
- Error handling
Key Handlers:
user_handler.go- User management endpointsevent_handler.go- Event management endpointsquest_handler.go- Quest system endpointsadmin_handler.go- Admin operationsnotification_handler.go- Notification endpointsconnection_handler.go- User connectionsspeaker_handler.go- Speaker managementconference_agenda_handler.go- Conference agendaconference_attendee_handler.go- Conference attendance
2. Service Layer (internal/services/)
The service layer contains business logic:
- Business rules and validations
- Orchestration of multiple repositories
- External API integrations
- Complex business workflows
Key Services:
user_service.go- User business logicevent_service.go- Event business logicquest_service.go- Quest system logicnotification_service.go- Notification logicenrichment_service.go- Social media enrichmenttelegram_service.go- Telegram integrationverification_service.go- Task verification
3. Repository Layer (internal/repositories/)
The repository layer provides data access abstraction:
- Database queries
- Data mapping
- Transaction management
- Cache integration
Key Repositories:
user_repository.go- User data accessevent_repository.go- Event data accessquest_repository.go- Quest data accessnotification_repository.go- Notification data access
4. Model Layer (internal/models/)
Domain models representing business entities:
user.go- User modelevent.go- Event modelquest.go- Quest modelconference.go- Conference modelspeaker.go- Speaker modelnotification.go- Notification model
Dependency Injection
The application uses a Dependencies struct (internal/deps/deps.go) to manage all dependencies:
type Dependencies struct {
DB *gorm.DB
Cache *redisStorage.Storage
Cfg *config.Config
UserService *services.UserService
EventService *services.EventService
QuestService *services.QuestService
// ... more dependencies
}This pattern:
- Centralizes dependency management
- Simplifies testing with mocks
- Reduces coupling between components
Server Initialization
The server initialization flow (internal/server/server.go):
- Load Configuration - Environment variables and config
- Initialize Database - PostgreSQL connection via GORM
- Initialize Cache - Redis connection
- Create Repositories - Data access layer
- Create Services - Business logic layer
- Create Handlers - HTTP handlers
- Register Routes - API endpoints
- Start Workers - Background job workers
Middleware Stack
The application uses Fiber middleware for:
- Logging - Request/response logging
- Recovery - Panic recovery
- CORS - Cross-origin resource sharing
- Request ID - Unique request tracking
- Authentication - JWT and Telegram auth
- Rate Limiting - API rate limiting
- Admin Auth - Admin API key validation
Background Workers
Notification Job Worker
The NotificationJobService runs a background worker that:
- Processes scheduled notifications
- Sends bulk notifications
- Tracks notification job status
- Handles retries and failures
Started in server.go:
notificationJobService.StartWorker(ctx)External Integrations
Telegram Integration
- User authentication via Telegram bot
- Notification sending
- Group chat integration
Social Media APIs
- Twitter: Profile and tweet fetching via Apify
- LinkedIn: Profile data fetching
- Enrichment: AI-powered social media enrichment
Storage Services
- Azure Blob Storage: Image/video storage
- Cloudflare R2: CDN and storage
AI Services
- Azure OpenAI: Content generation and enrichment
Database
ORM: GORM
- Database abstraction
- Migration support
- Relationship management
Connection Pooling
- pgx connection pool
- Configurable pool size
- Connection health checks
Caching Strategy
Redis is used for:
- Session storage
- Rate limiting
- Temporary data caching
Error Handling
Errors are handled at multiple levels:
- Repository Level: Database errors
- Service Level: Business logic errors
- Handler Level: HTTP error responses
Standard error response format:
{
"error": "Error message",
"code": "ERROR_CODE"
}Logging
Uses Zerolog for structured logging:
- Request/response logging
- Error tracking
- Performance monitoring
- Debug information
Security
-
Authentication:
- JWT tokens for API access
- Telegram bot tokens for user auth
-
Authorization:
- Admin API keys
- Role-based access control
-
Rate Limiting:
- Per-endpoint rate limits
- Redis-based rate limiting
-
Input Validation:
- Request validation
- SQL injection prevention (via GORM)
- XSS protection
Scalability Considerations
- Stateless Design: No server-side sessions
- Connection Pooling: Efficient database connections
- Caching: Redis for frequently accessed data
- Background Workers: Async job processing
- Horizontal Scaling: Can run multiple instances