Skip to content

Instantly share code, notes, and snippets.

@carefree-ladka
Created August 25, 2026 16:15
Show Gist options
  • Select an option

  • Save carefree-ladka/31f558518b4a8300f31a74e2050e4ba2 to your computer and use it in GitHub Desktop.

Select an option

Save carefree-ladka/31f558518b4a8300f31a74e2050e4ba2 to your computer and use it in GitHub Desktop.
Fastapi Unix Boilerplate
#!/usr/bin/env bash
set -e
PROJECT_NAME=${1:-fastapi-playground}
echo "⚡ Creating FastAPI playground: $PROJECT_NAME"
mkdir -p "$PROJECT_NAME/app"
cd "$PROJECT_NAME"
# ------------------------
# Basic structure
# ------------------------
touch app/__init__.py
# ------------------------
# .gitignore
# ------------------------
cat > .gitignore <<EOF
.venv/
__pycache__/
.pytest_cache/
*.pyc
.DS_Store
EOF
# ------------------------
# pyproject.toml
# ------------------------
cat > pyproject.toml <<EOF
[project]
name = "$PROJECT_NAME"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"fastapi",
"uvicorn[standard]",
"pydantic>=2.0",
]
[tool.ruff]
line-length = 88
EOF
# ------------------------
# main.py
# ------------------------
cat > app/main.py <<'EOF'
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="FastAPI Playground")
books = [
{"id": 1, "title": "Atomic Habits", "author": "James Clear"},
{"id": 2, "title": "Clean Code", "author": "Robert C. Martin"},
]
class BookCreate(BaseModel):
title: str
author: str
@app.get("/")
async def root():
return {"message": "FastAPI Playground 🚀"}
@app.get("/health")
async def health():
return {"status": "ok"}
@app.get("/books")
async def get_books():
return books
@app.get("/books/{book_id}")
async def get_book(book_id: int):
return next((b for b in books if b["id"] == book_id), None)
@app.post("/books")
async def create_book(book: BookCreate):
new_book = {
"id": len(books) + 1,
**book.model_dump()
}
books.append(new_book)
return new_book
EOF
# ------------------------
# README
# ------------------------
cat > README.md <<EOF
# FastAPI Playground
## Run
\`\`\`bash
python3 -m venv .venv
source .venv/bin/activate
pip install -e .
uvicorn app.main:app --reload
\`\`\`
Swagger Docs: http://127.0.0.1:8000/docs
EOF
echo "✅ Playground ready!"
echo ""
echo "Next:"
echo "cd $PROJECT_NAME"
echo "python3 -m venv .venv"
echo "source .venv/bin/activate"
echo "pip install -e ."
echo "uvicorn app.main:app --reload"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment