import os import sys import uvicorn from fastapi import Depends, FastAPI, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse from fastapi_jwt_auth import AuthJWT from fastapi_jwt_auth.exceptions import AuthJWTException from pydantic import BaseModel import routers app = FastAPI() app.include_router(routers.router, prefix='/sc/v0') app.add_middleware( CORSMiddleware, allow_origins=["http://localhost:8001"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], expose_headers=["X-Total-Count"], ) @app.middleware("http") async def mock_x_total_count_header(request: Request, call_next): response = await call_next(request) response.headers["X-Total-Count"] = "100" return response << << << < HEAD == == == = for i in range(10): try: db = DictDB() except Exception: print( f'Database not responding, will try again soon. Attempt {i + 1} of 10.') else: break time.sleep(1) else: print('Database did not respond after 10 attempts, quitting.') sys.exit(-1) >>>>>> > main def get_pubkey(): try: if 'JWT_PUBKEY_PATH' in os.environ: keypath = os.environ['JWT_PUBKEY_PATH'] else: keypath = '/opt/certs/public.pem' with open(keypath, "r") as fd: pubkey = fd.read() except FileNotFoundError: print(f"Could not find JWT certificate in {keypath}") sys.exit(-1) return pubkey class JWTConfig(BaseModel): authjwt_algorithm: str = "ES256" authjwt_public_key: str = get_pubkey() @AuthJWT.load_config def jwt_config(): return JWTConfig() @app.exception_handler(AuthJWTException) def authjwt_exception_handler(request: Request, exc: AuthJWTException): return JSONResponse(content={"status": "error", "message": exc.message}, status_code=400) @app.exception_handler(RuntimeError) def app_exception_handler(request: Request, exc: RuntimeError): return JSONResponse(content={"status": "error", "message": str(exc.with_traceback(None))}, status_code=400) def main(standalone=False): if not standalone: return app uvicorn.run(app, host="0.0.0.0", port=8000, log_level="debug") if __name__ == '__main__': main(standalone=True) else: app = main()