From e5d18a5bf329ab364bff003a2fc29d9ca7ea7776 Mon Sep 17 00:00:00 2001 From: lan-air Date: Sun, 11 Dec 2022 17:41:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20aiosqlite=20=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database.py | 1 - main.py | 26 ++++++++++---------------- requirements.txt | 6 +++++- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/database.py b/database.py index 4275b326..074319b9 100644 --- a/database.py +++ b/database.py @@ -5,7 +5,6 @@ from sqlalchemy.ext.asyncio import create_async_engine from sqlalchemy.ext.asyncio.session import AsyncSession - engine = create_async_engine("sqlite+aiosqlite:///database.db") Base = declarative_base() diff --git a/main.py b/main.py index d1105f0c..d135672f 100644 --- a/main.py +++ b/main.py @@ -3,22 +3,15 @@ import uuid import threading import random - from fastapi import FastAPI, Depends, UploadFile, Form, File from starlette.requests import Request from starlette.responses import HTMLResponse, FileResponse -import random from starlette.staticfiles import StaticFiles -from sqlalchemy import or_, select, update, delete, create_engine -from sqlalchemy import select, update, delete +from sqlalchemy import or_, select, update, delete from sqlalchemy.ext.asyncio.session import AsyncSession -from database import engine, get_session, Base, Codes - - -engine = create_engine('sqlite:///database.db', connect_args={"check_same_thread": False}) -Base.metadata.create_all(bind=engine) +from database import get_session, Codes app = FastAPI() if not os.path.exists('./static'): @@ -137,13 +130,14 @@ def ip_error(ip): @app.get('/select') -async def get_file(code: str, db: Session = Depends(get_db)): - file = db.query(database.Codes).filter(database.Codes.code == code).first() - if file: - if file.type == 'text': - return {'code': code, 'msg': '查询成功', 'data': file.text} +async def get_file(code: str, s: AsyncSession = Depends(get_session)): + query = select(Codes).where(Codes.code == code) + info = (await s.execute(query)).scalars().first() + if info: + if info.type == 'text': + return {'code': code, 'msg': '查询成功', 'data': info.text} else: - return FileResponse('.' + file.text, filename=file.name) + return FileResponse('.' + info.text, filename=info.name) else: return {'code': 404, 'msg': '口令不存在'} @@ -182,7 +176,7 @@ async def share(text: str = Form(default=None), style: str = Form(default='2'), query = select(Codes).where(or_(Codes.exp_time < datetime.datetime.now(), Codes.count == 0)) exps = (await s.execute(query)).scalars().all() threading.Thread(target=delete_file, args=([[{'type': old.type, 'text': old.text}] for old in exps],)).start() - + exps_ids = [exp.id for exp in exps] query = delete(Codes).where(Codes.id.in_(exps_ids)) await s.execute(query) diff --git a/requirements.txt b/requirements.txt index 95c6f01a..1f117ba0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,7 @@ -fastapi[all]==0.88.0 +fastapi==0.88.0 aiosqlite==0.17.0 SQLAlchemy==1.4.44 +python-multipart==0.0.5 +uvicorn==0.15.0 +greenlet==2.0.1 +starlette~=0.22.0 \ No newline at end of file