8.7 KiB
FastAPI ISS Project
A FastAPI application with Docker and docker-compose support.
Features
- FastAPI REST API with CRUD operations
- PostgreSQL database integration
- SQLAlchemy ORM with Alembic migrations
- Docker containerization
- Docker Compose for easy deployment
- Health check endpoints
- CORS middleware enabled
- Auto-generated API documentation
- Instagram post scraping via BoxAPI
- S3 media upload and storage
API Endpoints
GET /- Welcome messageGET /health- Health checkGET /items- Get all itemsGET /items/{item_id}- Get specific itemPOST /items- Create new itemPUT /items/{item_id}- Update itemDELETE /items/{item_id}- Delete itemPOST /scrape/- Scrape Instagram posts (requires API key)
Quick Start with Docker Compose
-
Set up environment variables:
# Copy the example environment file cp env.example .env # Edit the .env file with your secure passwords and BoxAPI credentials # nano .env # or use your preferred editor -
Configure BoxAPI and Redis:
# Add your BoxAPI credentials to .env BOXAPI_USERNAME=your_boxapi_username BOXAPI_PASSWORD=your_boxapi_password BOXAPI_ENDPOINT=https://boxapi.ir/api/instagram/user/get_media_by_username BOXAPI_LOG_DIR=/app/logs/boxapi BOXAPI_LOG_LEVEL=all # or "error" for production # Redis for pagination (optional but recommended) REDIS_URL=redis://localhost:6379/0 -
Build and start the application:
docker-compose up --build -
Access the application:
- API: http://localhost:8000
- Interactive API docs: http://localhost:8000/docs
- ReDoc documentation: http://localhost:8000/redoc
-
Stop the application:
docker-compose down
Development
Running locally without Docker
-
Install dependencies:
pip install -r requirements.txt -
Run the application:
uvicorn main:app --reload --host 0.0.0.0 --port 8000
Docker Commands
Build the image:
docker build -t fastapi-iss .
Run the container:
docker run -p 8000:8000 fastapi-iss
Run in detached mode:
docker run -d -p 8000:8000 --name fastapi-app fastapi-iss
API Examples
Create an item
curl -X POST "http://localhost:8000/items" \
-H "Content-Type: application/json" \
-d '{"name": "Sample Item", "description": "A sample item", "price": 29.99}'
Get all items
curl "http://localhost:8000/items"
Get specific item
curl "http://localhost:8000/items/1"
Update item
curl -X PUT "http://localhost:8000/items/1" \
-H "Content-Type: application/json" \
-d '{"name": "Updated Item", "description": "Updated description", "price": 39.99}'
Delete item
curl -X DELETE "http://localhost:8000/items/1"
Scrape Instagram posts
curl -X POST "http://localhost:8000/scrape/" \
-H "Content-Type: application/json" \
-H "X-API-Key: your_api_key_here" \
-d '{"username": "instagram_username", "seller_id": "your-seller-uuid", "max_count": 5}'
Database Connection
You can connect to the PostgreSQL database directly:
# Using psql (if installed)
psql -h localhost -p ${POSTGRES_PORT} -U ${POSTGRES_USER} -d ${POSTGRES_DB}
# Using Docker
docker exec -it fastapi-iss-postgres-1 psql -U ${POSTGRES_USER} -d ${POSTGRES_DB}
Project Structure
FastApi-ISS/
├── main.py # FastAPI application
├── database.py # Database configuration and models
├── scraper.py # Instagram scraping logic
├── schemas.py # Pydantic schemas
├── requirements.txt # Python dependencies
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose configuration
├── env.example # Example environment variables
├── .env # Environment variables (create from env.example)
├── alembic.ini # Alembic configuration
├── alembic/ # Database migrations
│ ├── env.py # Alembic environment
│ └── script.py.mako # Migration template
├── utils/ # Utility modules
│ ├── __init__.py
│ ├── boxapi_client.py # BoxAPI client for Instagram scraping
│ ├── parser_fast.py # Fast BoxAPI response parser
│ ├── boxapi_parser.py # Legacy BoxAPI response parser
│ ├── boxapi_log.py # BoxAPI response logging
│ ├── seen_store.py # Redis-based seen posts tracking
│ ├── instagram_client.py # Legacy Instagram client
│ └── s3_uploader.py # S3 media upload functionality
├── .dockerignore # Docker ignore file
├── .gitignore # Git ignore file
└── README.md # This file
Environment Variables
The application uses environment variables for configuration. Copy env.example to .env and customize the values:
BoxAPI Configuration (Instagram Scraping)
BOXAPI_USERNAME: Your BoxAPI usernameBOXAPI_PASSWORD: Your BoxAPI passwordBOXAPI_ENDPOINT: BoxAPI endpoint URL (default:https://boxapi.ir/api/instagram/user/get_media_by_username)BOXAPI_LOG_DIR: Directory for BoxAPI response logs (default:/app/logs/boxapi)BOXAPI_LOG_LEVEL: Logging level - "all", "error", or "none" (default: "all")
Redis Configuration (for pagination)
REDIS_URL: Redis connection URL (default:redis://localhost:6379/0)
Database Configuration
POSTGRES_DB: Database name (default:fastapi_db)POSTGRES_USER: Database user (default:fastapi_user)POSTGRES_PASSWORD: Database password (CHANGE THIS)POSTGRES_HOST: Database host (default:postgres)POSTGRES_PORT: Database port (default:5432)DATABASE_URL: Full database connection string (auto-generated from above)
Application Configuration
PYTHONPATH: Python path (default:/app)PYTHONUNBUFFERED: Python output buffering (default:1)FASTAPI_TITLE: Application titleFASTAPI_DESCRIPTION: Application descriptionFASTAPI_VERSION: Application version
Security Configuration
SECRET_KEY: Secret key for JWT tokens (CHANGE THIS)ALGORITHM: JWT algorithm (default:HS256)ACCESS_TOKEN_EXPIRE_MINUTES: Token expiration time
CORS Configuration
ALLOWED_ORIGINS: Allowed origins for CORSALLOWED_CREDENTIALS: Allow credentials in CORSALLOWED_METHODS: Allowed HTTP methodsALLOWED_HEADERS: Allowed HTTP headers
Logging
LOG_LEVEL: Logging level (default:INFO)
Database
The application uses PostgreSQL with the following configuration (configurable via environment variables):
- Database:
fastapi_db(configurable viaPOSTGRES_DB) - User:
fastapi_user(configurable viaPOSTGRES_USER) - Password: Configurable via
POSTGRES_PASSWORD(change this!) - Port:
5432(configurable viaPOSTGRES_PORT)
Database Migrations
To run database migrations:
# Generate a new migration
alembic revision --autogenerate -m "Description of changes"
# Apply migrations
alembic upgrade head
# Rollback migrations
alembic downgrade -1
Health Checks
The application includes health checks that can be accessed at:
- Docker health check:
http://localhost:8000/health - Docker Compose health check: Configured in docker-compose.yml
Security
Environment Variables
- Never commit
.envfiles to version control - Use
env.exampleas a template for your.envfile - Change all default passwords and secret keys
- Use strong, unique passwords for production
Production Deployment
- Change all default credentials
- Use strong secret keys
- Configure proper CORS settings
- Enable HTTPS
- Use environment-specific
.envfiles
Database Security
- Use strong database passwords
- Limit database access to necessary users only
- Regularly update dependencies
- Monitor database logs
Troubleshooting
-
Port already in use:
- Change the port in docker-compose.yml or use a different port
- Kill existing processes using the port
-
Docker build fails:
- Ensure Docker is running
- Clear Docker cache:
docker system prune -a
-
Application not starting:
- Check logs:
docker-compose logs - Verify all files are present in the project directory
- Check logs:
-
Database connection issues:
- Verify
.envfile exists and has correct values - Check if PostgreSQL container is running:
docker-compose ps - Check database logs:
docker-compose logs postgres
- Verify
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Test with Docker Compose
- Submit a pull request
License
This project is open source and available under the MIT License.