diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..2b9dc52 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,35 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:E:\pyprojects\PsycologyAPI\web_spider\db.sqlite3 + $ProjectFileDir$ + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:E:\pyprojects\PsycologyAPI\db.sqlite3 + $ProjectFileDir$ + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/xerial/sqlite-jdbc/3.45.1.0/sqlite-jdbc-3.45.1.0.jar + + + file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.45.1/org/slf4j/slf4j-api/1.7.36/slf4j-api-1.7.36.jar + + + + + \ No newline at end of file diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..9e7831f --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/webServers.xml b/.idea/webServers.xml new file mode 100644 index 0000000..66aaa77 --- /dev/null +++ b/.idea/webServers.xml @@ -0,0 +1,14 @@ + + + + + + \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index f9ae210..f30f98d 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/main.py b/main.py index 6d7c6d9..7ad2df6 100644 --- a/main.py +++ b/main.py @@ -1,13 +1,72 @@ +from random import randint + +from fastapi import FastAPI +import os +from contextlib import asynccontextmanager +from typing import AsyncGenerator + from fastapi import FastAPI -app = FastAPI() +from tortoise import Tortoise, generate_config +from tortoise.contrib.fastapi import RegisterTortoise + +from models import RawData + + +@asynccontextmanager +async def lifespan_test(app: FastAPI) -> AsyncGenerator[None, None]: + config = generate_config( + "sqlite://:memory:", + app_modules={"models": ["models"]}, + testing=True, + connection_label="models", + ) + async with RegisterTortoise( + app=app, + config=config, + generate_schemas=True, + add_exception_handlers=True, + _create_db=True, + ): + # db connected + yield + # app teardown + # db connections closed + await Tortoise._drop_databases() + + +@asynccontextmanager +async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: + if getattr(app.state, "testing", None): + async with lifespan_test(app) as _: + yield + else: + config = generate_config( + "sqlite:///db.sqlite3", + app_modules={"models": ["models"]}, + connection_label="models", + ) + async with RegisterTortoise( + app=app, + config=config, + generate_schemas=True, + add_exception_handlers=True, + _create_db=True, + ): + # db connected + yield + # app teardown + # db connections closed + await Tortoise.close_connections() + + +app = FastAPI(lifespan=lifespan) @app.get("/") async def root(): - return {"message": "Hello World"} - - -@app.get("/hello/{name}") -async def say_hello(name: str): - return {"message": f"Hello {name}"} + cnt = await RawData.all().count() + # 生成一个范围内的随机整数,随机返回一篇文章 + r = randint(1, cnt) + record = await RawData.get(id=r) + return {"title": record.title, "content": record.content} diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..84d4240 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +beautifulsoup4~=4.12.3 +Crawl4AI~=0.3.731 +requests~=2.32.3 +tortoise-orm~=0.21.7 + +toml~=0.10.2 +httpx~=0.27.2 +langchain~=0.3.7 + +websockets~=13.1 +fastapi~=0.115.3 +uvicorn~=0.20.0