feat(api): Add caching
This commit is contained in:
+17
-4
@@ -1,11 +1,19 @@
|
||||
from pathlib import Path
|
||||
from fastapi import FastAPI
|
||||
from fastapi.responses import StreamingResponse
|
||||
from fastapi.responses import FileResponse, StreamingResponse
|
||||
from ebooklib import epub
|
||||
from create_book import retrieve_story, set_cover, set_metadata, add_chapters
|
||||
from create_book import retrieve_story, set_cover, set_metadata, add_chapters, slugify
|
||||
import tempfile
|
||||
from io import BytesIO
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
app = FastAPI()
|
||||
BUILD_PATH = Path(__file__).parent / "build"
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def home():
|
||||
return FileResponse(BUILD_PATH / "index.html")
|
||||
|
||||
|
||||
@app.get("/download/{story_id}")
|
||||
@@ -37,11 +45,16 @@ async def download_book(story_id: int):
|
||||
return StreamingResponse(
|
||||
BytesIO(book_data),
|
||||
media_type="application/epub+zip",
|
||||
headers={"Content-Disposition": f'attachment; filename="book_{story_id}.epub"'},
|
||||
headers={
|
||||
"Content-Disposition": f'attachment; filename="{slugify(data["title"])}_{story_id}.epub"' # Thanks https://stackoverflow.com/a/72729058
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
app.mount("/", StaticFiles(directory=BUILD_PATH), "static")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=80)
|
||||
uvicorn.run(app, host="0.0.0.0", port=8084)
|
||||
|
||||
Reference in New Issue
Block a user