Added cors

This commit is contained in:
Dmitriy Shishkov 2021-07-17 11:16:16 +05:00
parent dcab64c78d
commit a1a4d15e4e
No known key found for this signature in database
GPG Key ID: 14358F96FCDD8060
3 changed files with 21 additions and 6 deletions

View File

@ -57,6 +57,7 @@ dokku git:from-image publitebackend publite/backend:latest
```
# TODO
* Separate epub and fb2 files to python modules
* Rewrite own `.opf` file parsing to get rid of dependency on EbookLib
* Add cli interfaces for epub and fb2 libs
- Separate epub and fb2 files to python modules
- Rewrite own `.opf` file parsing to get rid of dependency on EbookLib
- Add cli interfaces for epub and fb2 libs

View File

@ -8,7 +8,7 @@ from base64 import b64encode
from functools import cache
from tempfile import SpooledTemporaryFile
import aiofiles as aiof
import aiofiles
import ebooklib
from ebooklib import epub
from fastapi import HTTPException
@ -61,7 +61,7 @@ async def epub_to_tokens(
tokens = {}
async with aiof.tempfile.NamedTemporaryFile() as tmp:
async with aiofiles.tempfile.NamedTemporaryFile() as tmp:
await tmp.write(file.read())
# Reading book file

View File

@ -3,12 +3,17 @@
from datetime import datetime
from fastapi import FastAPI, File, HTTPException, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel # pylint: disable=no-name-in-module
from .epub import epub2html
from .fb2 import fb22html
from .utils import HashedHTMLBook, add_hash
origins = (
"*"
)
class DebugInfo(BaseModel): # pylint: disable=too-few-public-methods
"""Main handler return types"""
@ -18,6 +23,14 @@ class DebugInfo(BaseModel): # pylint: disable=too-few-public-methods
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
start_time = datetime.now()
@ -47,7 +60,8 @@ async def create_upload_file(file: UploadFile = File(...)):
elif file.filename.endswith(".epub"):
content = await epub2html(file.file)
else:
raise HTTPException(status_code=415, detail="Error! Unsupported file type")
raise HTTPException(
status_code=415, detail="Error! Unsupported file type")
h_content = add_hash(content)