myops/main.py
2021-06-21 12:07:23 +08:00

65 lines
1.8 KiB
Python

# cd .. && uvicorn myops.main:app --reload
from fastapi import Depends, FastAPI, BackgroundTasks
from .dependencies import get_query_token, get_token_header
from .routers import items, users
from .internal import admin
from typing import Optional
tags_metadata = [
{
"name": "users",
"description": "Operations with users. The **login** logic is also here.",
},
{
"name": "items",
"description": "Manage items. So _fancy_ they have their own docs.",
"externalDocs": {
"description": "Items external docs",
"url": "https://fastapi.tiangolo.com/",
},
},
]
# app = FastAPI(dependencies=[Depends(get_token_header)], title="My ops project",
# description="This is some interface for ops in kingsome!", version="1.0.1")
app = FastAPI(dependencies=[Depends(get_token_header)],
openapi_tags=tags_metadata)
app.include_router(users.router)
app.include_router(items.router)
app.include_router(
admin.router,
prefix="/admin",
tags=["admin"],
dependencies=[Depends(get_token_header)],
responses={418: {"description": "I`m a teapot"}}
)
app.get("/")
async def root():
return {"message": "Hello Bigger Applications!"}
def write_log(message: str):
with open("log.txt", mode="a") as log:
log.write(message)
def get_query(background_tasks: BackgroundTasks, q: Optional[str] = None):
if q:
message = f"found query:{q}\n"
background_tasks.add_task(write_log, message)
return q
@app.post("/send-notification/{email}")
async def send_notification(email: str, background_tasks: BackgroundTasks, q: str = Depends(get_query)):
message = f"message to {email} \n"
background_tasks.add_task(write_log, message)
return {"message": "Message sent"}