File size: 962 Bytes
b82e8b8 |
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 |
from fastapi import FastAPI, Request, Response
filenames = ["js/poseTweek.js","js/geometry.js","js/poseAnimator.js", "js/limb.js"]
contents = '\n'.join([f"<script type='text/javascript' src='{x}'></script>" for x in filenames])
app = FastAPI()
@app.middleware("http")
async def insert_js(request: Request, call_next):
path = request.scope['path'] # get the request route
response = await call_next(request)
if path == "/":
response_body = ""
async for chunk in response.body_iterator:
response_body += chunk.decode()
some_javascript = contents
response_body = response_body.replace("</head>", some_javascript + "</head>")
del response.headers["content-length"]
return Response(
content=response_body,
status_code=response.status_code,
headers=dict(response.headers),
media_type=response.media_type
)
return response |