token从config或redis获得
This commit is contained in:
parent
7712294cda
commit
842c792e6f
@ -10,6 +10,7 @@ class Settings(BaseConfig):
|
|||||||
mail_server: str = "smtp.exmail.qq.com" # 邮箱server
|
mail_server: str = "smtp.exmail.qq.com" # 邮箱server
|
||||||
mail_user: str = "ops@kingsome.cn" # 邮箱用户名
|
mail_user: str = "ops@kingsome.cn" # 邮箱用户名
|
||||||
mail_pswd: Optional[str] = os.getenv('mail_pswd') # 邮箱密码
|
mail_pswd: Optional[str] = os.getenv('mail_pswd') # 邮箱密码
|
||||||
|
x_token: str = "abc"
|
||||||
|
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
|
@ -1,8 +1,18 @@
|
|||||||
from fastapi import Header, HTTPException
|
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(...)):
|
async def get_token_header(request: Request, x_token: str = Header(...)):
|
||||||
if x_token != "fake-super-scret-token":
|
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")
|
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||||
|
|
||||||
|
|
||||||
|
10
log.txt
Normal file
10
log.txt
Normal file
@ -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
|
29
main.py
29
main.py
@ -1,11 +1,12 @@
|
|||||||
# uvicorn main:app --host=127.0.0.1 --port=8000 --reload
|
# 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 dependencies import get_query_token, get_token_header
|
||||||
# from routers import items, users
|
# from routers import items, users
|
||||||
# from internal import admin
|
# from internal import admin
|
||||||
from ops.common import common
|
from ops.common import common
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from scripts.common.redis import get_redis_pool
|
from scripts.common.redis import get_redis_pool
|
||||||
|
import pdb
|
||||||
# import sys
|
# import sys
|
||||||
# sys.path.append('.')
|
# sys.path.append('.')
|
||||||
|
|
||||||
@ -25,8 +26,14 @@ tags_metadata = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(dependencies=[Depends(get_token_header)],
|
def create_app():
|
||||||
|
application = FastAPI(dependencies=[Depends(get_token_header)],
|
||||||
openapi_tags=tags_metadata)
|
openapi_tags=tags_metadata)
|
||||||
|
application.include_router(common.router, prefix="/common")
|
||||||
|
return application
|
||||||
|
|
||||||
|
|
||||||
|
app = create_app()
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
@ -40,12 +47,14 @@ async def shutdown_event():
|
|||||||
await app.state.redis.wait_close()
|
await app.state.redis.wait_close()
|
||||||
|
|
||||||
|
|
||||||
app.include_router(common.router)
|
|
||||||
|
|
||||||
|
|
||||||
@app.get("/")
|
@app.get("/")
|
||||||
async def root():
|
async def root(request: Request):
|
||||||
return {"message": "Hello Bigger Applications!"}
|
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:
|
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}")
|
@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"
|
message = f"message to {email} \n"
|
||||||
background_tasks.add_task(write_log, message)
|
background_tasks.add_task(write_log, message)
|
||||||
return {"message": "Message sent"}
|
return {"message": "Message sent"}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user