41 lines
902 B
Python
41 lines
902 B
Python
# scopes.api.main
|
|
|
|
import sys
|
|
if '' not in sys.path:
|
|
sys.path = [''] + sys.path
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import HTMLResponse
|
|
import transaction # type: ignore
|
|
|
|
from scopes.api import tracking
|
|
from scopes.api.auth import user
|
|
|
|
# app and routes
|
|
|
|
app = FastAPI()
|
|
|
|
@app.middleware('http')
|
|
async def handle_transactions(request: Request, call_next):
|
|
response = await call_next(request)
|
|
if response.status_code >= 400:
|
|
transaction.abort()
|
|
else:
|
|
transaction.commit()
|
|
return response
|
|
|
|
app.include_router(tracking.router)
|
|
app.include_router(user.router)
|
|
|
|
@app.get('/')
|
|
async def read_root():
|
|
return {'Hello': 'World'}
|
|
|
|
@app.get('/demo')
|
|
async def read_demo():
|
|
return HTMLResponse('<b>Hello World</b>')
|
|
|
|
@app.get('/persons/{name}')
|
|
async def read_person(name: str, q: str | None = None):
|
|
return {'name': name, 'query': q}
|
|
|