-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix and examples/add examples for vue and fix faulty ssr (#24)
- Loading branch information
Showing
51 changed files
with
9,950 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import os | ||
from typing import Annotated | ||
from fastapi.templating import Jinja2Templates | ||
from pydantic import BaseModel, EmailStr | ||
|
||
from fastapi import FastAPI, Depends | ||
from fastapi.responses import RedirectResponse | ||
from fastapi.exceptions import RequestValidationError | ||
from starlette.staticfiles import StaticFiles | ||
from starlette.middleware.sessions import SessionMiddleware | ||
from inertia import ( | ||
InertiaResponse, | ||
Inertia, | ||
inertia_dependency_factory, | ||
inertia_version_conflict_exception_handler, | ||
inertia_request_validation_exception_handler, | ||
InertiaVersionConflictException, | ||
InertiaConfig, | ||
lazy, | ||
) | ||
|
||
template_dir = os.path.join(os.path.dirname(__file__), "templates") | ||
templates = Jinja2Templates(directory=template_dir) | ||
|
||
app = FastAPI() | ||
app.add_middleware(SessionMiddleware, secret_key="secret_key") | ||
app.add_exception_handler( | ||
InertiaVersionConflictException, | ||
inertia_version_conflict_exception_handler, # type: ignore[arg-type] | ||
) | ||
app.add_exception_handler( | ||
RequestValidationError, | ||
inertia_request_validation_exception_handler, # type: ignore[arg-type] | ||
) | ||
|
||
manifest_json = os.path.join( | ||
os.path.dirname(__file__), "..", "webapp", "dist", "manifest.json" | ||
) | ||
inertia_config = InertiaConfig( | ||
templates=templates, | ||
manifest_json_path=manifest_json, | ||
environment="development", | ||
use_flash_messages=True, | ||
use_flash_errors=True, | ||
entrypoint_filename="main.ts", | ||
assets_prefix="/src", | ||
) | ||
InertiaDep = Annotated[Inertia, Depends(inertia_dependency_factory(inertia_config))] | ||
|
||
|
||
vue_dir = ( | ||
os.path.join(os.path.dirname(__file__), "..", "webapp", "dist") | ||
if inertia_config.environment != "development" | ||
else os.path.join(os.path.dirname(__file__), "..", "webapp", "src") | ||
) | ||
|
||
app.mount("/src", StaticFiles(directory=vue_dir), name="src") | ||
app.mount( | ||
"/assets", StaticFiles(directory=os.path.join(vue_dir, "assets")), name="assets" | ||
) | ||
|
||
|
||
def some_dependency(inertia: InertiaDep) -> None: | ||
inertia.share(message="hello from dependency") | ||
|
||
|
||
@app.get("/", response_model=None) | ||
async def index(inertia: InertiaDep) -> InertiaResponse: | ||
props = { | ||
"message": "hello from index", | ||
"lazy_prop": lazy(lambda: "hello from lazy prop"), | ||
} | ||
return await inertia.render("IndexPage", props) | ||
|
||
|
||
@app.get("/2", response_model=None) | ||
async def other_page(inertia: InertiaDep) -> RedirectResponse: | ||
inertia.flash("hello from index2 (through flash)", category="message") | ||
return RedirectResponse(url="/3") | ||
|
||
|
||
@app.get("/3", response_model=None, dependencies=[Depends(some_dependency)]) | ||
async def other_page_with_flashed_data(inertia: InertiaDep) -> InertiaResponse: | ||
inertia.flash("hello from index3 (through flash)", category="message") | ||
return await inertia.render("OtherPage") | ||
|
||
|
||
class UserLogin(BaseModel): | ||
email: EmailStr | ||
password: str | ||
|
||
|
||
@app.post("/login", response_model=None) | ||
async def some_form(user: UserLogin, inertia: InertiaDep) -> RedirectResponse: | ||
inertia.flash("form submitted", category="message") | ||
return inertia.back() |
Oops, something went wrong.