File size: 5,734 Bytes
ee186bb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | """Asana offline clone — WebsiteBench candidate application.
Route hub only; business logic lives in ``asana_app``. All state is local:
site-bound SQLite through the generated ``websitebench.site_backend`` seam and
LocalAuthStore. No runtime request leaves the local origin.
"""
import json
import os
from pathlib import Path
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from asana_app import pages
from asana_app.api import _cookie_name, public_project_snapshot, router as api_router
from asana_app.services import SERVICES
SITE_ID = "asana"
DISPLAY_NAME = "Asana"
CLONE_ROOT = Path(__file__).resolve().parent
app = FastAPI(title=DISPLAY_NAME, docs_url=None, redoc_url=None, openapi_url=None)
app.include_router(api_router)
app.mount("/static", StaticFiles(directory=CLONE_ROOT / "static"), name="static")
@app.middleware("http")
async def validate_api_json(request: Request, call_next):
"""Reject malformed or non-object JSON before business handlers run."""
content_type = request.headers.get("content-type", "").lower()
if (request.url.path.startswith("/api/") and
request.method in {"POST", "PUT", "PATCH"} and
content_type.startswith("application/json")):
try:
body = await request.json()
except (json.JSONDecodeError, UnicodeDecodeError):
return JSONResponse(
{"error": {"code": "invalid-json",
"message": "Request body must be valid JSON."}},
status_code=400,
)
if not isinstance(body, dict):
return JSONResponse(
{"error": {"code": "invalid-json",
"message": "Request body must be a JSON object."}},
status_code=400,
)
return await call_next(request)
@app.get("/healthz")
def healthz() -> JSONResponse:
return JSONResponse(
{"ok": True, "site_id": SITE_ID},
headers={
"X-WebsiteBench-Container-Build-ID": os.environ.get(
"DEPLOYMENT_BUILD_ID", os.environ.get("SOURCE_REF", "")
)
},
)
_MARKETING = {
"/": pages.home_page,
"/pricing": pages.pricing_page,
"/product": pages.product_page,
"/resources": pages.resources_page,
"/templates": pages.templates_page,
}
def _register_marketing(path: str, render) -> None:
@app.get(path, response_class=HTMLResponse)
def marketing_page() -> str: # pragma: no cover - thin wrapper
return render()
for _path, _render in _MARKETING.items():
_register_marketing(_path, _render)
def _register_nav_detail(path: str) -> None:
@app.get(path, response_class=HTMLResponse)
def nav_detail_page() -> str: # pragma: no cover - thin wrapper
return pages.nav_detail_page(path)
for _path in pages.NAV_DETAIL_PAGES:
_register_nav_detail(_path)
@app.get("/templates/{category}", response_class=HTMLResponse)
def template_category(category: str) -> str:
"""Keep every observed public template-card destination locally reachable."""
return pages.templates_page()
@app.get("/resources/category/{category}", response_class=HTMLResponse)
def resource_category(category: str) -> str:
"""Keep every observed resource-card destination locally reachable."""
return pages.resources_page()
@app.get("/demo/main", response_class=HTMLResponse)
def public_demo() -> str:
return pages.resources_page()
@app.get("/terms/{document}", response_class=HTMLResponse)
def public_terms(document: str) -> str:
return pages.templates_page()
@app.get("/solutions", response_class=HTMLResponse)
def solutions() -> str:
return pages.solutions_page()
def _authenticated(request: Request) -> bool:
token = request.cookies.get(_cookie_name(request, SERVICES))
session = SERVICES.auth.resolve_session(token)
return bool(session and session.get("authenticated"))
@app.get("/-/login", response_class=HTMLResponse)
def login(request: Request):
if _authenticated(request):
return RedirectResponse("/app/home", status_code=302)
return pages.login_page()
@app.get("/create-account", response_class=HTMLResponse)
def create_account(request: Request, email: str = ""):
if _authenticated(request):
return RedirectResponse("/app/home", status_code=302)
return pages.signup_page(email)
@app.get("/-/forgot_password", response_class=HTMLResponse)
def forgot_password() -> str:
return pages.forgot_page()
# Dash-free aliases for tooling that cannot pass "-/" routes as arguments
# (OpenCLI adapters treat a leading dash as an option). Same content.
@app.get("/login", response_class=HTMLResponse)
def login_alias(request: Request):
return login(request)
@app.get("/forgot_password", response_class=HTMLResponse)
def forgot_alias() -> str:
return pages.forgot_page()
@app.get("/share/{project_id}", response_class=HTMLResponse)
def shared_project(project_id: str):
"""Anonymous read-only surface enabled only by a project's public-link mode."""
with SERVICES.auth.connect() as connection:
snapshot = public_project_snapshot(connection, project_id)
if snapshot is None:
return HTMLResponse(pages.not_found_page(), status_code=404)
return pages.shared_project_page(snapshot)
@app.get("/app", response_class=HTMLResponse)
@app.get("/app/{rest:path}", response_class=HTMLResponse)
def app_pages(request: Request, rest: str = ""):
if not _authenticated(request):
target = request.url.path
return RedirectResponse(f"/-/login?u={target}", status_code=302)
return pages.app_shell()
|