A small but complete FastAPI project featuring:
- 🔐 JWT authentication
- 📝 Posts with 🗨 comments and ❤️ likes
- 📤 File uploads (B2 helper included)
- 📨 Background tasks (email & image generation)
- 🧪 Basic tests and structured logging
-
🚀 App entry / lifecycle
app/main.py—lifespanand exportedapp
-
🧭 Routers
- Posts —
app/routers/routes_posts.py→router - Users —
app/routers/routes_users.py→router - Uploads —
app/routers/routes_upload.py→router
- Posts —
-
🔐 Security
app/security.py—get_current_user,create_access_token,create_confirmation_token,authenticate_user,get_subject_for_token_type
-
🗄 Database
app/database.py—databaseinstance & table schemas
-
🛠 Background tasks / mail / image
app/tasks.py—send_simple_email,generate_and_add_to_post
-
☁️ B2 Cloud helper
app/libs/b2/__init__.py—b2_upload_file
-
🧾 Logging
app/logging_conf.py—configure_logging
-
✅ Tests
app/test/
-
Python 3.10+
-
Dependencies from:
requirements.txt- (optional dev tools)
requirements-dev.txt
# 1) Create & activate a virtual environment (recommended)
python -m venv venv
# Linux/Mac:
source venv/bin/activate
# Windows:
venv\Scripts\activate
# 2) Install dependencies
pip install -r requirements.txt
# (optional for linting/tests/dev tools)
pip install -r requirements-dev.txt
# 3) Create a .env file in the project root (see template below)
# 4) Launch the API (auto-reload in dev)
uvicorn app.main:app --reload# ───── Environment Mode ───────────────────────────────────────────────
ENV_STATE=dev
# ───── Dev Database ──────────────────────────────────────────────────
DEV_DATABASE_URL=sqlite+aiosqlite:///./dev.db
# ───── Mailgun (for emails) ──────────────────────────────────────────
DEV_MAILGUN_API_KEY=YOUR_MAILGUN_API_KEY_HERE
DEV_MAILGUN_DOMAIN=YOUR_MAILGUN_DOMAIN_HERE
# ───── Sentry (optional monitoring) ──────────────────────────────────
DEV_SENTRY_DNS=YOUR_SENTRY_DSN_HERE
# ───── Backblaze B2 Storage (for uploads) ────────────────────────────
B2_KEY_ID=YOUR_B2_KEY_ID
B2_APPLICATION_KEY=YOUR_B2_APPLICATION_KEY
B2_BUCKET_NAME=YOUR_B2_BUCKET_NAME
# ───── External API (e.g., DeepAI for images) ────────────────────────
DEEPAI_API_KEY=YOUR_DEEPAI_API_KEY
# ───── Test Database ─────────────────────────────────────────────────
TEST_DATABASE_URL=sqlite+aiosqlite:///./test.db
# ───── Production Database (example) ─────────────────────────────────
PROD_DATABASE_URL=postgresql+asyncpg://USER:PASSWORD@HOST/DB_NAME
# ───── JWT Secret (Dev) ─────────────────────────────────────────────
DEV_SECRET_KEY=CHANGE_ME_TO_A_RANDOM_LONG_SECRET# Assuming pytest is in requirements-dev.txt
pytest -q- Keep real secrets out of
README.mdand version control. - Rotate API keys periodically.
- Use HTTPS and proper CORS settings in production.
- Prefer a production-grade server (e.g.,
gunicornwithuvicorn.workers.UvicornWorker) behind a reverse proxy.
- Configure
ENV_STATE=prodand point toPROD_DATABASE_URL. - Set real secrets via environment variables (or a secret manager).
- Use a process manager (e.g., systemd, Supervisor) or containers (Docker).
- Open an issue describing the change
- Submit a PR with a clear title/description
- Include tests or example calls if applicable