Created
February 19, 2022 17:58
-
-
Save yeus/43b1d3c21cdc2c0eaab1832268eb2ddd to your computer and use it in GitHub Desktop.
Single Page Application Server based on fastapi/starlette.
This file contains hidden or 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
from fastapi import FastAPI | |
from fastapi.responses import FileResponse | |
from fastapi.staticfiles import StaticFiles | |
app = FastAPI() | |
STATIC_FILES_DIR = "./static/" | |
index_file = FileResponse( | |
STATIC_FILES_DIR + "index.html", | |
headers={"Cache-Control": "no-cache"} | |
) | |
print(f"serving file: {index_file.path}") | |
@app.get("/", response_class=FileResponse) | |
async def index(request: Request): | |
return index_file | |
@app.middleware("http") | |
async def add_process_time_header(request: Request, call_next: Response): | |
response = await call_next(request) | |
if response.status_code == 404: | |
return index_file | |
else: | |
return response | |
app.mount("/", StaticFiles(directory=STATIC_FILES_DIR, html=True), name="index") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment