Properly implement lifespan
This commit is contained in:
parent
861aaeb762
commit
71b1a0ab36
1 changed files with 20 additions and 18 deletions
38
server.py
38
server.py
|
@ -19,6 +19,25 @@ from rich.logging import RichHandler
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.asynccontextmanager
|
||||||
|
async def startup(_):
|
||||||
|
if not CACHE_DIR.exists():
|
||||||
|
CACHE_DIR.mkdir(parents=True)
|
||||||
|
with sqlite3.connect(CACHE_FILE) as conn:
|
||||||
|
conn.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE IF NOT EXISTS cache (
|
||||||
|
uuid TEXT PRIMARY KEY,
|
||||||
|
url TEXT NOT NULL,
|
||||||
|
ts INTEGER NOT NULL,
|
||||||
|
metadata TEXT NOT NULL
|
||||||
|
)
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
level=logging.getLevelName(os.environ.get("LOG_LEVEL", "INFO").upper()),
|
level=logging.getLevelName(os.environ.get("LOG_LEVEL", "INFO").upper()),
|
||||||
format="%(message)s",
|
format="%(message)s",
|
||||||
|
@ -27,6 +46,7 @@ logging.basicConfig(
|
||||||
)
|
)
|
||||||
app = fastapi.FastAPI(
|
app = fastapi.FastAPI(
|
||||||
root_path=os.environ.get("PREVIEW_ROOT_PATH", ""),
|
root_path=os.environ.get("PREVIEW_ROOT_PATH", ""),
|
||||||
|
lifespan=startup
|
||||||
)
|
)
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
|
@ -127,24 +147,6 @@ CACHE_FILE.touch(exist_ok=True)
|
||||||
logging.debug("Cache file: %r", CACHE_FILE)
|
logging.debug("Cache file: %r", CACHE_FILE)
|
||||||
|
|
||||||
|
|
||||||
@contextlib.asynccontextmanager
|
|
||||||
async def startup():
|
|
||||||
if not CACHE_DIR.exists():
|
|
||||||
CACHE_DIR.mkdir(parents=True)
|
|
||||||
with sqlite3.connect(CACHE_FILE) as conn:
|
|
||||||
conn.execute(
|
|
||||||
"""
|
|
||||||
CREATE TABLE IF NOT EXISTS cache (
|
|
||||||
uuid TEXT PRIMARY KEY,
|
|
||||||
url TEXT NOT NULL,
|
|
||||||
ts INTEGER NOT NULL,
|
|
||||||
metadata TEXT NOT NULL
|
|
||||||
)
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
yield
|
|
||||||
|
|
||||||
|
|
||||||
def upload_media(domain: str, access_token: str, file: io.BytesIO, filename: str, content_type: str):
|
def upload_media(domain: str, access_token: str, file: io.BytesIO, filename: str, content_type: str):
|
||||||
file.seek(0)
|
file.seek(0)
|
||||||
logging.info(
|
logging.info(
|
||||||
|
|
Loading…
Reference in a new issue