This commit is contained in:
wuaho 2021-05-12 10:35:02 +08:00
parent 224d9816fa
commit baa8635a17
13 changed files with 186 additions and 19 deletions

View File

@ -6,6 +6,7 @@ from .endpoints import space
from .endpoints import dashboard from .endpoints import dashboard
from .endpoints import report from .endpoints import report
from .endpoints import authority from .endpoints import authority
from .endpoints import table_struct
api_router = APIRouter() 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(report.router, tags=["报表接口"], prefix='/report')
api_router.include_router(authority.router, tags=["权限管理接口"], prefix='/authority') api_router.include_router(authority.router, tags=["权限管理接口"], prefix='/authority')
api_router.include_router(table_struct.router, tags=["表结构"], prefix='/table_struct')

View File

@ -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))

View File

@ -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))

35
ck_test.py Normal file
View File

@ -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()]))

View File

@ -28,8 +28,25 @@ class Settings(BaseSettings):
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8 ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
SECRET_KEY: str = 'ZaFX6EypK6PtuhGv11q4DLRvAb0csiLx4dbKUwLwCe8' 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: class Config:
case_sensitive = True case_sensitive = True
settings = Settings() class Debug(Settings):
REDIS_CONF = {
'address': ('139.159.159.3', 6378),
'password': 'd1Gh*zp5',
'db': 1
}
settings = Debug()

12
db/ckdb.py Normal file
View File

@ -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

14
db/ckdb_utils.py Normal file
View File

@ -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()

12
db/redisdb.py Normal file
View File

@ -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

14
db/redisdb_utils.py Normal file
View File

@ -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()

32
main.py
View File

@ -1,33 +1,27 @@
import base64
import binascii import binascii
import uvicorn import uvicorn
from fastapi import FastAPI 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.middleware.cors import CORSMiddleware
from starlette.authentication import AuthenticationBackend, AuthenticationError, AuthCredentials, BaseUser, SimpleUser from starlette.authentication import AuthenticationBackend, AuthenticationError, AuthCredentials, BaseUser, SimpleUser
from starlette.middleware.authentication import AuthenticationMiddleware from starlette.middleware.authentication import AuthenticationMiddleware
from fastapi_authz import CasbinMiddleware from fastapi_authz import CasbinMiddleware
from db import connect_to_mongo, close_mongo_connection, get_database 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 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 = 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_mongo)
app.add_event_handler("startup", connect_to_redis)
app.add_event_handler("shutdown", close_mongo_connection) app.add_event_handler("shutdown", close_mongo_connection)
app.add_event_handler("shutdown", close_redis_connection)
class CurrentUser(BaseUser): class CurrentUser(BaseUser):
@ -67,9 +61,13 @@ class BasicAuth(AuthenticationBackend):
app.add_middleware(CasbinMiddleware, enforcer=casbin_enforcer) app.add_middleware(CasbinMiddleware, enforcer=casbin_enforcer)
app.add_middleware(AuthenticationMiddleware, backend=BasicAuth()) app.add_middleware(AuthenticationMiddleware, backend=BasicAuth())
from api.api_v1.api import api_router app.add_middleware(
app.include_router(api_router, prefix=settings.API_V1_STR) CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
if __name__ == '__main__': 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)

View File

@ -5,4 +5,5 @@ from .folder import *
from .space import * from .space import *
from .dashboard import * from .dashboard import *
from .report import * from .report import *
from .authotity import * from .authotity import *
from .table_struct import *

View File

@ -14,6 +14,7 @@ class ProjectBase(BaseModel):
# 解析请求json 创建项目 # 解析请求json 创建项目
class ProjectCreate(ProjectBase): class ProjectCreate(ProjectBase):
name: str = Field(..., title='项目名') name: str = Field(..., title='项目名')
game: str = Field(..., title='游戏代号')
# 查询某个项目看板 # 查询某个项目看板
@ -25,6 +26,7 @@ class ProjectKanban(DBBase):
# 数据库模型 # 数据库模型
class ProjectDB(DBBase): class ProjectDB(DBBase):
name: str name: str
game: str
user_id: str user_id: str
members: List[str] = [] members: List[str] = []
create_date: datetime = datetime.now() create_date: datetime = datetime.now()

13
schemas/table_struct.py Normal file
View File

@ -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