added fastapi simple example

This commit is contained in:
typhoon 2022-05-13 22:56:34 +10:00
parent 17f2970020
commit e5d9bf427e
2 changed files with 66 additions and 21 deletions

View file

@ -1,12 +1,10 @@
from fastapi_utils.inferring_router import InferringRouter
from fastapi import FastAPI, status, HTTPException
from aiogram import Bot, Dispatcher, types
from fastapi_utils.cbv import cbv
from fastapi import FastAPI
import logging
import asyncio
API_TOKEN = "<TOKEN>"
logging.basicConfig(level=logging.DEBUG)
class AsyncioBot:
@ -33,23 +31,15 @@ class AsyncioBot:
app = FastAPI()
bot = AsyncioBot()
router = InferringRouter()
@cbv(router)
class MainServer:
@app.on_event("startup")
async def on_startup():
await bot.run()
@app.on_event("startup")
async def on_startup():
await bot.run()
@router.get("/echo", status_code=status.HTTP_200_OK)
async def echo(self):
return {
"status": True
}
@app.on_event("shutdown")
async def on_shutdown():
await bot.close() # closing bot without error
@app.get("/")
async def root():
return {"data": "Hello World"}
app.include_router(router)
@app.on_event("shutdown")
async def on_shutdown():
await bot.close() # closing bot without error