From 842c792e6fc67d0da00a65c9f5a36f6ad55b42d3 Mon Sep 17 00:00:00 2001 From: pengtao Date: Tue, 22 Jun 2021 15:40:45 +0800 Subject: [PATCH] =?UTF-8?q?token=E4=BB=8Econfig=E6=88=96redis=E8=8E=B7?= =?UTF-8?q?=E5=BE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.py | 1 + dependencies.py | 14 ++++++++++++-- log.txt | 10 ++++++++++ main.py | 31 ++++++++++++++++++++++--------- 4 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 log.txt diff --git a/config/config.py b/config/config.py index 0e2c699..2bed001 100644 --- a/config/config.py +++ b/config/config.py @@ -10,6 +10,7 @@ class Settings(BaseConfig): mail_server: str = "smtp.exmail.qq.com" # 邮箱server mail_user: str = "ops@kingsome.cn" # 邮箱用户名 mail_pswd: Optional[str] = os.getenv('mail_pswd') # 邮箱密码 + x_token: str = "abc" settings = Settings() diff --git a/dependencies.py b/dependencies.py index 82d02a5..7dec374 100644 --- a/dependencies.py +++ b/dependencies.py @@ -1,8 +1,18 @@ from fastapi import Header, HTTPException +from pydantic.fields import T +from starlette.requests import Request +import pdb +from config.config import settings -async def get_token_header(x_token: str = Header(...)): - if x_token != "fake-super-scret-token": +async def get_token_header(request: Request, x_token: str = Header(...)): + client = request.app.state.redis + dd = await client.get('x_token') + # pdb.set_trace() + #print(dd, settings.x_token, x_token) + if x_token == settings.x_token or x_token == dd: + return True + else: raise HTTPException(status_code=400, detail="X-Token header invalid") diff --git a/log.txt b/log.txt new file mode 100644 index 0000000..f44273f --- /dev/null +++ b/log.txt @@ -0,0 +1,10 @@ +found query:aaa +message to asa +found query:aaa +message to asa +found query:aaa +message to asa +found query:aaa +message to asa +found query:aaa +message to asa diff --git a/main.py b/main.py index a8de30a..1b2b16f 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,12 @@ # uvicorn main:app --host=127.0.0.1 --port=8000 --reload -from fastapi import Depends, FastAPI, BackgroundTasks +from fastapi import Depends, FastAPI, BackgroundTasks, Request from dependencies import get_query_token, get_token_header # from routers import items, users # from internal import admin from ops.common import common from typing import Optional from scripts.common.redis import get_redis_pool +import pdb # import sys # sys.path.append('.') @@ -25,8 +26,14 @@ tags_metadata = [ ] -app = FastAPI(dependencies=[Depends(get_token_header)], - openapi_tags=tags_metadata) +def create_app(): + application = FastAPI(dependencies=[Depends(get_token_header)], + openapi_tags=tags_metadata) + application.include_router(common.router, prefix="/common") + return application + + +app = create_app() @app.on_event("startup") @@ -40,12 +47,14 @@ async def shutdown_event(): await app.state.redis.wait_close() -app.include_router(common.router) - - @app.get("/") -async def root(): - return {"message": "Hello Bigger Applications!"} +async def root(request: Request): + redis_client = request.app.state.redis + keys = await redis_client.get("online_devices") + if keys: + return {"message": "Hello Bigger Applications! {}".format(keys)} + else: + return {"message": "kajsk"} def write_log(message: str) -> True: @@ -62,7 +71,11 @@ def get_query(background_tasks: BackgroundTasks, q: Optional[str] = None): @app.post("/send-notification/{email}") -async def send_notification(email: str, background_tasks: BackgroundTasks, q: str = Depends(get_query)): +async def send_notification(request: Request, email: str, background_tasks: BackgroundTasks, q: str = Depends(get_query)): + # pdb.set_trace() + redis_client = request.app.state.redis + keys = await redis_client.get("online_devices") + # print(keys) message = f"message to {email} \n" background_tasks.add_task(write_log, message) return {"message": "Message sent"}