From d5c257a791a927fdb0cd117cfc8fd731a3dc2a46 Mon Sep 17 00:00:00 2001 From: Donny Winston Date: Fri, 19 Apr 2024 13:15:57 -0400 Subject: [PATCH] load optional info banner via env var --- .env.example | 4 +++- nmdc_runtime/api/main.py | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 4706e636..95909141 100644 --- a/.env.example +++ b/.env.example @@ -38,4 +38,6 @@ NEON_API_BASE_URL=https://data.neonscience.org/api/v0 NERSC_USERNAME=replaceme ORCID_NMDC_CLIENT_ID=replaceme -ORCID_NMDC_CLIENT_SECRET=replaceme \ No newline at end of file +ORCID_NMDC_CLIENT_SECRET=replaceme + +INFO_BANNER_INNERHTML='Announcement: Something important is about to happen. If you have questions, please contact support@microbiomedata.org.' \ No newline at end of file diff --git a/nmdc_runtime/api/main.py b/nmdc_runtime/api/main.py index 806f2e09..2e00c002 100644 --- a/nmdc_runtime/api/main.py +++ b/nmdc_runtime/api/main.py @@ -8,6 +8,7 @@ import fastapi import requests import uvicorn +from bs4 import BeautifulSoup from fastapi import APIRouter, FastAPI, Cookie from fastapi.middleware.cors import CORSMiddleware from fastapi.openapi.docs import get_swagger_ui_html @@ -483,10 +484,16 @@ def custom_swagger_ui_html( access_token = rv.json()["access_token"] swagger_ui_parameters = {"withCredentials": True} + onComplete = "" if access_token is not None: + onComplete += f"""ui.preauthorizeApiKey(bearerAuth, {access_token}); """ + if os.getenv("INFO_BANNER_INNERHTML"): + info_banner_innerhtml = os.getenv("INFO_BANNER_INNERHTML") + onComplete += f"""banner = document.createElement(section); banner.classList.add(nmdc-info-banner); banner.classList.add(block); banner.classList.add(col-12); banner.innerHTML = `{info_banner_innerhtml.replace('"', '')}`; document.querySelector(.information-container).prepend(banner); """ + if onComplete: swagger_ui_parameters.update( { - "onComplete": f"""() => {{ ui.preauthorizeApiKey(bearerAuth, {access_token}) }}""", + "onComplete": f"""() => {{ {onComplete} }}""", } ) response = get_swagger_ui_html( @@ -504,6 +511,10 @@ def custom_swagger_ui_html( .replace('"', "") .replace("", '"') .replace("", '"') + .replace( + "", + "", + ) ) return HTMLResponse(content=content)