Spaces:
Runtime error
Runtime error
File size: 3,659 Bytes
66340f1 |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
from fastapi import FastAPI
from fastapi.routing import APIRoute
from fastapi.staticfiles import StaticFiles
from fastapi_users import FastAPIUsers
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
from starlette.responses import FileResponse
from app.api import api_router
from app.core.config import settings
from app.deps.users import fastapi_users, jwt_authentication
from app.schemas.user import UserCreate, UserRead, UserUpdate
import os
from httpx_oauth.clients.google import GoogleOAuth2 # google client
# for dev only
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
def create_app():
description = f"{settings.PROJECT_NAME} API"
app = FastAPI(
title=settings.PROJECT_NAME,
openapi_url=f"{settings.API_PATH}/openapi.json",
docs_url="/docs/",
description=description,
redoc_url=None,
)
setup_routers(app, fastapi_users)
setup_cors_middleware(app)
serve_static_app(app)
return app
def setup_routers(app: FastAPI, fastapi_users: FastAPIUsers) -> None:
app.include_router(api_router, prefix=settings.API_PATH)
app.include_router(
fastapi_users.get_auth_router(
jwt_authentication,
requires_verification=False,
),
prefix=f"{settings.API_PATH}/auth/jwt",
tags=["auth"],
)
app.include_router(
fastapi_users.get_register_router(UserRead, UserCreate),
prefix=f"{settings.API_PATH}/auth",
tags=["auth"],
)
app.include_router(
fastapi_users.get_users_router(
UserRead, UserUpdate, requires_verification=False
),
prefix=f"{settings.API_PATH}/users",
tags=["users"],
)
google_oauth_client = GoogleOAuth2(
os.getenv("GOOGLE_OAUTH_CLIENT_ID", ""),
os.getenv("GOOGLE_OAUTH_CLIENT_SECRET", ""),
)
app.include_router(
fastapi_users.get_oauth_router(
google_oauth_client, jwt_authentication, "SECRET"
),
prefix="/auth/google",
tags=["auth"],
)
# The following operation needs to be at the end of this function
use_route_names_as_operation_ids(app)
def serve_static_app(app):
app.mount("/", StaticFiles(directory="static"), name="static")
@app.middleware("http")
async def _add_404_middleware(request: Request, call_next):
"""Serves static assets on 404"""
response = await call_next(request)
path = request["path"]
if path.startswith(settings.API_PATH) or path.startswith("/docs"):
return response
if response.status_code == 404:
return FileResponse("static/index.html")
return response
def setup_cors_middleware(app):
if settings.BACKEND_CORS_ORIGINS:
app.add_middleware(
CORSMiddleware,
allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
allow_credentials=True,
allow_methods=["*"],
expose_headers=["Content-Range", "Range"],
allow_headers=["Authorization", "Range", "Content-Range"],
)
def use_route_names_as_operation_ids(app: FastAPI) -> None:
"""
Simplify operation IDs so that generated API clients have simpler function
names.
Should be called only after all routes have been added.
"""
route_names = set()
for route in app.routes:
if isinstance(route, APIRoute):
if route.name in route_names:
raise Exception("Route function names should be unique")
route.operation_id = route.name
route_names.add(route.name)
|