~nora/fastapi-sandbox

198cfe012ceff0017868fa0c7c984c6b29c0e5f8 — nora 8 months ago 96b1a0c
Move app settings in config.py
4 files changed, 18 insertions(+), 9 deletions(-)

M README.md
M app/config.py
M app/items/routers.py
M app/main.py
M README.md => README.md +1 -0
@@ 15,6 15,7 @@ Create configuration file in `app/.env`.
Run server (webpages reloaded on change).

    uvicorn app.main:app --reload
    uvicorn app.main:app --reload --reload-include *.html

### Testing


M app/config.py => app/config.py +13 -1
@@ 1,11 1,23 @@
from functools import lru_cache
from typing import Any

from pydantic import EmailStr
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
    app_name: str = "fastapi-sandbox"
    app_properties: dict[str, Any] = {
        "title": "fastapi-sandbox",
        "description": "A sandbox to play with *FastAPI* framework.",
        "version": "0.0.1",
        "contact": {"name": "nora", "url": "https://nora.nckm.eu", "email": "nora@nckm.eu"},
        "license_info": {"name": "MIT", "url": "https://opensource.org/licenses/MIT"},
        "openapi_tags": [{"name": "items", "description": "Manage *items*."}],
        # "default_response_class": HTMLResponse
    }

    static_dir: str = "app/static"

    admin_email: EmailStr = "admin@example.com"
    database_url: str = "sqlite:///db.sqlite3"


M app/items/routers.py => app/items/routers.py +1 -0
@@ 7,6 7,7 @@ from fastapi.templating import Jinja2Templates

from .schemas import Item

# todo use jinja2-fragments Jinja2Blocks
templates = Jinja2Templates(directory="app/items/templates")

router = APIRouter(

M app/main.py => app/main.py +3 -8
@@ 1,21 1,16 @@
from fastapi import FastAPI, BackgroundTasks
from fastapi.staticfiles import StaticFiles

from .config import settings
from .items.routers import router as items_router
from .users.routers import router as users_router

app = FastAPI(
    title="fastapi-sandbox",
    description="A sandbox to play with *FastAPI* framework.",
    contact={"name": "nora", "url": "https://nora.nckm.eu", "email": "nora@nckm.eu"},
    license_info={"name": "MIT", "url": "https://opensource.org/licenses/MIT"},
    openapi_tags=[{"name": "items", "description": "Manage *items*."}]
)
app = FastAPI(**settings.app_properties)

app.include_router(items_router)
app.include_router(users_router)

app.mount("/static", StaticFiles(directory="app/static"), name="static")
app.mount("/static", StaticFiles(directory=settings.static_dir), name="static")


@app.get("/")