-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·93 lines (71 loc) · 2.44 KB
/
server.py
File metadata and controls
executable file
·93 lines (71 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
import json
import uvicorn
from fastapi import Depends, Request, Response, FastAPI, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse
from contextlib import asynccontextmanager
from starlette.background import BackgroundTask
from routers import BASE_PATH, config, resource, schema, users, groups
from utils.auth import api_key_auth
from utils import host, port
import os
import sys
import logging
logging.basicConfig(
level=os.environ.get('LOGLEVEL', 'INFO').upper()
)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
# Why is this here in the first place?
pass
yield
pass
app = FastAPI(lifespan=lifespan)
app = FastAPI(
lifespan=lifespan,
title="SCIM Sample",
docs_url=BASE_PATH if BASE_PATH.startswith('/') else '/',
redoc_url=None,
dependencies=[Depends(api_key_auth)],
openapi_url=BASE_PATH + '/openapi.json',
responses={
401: {"description": "Operation forbidden"},
404: {"description": "Not found"},
422: {"description": "Unprocessable input"},
},
)
app.include_router(config.router)
app.include_router(resource.router)
app.include_router(schema.router)
app.include_router(users.router)
app.include_router(groups.router)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
request: Request,
exc: RequestValidationError
):
exc_str = f'{exc}'.replace('\n', ' ').replace(' ', ' ')
logging.error(f"{request}: {exc_str}")
content = {'status_code': 10422, 'message': exc_str}
return JSONResponse(
content=content,
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY
)
def log_info(req_body, res_body):
logging.debug("request: {}".format(json.dumps(req_body)))
logging.debug("response: {}".format(json.dumps(res_body)))
@app.middleware('http')
async def some_middleware(request: Request, call_next):
req_body = await request.body()
response = await call_next(request)
chunks = []
async for chunk in response.body_iterator:
chunks.append(chunk)
res_body = b''.join(chunks)
task = BackgroundTask(log_info, req_body, res_body)
return Response(content=res_body, status_code=response.status_code,
headers=dict(response.headers), media_type=response.media_type, background=task)
if __name__ == "__main__":
uvicorn.run(app, host=host, port=port)