Skip to content

Commit

Permalink
Merge pull request #507 from microbiomedata/506-nmdc-info-banner
Browse files Browse the repository at this point in the history
load optional info banner via env var
  • Loading branch information
PeopleMakeCulture authored Apr 19, 2024
2 parents 2c11b6a + d5c257a commit 6936534
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
ORCID_NMDC_CLIENT_SECRET=replaceme

INFO_BANNER_INNERHTML='Announcement: Something important is about to happen. If you have questions, please contact <a href="mailto:support@microbiomedata.org">support@microbiomedata.org</a>.'
13 changes: 12 additions & 1 deletion nmdc_runtime/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(<double-quote>bearerAuth</double-quote>, <double-quote>{access_token}</double-quote>); """
if os.getenv("INFO_BANNER_INNERHTML"):
info_banner_innerhtml = os.getenv("INFO_BANNER_INNERHTML")
onComplete += f"""banner = document.createElement(<double-quote>section</double-quote>); banner.classList.add(<double-quote>nmdc-info-banner</double-quote>); banner.classList.add(<double-quote>block</double-quote>); banner.classList.add(<double-quote>col-12</double-quote>); banner.innerHTML = `{info_banner_innerhtml.replace('"', '<double-quote>')}`; document.querySelector(<double-quote>.information-container</double-quote>).prepend(banner); """
if onComplete:
swagger_ui_parameters.update(
{
"onComplete": f"""<unquote-safe>() => {{ ui.preauthorizeApiKey(<double-quote>bearerAuth</double-quote>, <double-quote>{access_token}</double-quote>) }}</unquote-safe>""",
"onComplete": f"""<unquote-safe>() => {{ {onComplete} }}</unquote-safe>""",
}
)
response = get_swagger_ui_html(
Expand All @@ -504,6 +511,10 @@ def custom_swagger_ui_html(
.replace('</unquote-safe>"', "")
.replace("<double-quote>", '"')
.replace("</double-quote>", '"')
.replace(
"</head>",
"<style>.nmdc-info-banner { padding: 1em; background-color: #448aff1a; border: .075rem solid #448aff; }</style></head>",
)
)
return HTMLResponse(content=content)

Expand Down

0 comments on commit 6936534

Please sign in to comment.