- Modified _ensure_initialized() to run in background thread (non-blocking) - Added _ensure_initialized_async() for truly async initialization - Updated index_content() to return immediately without waiting for initialization - Weights now load in background thread instead of blocking event loop - Added initialization tracking to prevent duplicate initialization - Modified today_workflow API to handle non-blocking indexing gracefully - This prevents dashboard refresh from blocking other services When a user accesses the dashboard, the indexing now happens in background instead of blocking the HTTP response, allowing other services to function normally while weights are being loaded.
ALwrity Backend
Welcome to the ALwrity Backend! This is the FastAPI-powered backend that provides RESTful APIs for the ALwrity AI content creation platform.
🚀 Quick Start
Prerequisites
- Python 3.8+ installed
- pip (Python package manager)
1. Install Dependencies
cd backend
pip install -r requirements.txt
2. Start the Backend Server
python start_alwrity_backend.py
3. Verify It's Working
- Open your browser to: http://localhost:8000/api/docs
- You should see the interactive API documentation
- Health check: http://localhost:8000/health
📁 Project Structure
backend/
├── app.py # FastAPI application definition
├── start_alwrity_backend.py # Server startup script
├── requirements.txt # Python dependencies
├── api/
│ ├── __init__.py
│ └── onboarding.py # Onboarding API endpoints
├── services/
│ ├── __init__.py
│ ├── api_key_manager.py # API key management
│ └── validation.py # Validation services
├── models/
│ ├── __init__.py
│ └── onboarding.py # Data models
└── README.md # This file
🔧 File Descriptions
Core Files
app.py - FastAPI Application
- What it does: Defines all API endpoints and middleware
- Contains:
- FastAPI app initialization
- All API routes (onboarding, health, etc.)
- CORS middleware for frontend integration
- Static file serving for React frontend
- When to edit: When adding new API endpoints or modifying existing ones
start_alwrity_backend.py - Server Startup
- What it does: Enhanced startup script with dependency checking
- Contains:
- Dependency validation
- Environment setup (creates directories)
- User-friendly logging and error messages
- Server startup with uvicorn
- When to use: This is your main entry point to start the server
Supporting Directories
api/ - API Endpoints
- Contains modular API endpoint definitions
- Organized by feature (onboarding, etc.)
- Each file handles a specific domain of functionality
services/ - Business Logic
- Contains service layer functions
- Handles database operations, API key management, etc.
- Separates business logic from API endpoints
models/ - Data Models
- Contains Pydantic models and database schemas
- Defines data structures for API requests/responses
- Ensures type safety and validation
🎯 How to Start the Backend
Option 1: Recommended (Using the startup script)
cd backend
python start_alwrity_backend.py
Option 2: Direct uvicorn (For development)
cd backend
uvicorn app:app --reload --host 0.0.0.0 --port 8000
Option 3: Production mode
cd backend
uvicorn app:app --host 0.0.0.0 --port 8000
🌐 What You'll See
When you start the backend successfully, you'll see:
🎯 ALwrity Backend Server
========================================
✅ All dependencies are installed
🔧 Setting up environment...
✅ Created directory: lib/workspace/alwrity_content
✅ Created directory: lib/workspace/alwrity_web_research
✅ Created directory: lib/workspace/alwrity_prompts
✅ Created directory: lib/workspace/alwrity_config
ℹ️ No .env file found. API keys will need to be configured.
✅ Environment setup complete
🚀 Starting ALwrity Backend...
📍 Host: 0.0.0.0
🔌 Port: 8000
🔄 Reload: true
🌐 Backend is starting...
📖 API Documentation: http://localhost:8000/api/docs
🔍 Health Check: http://localhost:8000/health
📊 ReDoc: http://localhost:8000/api/redoc
⏹️ Press Ctrl+C to stop the server
============================================================
📚 API Documentation
Once the server is running, you can access:
- 📖 Interactive API Docs (Swagger): http://localhost:8000/api/docs
- 📊 ReDoc Documentation: http://localhost:8000/api/redoc
- 🔍 Health Check: http://localhost:8000/health
🔑 Available Endpoints
Health & Status
GET /health- Health check endpoint
Onboarding System
GET /api/onboarding/status- Get current onboarding statusGET /api/onboarding/progress- Get full progress dataGET /api/onboarding/config- Get onboarding configuration
Step Management
GET /api/onboarding/step/{step_number}- Get step dataPOST /api/onboarding/step/{step_number}/complete- Complete a stepPOST /api/onboarding/step/{step_number}/skip- Skip a stepGET /api/onboarding/step/{step_number}/validate- Validate step access
API Key Management
GET /api/onboarding/api-keys- Get configured API keysPOST /api/onboarding/api-keys- Save an API keyPOST /api/onboarding/api-keys/validate- Validate API keys
Onboarding Control
POST /api/onboarding/start- Start onboardingPOST /api/onboarding/complete- Complete onboardingPOST /api/onboarding/reset- Reset progressGET /api/onboarding/resume- Get resume information
🧪 Testing the Backend
Quick Test with curl
# Health check
curl http://localhost:8000/health
# Get onboarding status
curl http://localhost:8000/api/onboarding/status
# Complete step 1
curl -X POST http://localhost:8000/api/onboarding/step/1/complete \
-H "Content-Type: application/json" \
-d '{"data": {"api_keys": ["openai"]}}'
Using the Swagger UI
- Open http://localhost:8000/api/docs
- Click on any endpoint
- Click "Try it out"
- Fill in the parameters
- Click "Execute"
⚙️ Configuration
Environment Variables
You can customize the server behavior with these environment variables:
HOST: Server host (default: 0.0.0.0)PORT: Server port (default: 8000)RELOAD: Enable auto-reload (default: true)
Subscription billing (Stripe) variables used in deployment:
STRIPE_SECRET_KEY: Stripe API secret key (sk_test_...for test,sk_live_...for live).STRIPE_WEBHOOK_SECRET: Stripe webhook signing secret for/api/subscription/webhook.STRIPE_MODE: Stripe mode selector (testorlive). Recommended to set explicitly in each environment.STRIPE_PLAN_PRICE_MAPPING_TEST: JSON mapping for test mode price IDs.STRIPE_PLAN_PRICE_MAPPING_LIVE: JSON mapping for live mode price IDs.STRIPE_PLAN_PRICE_MAPPING: Optional fallback JSON mapping used when mode-specific variable is not provided.
Required mapping keys validated at startup:
basic.monthlypro.monthly
Example mapping value:
{"basic":{"monthly":"price_123"},"pro":{"monthly":"price_456"}}
Example:
HOST=127.0.0.1 PORT=8080 python start_alwrity_backend.py
CORS Configuration
The backend is configured to allow requests from:
http://localhost:3000(React dev server)http://localhost:8000(Backend dev server)http://localhost:3001(Alternative React port)
🔄 Development Workflow
1. Start Development Server
cd backend
python start_alwrity_backend.py
2. Make Changes
- Edit
app.pyfor API changes - Edit files in
api/for endpoint modifications - Edit files in
services/for business logic changes
3. Auto-reload
The server automatically reloads when you save changes to Python files.
4. Test Changes
- Use the Swagger UI at http://localhost:8000/api/docs
- Or use curl commands for quick testing
🐛 Troubleshooting
Common Issues
1. "Module not found" errors
# Make sure you're in the backend directory
cd backend
# Install dependencies
pip install -r requirements.txt
2. "Port already in use" error
# Use a different port
PORT=8080 python start_alwrity_backend.py
3. "Permission denied" errors
# On Windows, run PowerShell as Administrator
# On Linux/Mac, check file permissions
ls -la
4. CORS errors from frontend
- Make sure the frontend is running on http://localhost:3000
- Check that CORS is properly configured in
app.py
Getting Help
- Check the logs: The startup script provides detailed information
- API Documentation: Use http://localhost:8000/api/docs to test endpoints
- Health Check: Visit http://localhost:8000/health to verify the server is running
🚀 Production Deployment
Using Docker
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
Using Gunicorn (Recommended for production)
# Install gunicorn
pip install gunicorn
# Run with multiple workers
gunicorn app:app -w 4 -k uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000
🔗 Integration with Frontend
This backend is designed to work seamlessly with the React frontend:
- API Client: Frontend uses axios to communicate with these endpoints
- Real-time Updates: Frontend polls status endpoints for live updates
- Error Handling: Comprehensive error responses for frontend handling
- CORS: Configured for cross-origin requests from React
📈 Features
- ✅ Onboarding Progress Tracking: Complete 6-step onboarding flow with persistence
- 🔑 API Key Management: Secure storage and validation of AI provider API keys
- 🔄 Resume Functionality: Users can resume onboarding from where they left off
- ✅ Validation: Comprehensive validation for API keys and step completion
- 🌐 CORS Support: Configured for React frontend integration
- 📚 Auto-generated Documentation: Swagger UI and ReDoc
- 🔍 Health Monitoring: Built-in health check endpoint
🤝 Contributing
When adding new features:
- Add API endpoints in
api/directory - Add business logic in
services/directory - Add data models in
models/directory - Update this README with new information
- Test thoroughly using the Swagger UI
📞 Support
If you encounter issues:
- Check the console output for error messages
- Verify all dependencies are installed
- Test individual endpoints using the Swagger UI
- Check the health endpoint: http://localhost:8000/health
Happy coding! 🎉