felicity-lims/felicity/views/__init__.py

48 lines
1.6 KiB
Python
Raw Permalink Normal View History

2024-01-28 21:17:16 +08:00
import graphdoc
2024-09-28 15:29:22 +08:00
from strawberry import Schema
2024-01-28 21:17:16 +08:00
from fastapi import FastAPI, Request
2023-11-22 17:13:16 +08:00
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from felicity.core import get_settings
2022-01-19 08:40:02 +08:00
settings = get_settings()
2023-11-11 00:48:39 +08:00
STATIC_DIR = f"{settings.BASE_DIR}/templates/static/"
2022-01-19 08:40:02 +08:00
2023-11-11 00:48:39 +08:00
2024-09-28 15:29:22 +08:00
def setup_webapp(app: FastAPI, serve_webapp: bool, schema: Schema) -> None:
backends: str = "/backends" if serve_webapp else "/"
2023-11-22 17:13:16 +08:00
if serve_webapp:
app.mount(
"/assets", StaticFiles(directory=f"{STATIC_DIR}assets"), name="assets"
)
2023-11-11 00:48:39 +08:00
2023-11-22 17:13:16 +08:00
@app.get("/", response_class=HTMLResponse)
2024-09-28 15:29:22 +08:00
async def handler(request: Request) -> HTMLResponse:
2024-01-28 21:17:16 +08:00
return Jinja2Templates(directory=STATIC_DIR).TemplateResponse(
"index.html", context={"request": request}
)
2023-11-11 00:48:39 +08:00
2024-01-28 21:17:16 +08:00
@app.get("/graphql-docs", response_class=HTMLResponse)
2024-09-28 15:29:22 +08:00
async def graphql_docs() -> str:
2024-01-28 21:17:16 +08:00
return graphdoc.to_doc(schema)
@app.get(backends, response_class=HTMLResponse)
2024-09-28 15:29:22 +08:00
def api_gql_view() -> str:
2024-01-28 21:17:16 +08:00
return """
<!Doctype html>
<html>
<body>
<h1>SecureAPI</h1>
<div class="btn-group">
<a href="/docs"><button>SwaggerUI</button></a>
<a href="/redoc"><button>Redoc</button></a>
<a href="/felicity-gql"><button>GraphQL Playground</button></a>
<a href="/graphql-docs"><button>GraphQL Documentation</button></a>
</div>
</body>
</html>
"""