This project implements an AI-driven bot designed to increase sales, followers, and engagement for Print on Demand (PoD) businesses, leveraging free-tier cloud resources and AI services like Gemini and OpenAI.
This application has been enhanced for production readiness with a focus on robust data handling, optimization, and scalability.
- Centralized Error Handling: Implemented a global error handling middleware to ensure consistent error responses.
- Input Validation: Integrated Joi for validating incoming API request data and BullMQ job payloads, preventing malformed data from entering the system.
- Optimized Asynchronous Processing: Utilized BullMQ for job queuing, with added retry mechanisms (exponential backoff) and improved error logging for workers, enhancing resilience against transient failures.
- Robust Configuration Management: Environment variables are now validated at application startup to ensure all necessary configurations are present and correctly formatted.
- External API Call Resilience: A custom
ApiClienthas been implemented with features like request timeouts, automatic retries, and a circuit breaker pattern to gracefully handle external API (e.g., Reddit, Facebook) unresponsiveness or failures. - Dockerization: A
Dockerfilehas been provided for the backend service, enabling easy containerization and deployment.
For data modification operations (e.g., saving posts, comments), idempotency is crucial to prevent duplicate data or unintended side effects during retries. This is primarily handled through:
- Database Constraints: Utilizing unique constraints on relevant database fields (e.g.,
facebook_post_idfor posts,facebook_comment_idfor comments) to prevent duplicate entries. - Careful Job Design: Ensuring that worker jobs are designed to check for existing records before creating new ones, or to update existing records rather than always inserting.
Further enhancements for idempotency would involve implementing unique request IDs for all external API calls and storing them to prevent reprocessing, or using transactional outbox patterns for critical operations.
While several security measures have been implemented (e.g., environment variable validation, rate limiting), a comprehensive security audit is recommended for a production environment. Key areas for further consideration include:
- Authentication and Authorization: Implementing robust user authentication and authorization mechanisms for accessing internal APIs.
- Data Encryption: Ensuring all sensitive data is encrypted at rest and in transit.
- Vulnerability Scanning: Regularly scanning dependencies and the application code for known vulnerabilities.
- Least Privilege Principle: Ensuring that services and users only have the minimum necessary permissions.
- API Key Management: Securely managing and rotating API keys.
To run the backend application:
- Navigate to the
backenddirectory:cd backend - Install dependencies:
npm install
- Set up your environment variables in a
.envfile (refer to.env.exampleor theenvValidation.tsfor required variables). - Build the application:
npm run build
- Start the application:
npm start
Alternatively, you can use Docker:
- Navigate to the
backenddirectory:cd backend - Build the Docker image:
docker build -t pod-growth-bot-backend . - Run the Docker container:
docker run -p 3000:3000 --env-file ./.env pod-growth-bot-backend
(This section can be expanded with more details about each directory and its purpose.)
(Add contributing guidelines here.)