Starting the slow migration to FastAPI...

This commit is contained in:
Scott Idem
2020-11-10 17:04:12 -05:00
parent 6bee9c19cf
commit 8433960d0d
12 changed files with 524 additions and 163 deletions

View File

@@ -16,7 +16,18 @@ from sqlalchemy.exc import IntegrityError, OperationalError
from . import config
from .lib_general import *
from .log import *
from .routers import items, users, websockets
# Import the routers here first:
from .routers import items, journals, users, websockets
# TEST TEST TEST
print('**** Calling db.py ... ****')
#from .db import engine, SessionLocal, Base
from .db import db
print('**** Called db.py ****')
# TEST TEST TEST
#log = logging.getLogger('root')
#log.setLevel(logging.ERROR) # DEBUG > INFO > WARNING > ERROR > CRITICAL
@@ -36,14 +47,7 @@ def get_settings():
app.mount('/static', StaticFiles(directory='static'), name='static')
app.include_router(
users.router,
prefix='/user',
tags=['Users'],
#dependencies=[Depends(get_token_header)],
#dependencies=[Depends(get_account_header)],
#responses={404: {'description': 'Not found'}},
)
# Set up each route once the router has been imported
app.include_router(
items.router,
prefix='/item',
@@ -51,9 +55,26 @@ app.include_router(
#dependencies=[Depends(get_token_header)],
#responses={404: {'description': 'Not found'}},
)
app.include_router(
journals.router,
prefix='/journal',
tags=['Journals'],
#dependencies=[Depends(get_token_header)],
#dependencies=[Depends(get_account_header)],
#responses={404: {'description': 'Not found'}},
)
app.include_router(
users.router,
prefix='/user',
tags=['Users'],
#dependencies=[Depends(get_token_header)],
#dependencies=[Depends(get_account_header)],
#responses={404: {'description': 'Not found'}},
)
app.include_router(
websockets.router,
#prefix='/item',
#prefix='/websocket',
tags=['Websockets'],
#dependencies=[Depends(get_token_header)],
#responses={404: {'description': 'Not found'}},
@@ -82,6 +103,24 @@ app.add_middleware(
# END: CORS
@app.on_event('startup')
async def startup():
log.setLevel(logging.INFO) # DEBUG, INFO, WARN, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info('FastAPI app is starting up...')
#await database.connect()
@app.on_event('shutdown')
async def shutdown():
log.setLevel(logging.INFO) # DEBUG, INFO, WARN, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info('FastAPI app is shutting down...')
#await database.disconnect()
#Add the processing time to the response header.
@app.middleware('http')
async def add_process_time_header(request: Request, call_next):
@@ -95,7 +134,10 @@ async def add_process_time_header(request: Request, call_next):
@app.get('/', tags=['Default'])
async def get_root():
print(config.settings.APP_NAME)
log.setLevel(logging.INFO) # DEBUG, INFO, WARN, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info(config.settings.APP_NAME)
log.setLevel(logging.DEBUG)
@@ -118,3 +160,42 @@ async def get_root():
print('^^^')
return {'hello': 'This is the Aether API using FastAPI.'}
# ### TEST TEST TEST ### #
@app.get('/quick_test', tags=['Default'])
async def quick_test():
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARN, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
log.info('Getting all accounts...')
sql = text(
"""
SELECT *
FROM `account`
"""
)
try:
result = db.execute(sql)
except Exception as e:
log.error('*** An exception happened. ***')
log.error(repr(e))
log.error('***')
log.error(str(e))
log.error('^^^ exception ^^^')
else:
if result.rowcount:
records = result.fetchall()
log.debug(records)
else:
log.warning('Something went wrong.')
log.info('Got the account list')
response = {}
response['hello'] = 'This is the Aether API using FastAPI.'
response['data'] = records
return response
# ### TEST TEST TEST ### #