diff --git a/api/api_v1/api.py b/api/api_v1/api.py index 6535a67..d783838 100644 --- a/api/api_v1/api.py +++ b/api/api_v1/api.py @@ -6,6 +6,7 @@ from .endpoints import space from .endpoints import dashboard from .endpoints import report from .endpoints import authority +from .endpoints import table_struct api_router = APIRouter() @@ -17,3 +18,5 @@ api_router.include_router(dashboard.router, tags=["看板接口"], prefix='/dash api_router.include_router(report.router, tags=["报表接口"], prefix='/report') api_router.include_router(authority.router, tags=["权限管理接口"], prefix='/authority') + +api_router.include_router(table_struct.router, tags=["表结构"], prefix='/table_struct') diff --git a/api/api_v1/endpoints/query.py b/api/api_v1/endpoints/query.py new file mode 100644 index 0000000..b1b3da1 --- /dev/null +++ b/api/api_v1/endpoints/query.py @@ -0,0 +1,23 @@ +import json + +from aioredis import Redis +from fastapi import APIRouter, Depends, Request +import crud, schemas + +from api import deps +from db.redisdb import get_redis_pool + +router = APIRouter() + + +@router.post("/sql") +async def query_sql( + request: Request, + data_in: schemas.GetTable, + rdb: Redis = Depends(get_redis_pool), + current_user: schemas.UserDB = Depends(deps.get_current_user) +) -> schemas.Msg: + """原 sql 查询 """ + data = await rdb.get(f'{data_in.game}_{data_in.name}') + return schemas.Msg(code=0, msg='ok', data=json.loads(data)) + diff --git a/api/api_v1/endpoints/table_struct.py b/api/api_v1/endpoints/table_struct.py new file mode 100644 index 0000000..23c0fa8 --- /dev/null +++ b/api/api_v1/endpoints/table_struct.py @@ -0,0 +1,23 @@ +import json + +from aioredis import Redis +from fastapi import APIRouter, Depends, Request +import crud, schemas + +from api import deps +from db.redisdb import get_redis_pool + +router = APIRouter() + + +@router.post("/event") +async def read_event( + request: Request, + data_in: schemas.GetTable, + rdb: Redis = Depends(get_redis_pool), + current_user: schemas.UserDB = Depends(deps.get_current_user) +) -> schemas.Msg: + """获取时间表结构""" + data = await rdb.get(f'{data_in.game}_{data_in.name}') + return schemas.Msg(code=0, msg='ok', data=json.loads(data)) + diff --git a/ck_test.py b/ck_test.py new file mode 100644 index 0000000..5204e17 --- /dev/null +++ b/ck_test.py @@ -0,0 +1,35 @@ +from datetime import datetime + +import asyncio +from aioch import Client + +from core.config import settings + + +async def exec_progress(): + client = Client(**settings.CK_CONFIG) + + progress = await client.execute_with_progress('LONG AND COMPLICATED QUERY') + timeout = 20 + started_at = datetime.now() + + async for num_rows, total_rows in progress: + done = num_rows / total_rows if total_rows else total_rows + now = datetime.now() + # Cancel query if it takes more than 20 seconds to process 50% of rows. + if (now - started_at).total_seconds() > timeout and done < 0.5: + await client.cancel() + break + else: + rv = await progress.get_result() + print(rv) + + +async def exec_no_progress(): + client = Client('localhost') + rv = await client.execute('LONG AND COMPLICATED QUERY') + print(rv) + + +loop = asyncio.get_event_loop() +loop.run_until_complete(asyncio.wait([exec_progress(), exec_no_progress()])) diff --git a/core/config.py b/core/config.py index c9d4204..96f124f 100644 --- a/core/config.py +++ b/core/config.py @@ -28,8 +28,25 @@ class Settings(BaseSettings): ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 SECRET_KEY: str = 'ZaFX6EypK6PtuhGv11q4DLRvAb0csiLx4dbKUwLwCe8' + REDIS_CONF = { + 'address': ('192.168.0.161', 6379), + 'password': 'd1Gh*zp5', + 'db': 1, # 存表结构 + } + + CK_CONFIG = {'host': '119.29.176.224', + 'send_receive_timeout': 30} + class Config: case_sensitive = True -settings = Settings() +class Debug(Settings): + REDIS_CONF = { + 'address': ('139.159.159.3', 6378), + 'password': 'd1Gh*zp5', + 'db': 1 + } + + +settings = Debug() diff --git a/db/ckdb.py b/db/ckdb.py new file mode 100644 index 0000000..21d92a2 --- /dev/null +++ b/db/ckdb.py @@ -0,0 +1,12 @@ +from aioredis import create_redis_pool, Redis + + +class RedisBase: + client: Redis = None + + +rdb = RedisBase() + + +def get_redis_pool() -> Redis: + return rdb.client diff --git a/db/ckdb_utils.py b/db/ckdb_utils.py new file mode 100644 index 0000000..6ad1237 --- /dev/null +++ b/db/ckdb_utils.py @@ -0,0 +1,14 @@ +import aioredis +from aioredis import create_redis_pool, Redis + +from core.config import settings +from .redisdb import rdb + + +async def connect_to_redis(): + rdb.client = await create_redis_pool(**settings.REDIS_CONF) + + +async def close_redis_connection(): + rdb.client.close() + await rdb.client.wait_closed() diff --git a/db/redisdb.py b/db/redisdb.py new file mode 100644 index 0000000..21d92a2 --- /dev/null +++ b/db/redisdb.py @@ -0,0 +1,12 @@ +from aioredis import create_redis_pool, Redis + + +class RedisBase: + client: Redis = None + + +rdb = RedisBase() + + +def get_redis_pool() -> Redis: + return rdb.client diff --git a/db/redisdb_utils.py b/db/redisdb_utils.py new file mode 100644 index 0000000..6ad1237 --- /dev/null +++ b/db/redisdb_utils.py @@ -0,0 +1,14 @@ +import aioredis +from aioredis import create_redis_pool, Redis + +from core.config import settings +from .redisdb import rdb + + +async def connect_to_redis(): + rdb.client = await create_redis_pool(**settings.REDIS_CONF) + + +async def close_redis_connection(): + rdb.client.close() + await rdb.client.wait_closed() diff --git a/main.py b/main.py index 9b0537c..0594e63 100644 --- a/main.py +++ b/main.py @@ -1,33 +1,27 @@ -import base64 import binascii import uvicorn from fastapi import FastAPI -import casbin - -from api.deps import get_current_user2 -from core.config import settings from starlette.middleware.cors import CORSMiddleware from starlette.authentication import AuthenticationBackend, AuthenticationError, AuthCredentials, BaseUser, SimpleUser from starlette.middleware.authentication import AuthenticationMiddleware from fastapi_authz import CasbinMiddleware from db import connect_to_mongo, close_mongo_connection, get_database +from db.redisdb_utils import connect_to_redis, close_redis_connection from utils import * +from api.api_v1.api import api_router +from core.config import settings +from api.deps import get_current_user2 app = FastAPI(title=settings.PROJECT_NAME) +app.include_router(api_router, prefix=settings.API_V1_STR) -if settings.BACKEND_CORS_ORIGINS: - app.add_middleware( - CORSMiddleware, - allow_origins=['*'], - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], - ) app.add_event_handler("startup", connect_to_mongo) +app.add_event_handler("startup", connect_to_redis) app.add_event_handler("shutdown", close_mongo_connection) +app.add_event_handler("shutdown", close_redis_connection) class CurrentUser(BaseUser): @@ -67,9 +61,13 @@ class BasicAuth(AuthenticationBackend): app.add_middleware(CasbinMiddleware, enforcer=casbin_enforcer) app.add_middleware(AuthenticationMiddleware, backend=BasicAuth()) -from api.api_v1.api import api_router -app.include_router(api_router, prefix=settings.API_V1_STR) - +app.add_middleware( + CORSMiddleware, + allow_origins=['*'], + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) if __name__ == '__main__': - uvicorn.run(app='main2:app', host="0.0.0.0", port=8889, reload=True, debug=True) + uvicorn.run(app='main:app', host="0.0.0.0", port=8889, reload=True, debug=True) diff --git a/schemas/__init__.py b/schemas/__init__.py index e4dcc3f..5d30729 100644 --- a/schemas/__init__.py +++ b/schemas/__init__.py @@ -5,4 +5,5 @@ from .folder import * from .space import * from .dashboard import * from .report import * -from .authotity import * \ No newline at end of file +from .authotity import * +from .table_struct import * \ No newline at end of file diff --git a/schemas/project.py b/schemas/project.py index f38a144..069b658 100644 --- a/schemas/project.py +++ b/schemas/project.py @@ -14,6 +14,7 @@ class ProjectBase(BaseModel): # 解析请求json 创建项目 class ProjectCreate(ProjectBase): name: str = Field(..., title='项目名') + game: str = Field(..., title='游戏代号') # 查询某个项目看板 @@ -25,6 +26,7 @@ class ProjectKanban(DBBase): # 数据库模型 class ProjectDB(DBBase): name: str + game: str user_id: str members: List[str] = [] create_date: datetime = datetime.now() diff --git a/schemas/table_struct.py b/schemas/table_struct.py new file mode 100644 index 0000000..963aef5 --- /dev/null +++ b/schemas/table_struct.py @@ -0,0 +1,13 @@ +from enum import Enum + +from pydantic import BaseModel + + +class TableEnum(str, Enum): + event = 'event' + user = 'user' + + +class GetTable(BaseModel): + game: str + name: TableEnum