-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rework to replace aiohttp with fastapi
- Loading branch information
Showing
20 changed files
with
1,110 additions
and
1,172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import logging | ||
from contextlib import AsyncExitStack, asynccontextmanager | ||
from pprint import pformat | ||
|
||
from fastapi import FastAPI | ||
|
||
from . import broadcaster, mqtt, parser, scanner, utils | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def setup_logging(debug: bool): | ||
level = logging.DEBUG if debug else logging.INFO | ||
unimportant_level = logging.INFO if debug else logging.WARN | ||
format = '%(asctime)s.%(msecs)03d [%(levelname).1s:%(name)s:%(lineno)d] %(message)s' | ||
datefmt = '%Y/%m/%d %H:%M:%S' | ||
|
||
logging.basicConfig(level=level, format=format, datefmt=datefmt) | ||
logging.captureWarnings(True) | ||
|
||
logging.getLogger('gmqtt').setLevel(unimportant_level) | ||
logging.getLogger('httpx').setLevel(unimportant_level) | ||
logging.getLogger('httpcore').setLevel(logging.WARN) | ||
logging.getLogger('uvicorn.access').setLevel(unimportant_level) | ||
logging.getLogger('uvicorn.error').disabled = True | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
LOGGER.info(utils.get_config()) | ||
LOGGER.debug('LOGGERS:\n' + pformat(logging.root.manager.loggerDict)) | ||
|
||
async with AsyncExitStack() as stack: | ||
await stack.enter_async_context(mqtt.lifespan()) | ||
await stack.enter_async_context(broadcaster.lifespan()) | ||
yield | ||
|
||
|
||
def create_app() -> FastAPI: | ||
config = utils.get_config() | ||
setup_logging(config.debug) | ||
|
||
# Call setup functions for modules | ||
mqtt.setup() | ||
parser.setup() | ||
scanner.setup() | ||
|
||
app = FastAPI(lifespan=lifespan) | ||
return app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.