Created
August 25, 2026 16:16
-
-
Save carefree-ladka/21353681189e68adb1b845c0a0443082 to your computer and use it in GitHub Desktop.
create-fastapi.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| PROJECT_NAME=${1:-fastapi-boilerplate} | |
| echo "🚀 Creating FastAPI project: $PROJECT_NAME" | |
| mkdir -p "$PROJECT_NAME" | |
| cd "$PROJECT_NAME" | |
| # ------------------------ | |
| # Folder Structure | |
| # ------------------------ | |
| mkdir -p app/{api/v1/endpoints,core,db,models,schemas,services,repositories,utils,middleware} | |
| mkdir -p tests | |
| mkdir -p alembic/versions | |
| touch app/__init__.py | |
| touch app/api/__init__.py | |
| touch app/api/v1/__init__.py | |
| touch app/api/v1/endpoints/__init__.py | |
| touch app/core/__init__.py | |
| touch app/db/__init__.py | |
| touch app/models/__init__.py | |
| touch app/schemas/__init__.py | |
| touch app/services/__init__.py | |
| touch app/repositories/__init__.py | |
| touch app/utils/__init__.py | |
| touch app/middleware/__init__.py | |
| # ------------------------ | |
| # .gitignore | |
| # ------------------------ | |
| cat > .gitignore <<EOF | |
| .venv/ | |
| __pycache__/ | |
| .pytest_cache/ | |
| .mypy_cache/ | |
| .coverage | |
| htmlcov/ | |
| .env | |
| *.pyc | |
| .DS_Store | |
| EOF | |
| # ------------------------ | |
| # .env | |
| # ------------------------ | |
| cat > .env <<EOF | |
| APP_NAME=FastAPI Boilerplate | |
| DEBUG=true | |
| DATABASE_URL=postgresql+psycopg://postgres:postgres@localhost:5432/fastapi_db | |
| SECRET_KEY=change_me | |
| ALGORITHM=HS256 | |
| ACCESS_TOKEN_EXPIRE_MINUTES=30 | |
| EOF | |
| # ------------------------ | |
| # pyproject.toml | |
| # ------------------------ | |
| cat > pyproject.toml <<EOF | |
| [project] | |
| name = "$PROJECT_NAME" | |
| version = "0.1.0" | |
| description = "Production FastAPI Boilerplate" | |
| requires-python = ">=3.12" | |
| dependencies = [ | |
| "fastapi", | |
| "uvicorn[standard]", | |
| "sqlalchemy>=2.0", | |
| "psycopg[binary]", | |
| "alembic", | |
| "pydantic>=2.0", | |
| "pydantic-settings", | |
| "python-jose[cryptography]", | |
| "passlib[bcrypt]", | |
| "python-multipart", | |
| ] | |
| [tool.ruff] | |
| line-length = 88 | |
| [tool.pytest.ini_options] | |
| pythonpath = ["."] | |
| EOF | |
| # ------------------------ | |
| # Dockerfile | |
| # ------------------------ | |
| cat > Dockerfile <<EOF | |
| FROM python:3.12-slim | |
| WORKDIR /app | |
| COPY pyproject.toml . | |
| RUN pip install --upgrade pip && pip install . | |
| COPY . . | |
| CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"] | |
| EOF | |
| # ------------------------ | |
| # docker-compose.yml | |
| # ------------------------ | |
| cat > docker-compose.yml <<EOF | |
| version: "3.9" | |
| services: | |
| db: | |
| image: postgres:16 | |
| restart: always | |
| environment: | |
| POSTGRES_DB: fastapi_db | |
| POSTGRES_USER: postgres | |
| POSTGRES_PASSWORD: postgres | |
| ports: | |
| - "5432:5432" | |
| api: | |
| build: . | |
| depends_on: | |
| - db | |
| ports: | |
| - "8000:8000" | |
| env_file: | |
| - .env | |
| EOF | |
| # ------------------------ | |
| # Core Config | |
| # ------------------------ | |
| cat > app/core/config.py <<EOF | |
| from pydantic_settings import BaseSettings, SettingsConfigDict | |
| class Settings(BaseSettings): | |
| APP_NAME: str | |
| DEBUG: bool | |
| DATABASE_URL: str | |
| SECRET_KEY: str | |
| ALGORITHM: str | |
| ACCESS_TOKEN_EXPIRE_MINUTES: int | |
| model_config = SettingsConfigDict(env_file=".env") | |
| settings = Settings() | |
| EOF | |
| # ------------------------ | |
| # Database | |
| # ------------------------ | |
| cat > app/db/session.py <<EOF | |
| from sqlalchemy import create_engine | |
| from sqlalchemy.orm import sessionmaker, DeclarativeBase | |
| from app.core.config import settings | |
| engine = create_engine(settings.DATABASE_URL, echo=settings.DEBUG) | |
| SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False) | |
| class Base(DeclarativeBase): | |
| pass | |
| def get_db(): | |
| db = SessionLocal() | |
| try: | |
| yield db | |
| finally: | |
| db.close() | |
| EOF | |
| # ------------------------ | |
| # Health Endpoint | |
| # ------------------------ | |
| cat > app/api/v1/endpoints/health.py <<EOF | |
| from fastapi import APIRouter | |
| router = APIRouter(prefix="/health", tags=["Health"]) | |
| @router.get("/") | |
| async def health(): | |
| return {"status": "ok"} | |
| EOF | |
| # ------------------------ | |
| # API Router | |
| # ------------------------ | |
| cat > app/api/v1/router.py <<EOF | |
| from fastapi import APIRouter | |
| from app.api.v1.endpoints import health | |
| router = APIRouter(prefix="/api/v1") | |
| router.include_router(health.router) | |
| EOF | |
| # ------------------------ | |
| # Main App | |
| # ------------------------ | |
| cat > app/main.py <<EOF | |
| from fastapi import FastAPI | |
| from app.api.v1.router import router | |
| from app.core.config import settings | |
| app = FastAPI(title=settings.APP_NAME) | |
| app.include_router(router) | |
| @app.get("/") | |
| async def root(): | |
| return {"message": settings.APP_NAME} | |
| EOF | |
| # ------------------------ | |
| # Alembic Config Placeholder | |
| # ------------------------ | |
| cat > alembic.ini <<EOF | |
| [alembic] | |
| script_location = alembic | |
| EOF | |
| cat > alembic/env.py <<EOF | |
| from app.db.session import Base | |
| target_metadata = Base.metadata | |
| EOF | |
| # ------------------------ | |
| # README | |
| # ------------------------ | |
| cat > README.md <<EOF | |
| # FastAPI Boilerplate | |
| ## Setup | |
| \`\`\`bash | |
| python -m venv .venv | |
| source .venv/bin/activate | |
| pip install -e . | |
| uvicorn app.main:app --reload | |
| \`\`\` | |
| Docs: | |
| - Swagger → http://localhost:8000/docs | |
| - Redoc → http://localhost:8000/redoc | |
| EOF | |
| echo "✅ Project created successfully!" | |
| echo "" | |
| echo "Next steps:" | |
| echo "cd $PROJECT_NAME" | |
| echo "python -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