Skip to content

Commit

Permalink
完善 aiosqlite 异步数据库驱动
Browse files Browse the repository at this point in the history
  • Loading branch information
lan-air committed Dec 11, 2022
1 parent d25a921 commit e5d18a5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
1 change: 0 additions & 1 deletion database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
26 changes: 10 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand Down Expand Up @@ -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': '口令不存在'}

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e5d18a5

Please sign in to comment.