Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions functions-python/operations_api/function_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"environment_variables": [
{
"key": "GOOGLE_CLIENT_ID"
},
{
"key": "CORS_ALLOWED_ORIGINS"
}
],
"secret_environment_variables": [
Expand Down
18 changes: 18 additions & 0 deletions functions-python/operations_api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
# limitations under the License.
#

import os

from flask import Request, Response
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from feeds_gen.apis.operations_api import router as FeedsApiRouter
from feeds_gen.apis.licenses_api import router as LicenseApiRouter
from feeds_gen.apis.users_api import router as UsersApiRouter
Expand All @@ -35,6 +38,21 @@

# Add here middlewares that should be applied to all routes.
app.add_middleware(RequestContextMiddleware)
# Added last so it becomes the outermost ASGI layer (Starlette middlewares wrap
# in reverse order of registration) and can answer CORS preflight OPTIONS
# requests before they ever reach RequestContextMiddleware's auth check, which
# would otherwise 401 every preflight since browsers never send Authorization
# on an OPTIONS request.
app.add_middleware(
CORSMiddleware,
allow_origins=[
origin.strip()
for origin in os.getenv("CORS_ALLOWED_ORIGINS", "*").split(",")
if origin.strip()
],
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(FeedsApiRouter)
app.include_router(LicenseApiRouter)
app.include_router(UsersApiRouter)
Expand Down
Loading