2023-11-22 17:13:16 +08:00
|
|
|
from fastapi import FastAPI, Response, Request
|
|
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from fastapi.templating import Jinja2Templates
|
|
|
|
|
2024-01-26 01:52:03 +08:00
|
|
|
from core import get_settings
|
2022-01-19 08:40:02 +08:00
|
|
|
|
2024-01-26 01:52:03 +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
|
|
|
|
2023-11-22 17:13:16 +08:00
|
|
|
def setup_webapp(app: FastAPI, serve_webapp: bool):
|
2023-04-08 14:48:01 +08:00
|
|
|
backends = "/backends" if serve_webapp else "/"
|
2023-11-22 17:13:16 +08:00
|
|
|
templates = Jinja2Templates(directory=STATIC_DIR)
|
|
|
|
|
|
|
|
if serve_webapp:
|
2023-12-30 15:51:11 +08:00
|
|
|
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)
|
|
|
|
async def handler(request: Request):
|
2023-12-30 15:51:11 +08:00
|
|
|
return templates.TemplateResponse(
|
|
|
|
"index.html", context={"request": request}
|
|
|
|
)
|
2023-11-11 00:48:39 +08:00
|
|
|
|
|
|
|
@app.get(backends)
|
|
|
|
def api_gql_view(request):
|
2023-11-22 17:13:16 +08:00
|
|
|
return Response(
|
2023-11-11 00:48:39 +08:00
|
|
|
"""
|
|
|
|
<!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>Felicity GraphQL</button></a>
|
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
)
|