Skip to content

Commit

Permalink
Fix openapi generation within docker
Browse files Browse the repository at this point in the history
  • Loading branch information
francbartoli committed Oct 16, 2024
1 parent 95575a4 commit 9e29bbf
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 248 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.venv
pygeoapi-openapi.yml
pygeoapi-openapi.json
3 changes: 0 additions & 3 deletions .env
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
ENV_STATE=dev

# base configs
# tiangolo uvicorn-gunicorn-fastapi-docker configs
MODULE_NAME=app.main
VARIABLE_NAME=app
#- GUNICORN_CONF="/app/custom_gunicorn_conf.py"
WORKERS_PER_CORE=1
WEB_CONCURRENCY=2
HOST=0.0.0.0
PORT=5000
LOG_LEVEL=debug
#- WORKER_CLASS="uvicorn.workers.UvicornWorker"
TIMEOUT=120

# aws
Expand Down
27 changes: 15 additions & 12 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,21 @@ async def custom_app_exception_handler(request, e):
os.environ["HOST"] = cfg.HOST
os.environ["PORT"] = cfg.PORT

# import pygeoapi starlette application once env vars are set
# and prepare the objects to override some core behavior
# prepare pygeoapi openapi file if it doesn't exist
pygeoapi_conf = Path.cwd() / os.environ["PYGEOAPI_CONFIG"]
pygeoapi_oapi = Path.cwd() / os.environ["PYGEOAPI_OPENAPI"]
if not pygeoapi_oapi.exists():
pygeoapi_oapi.write_text(data="")
with pygeoapi_oapi.open(mode="w") as oapi_file:
oapi_content = generate_openapi_document(
pygeoapi_conf,
output_format="yaml",
)
logger.debug(f"OpenAPI content: \n{oapi_content}")
oapi_file.write(oapi_content)

# import pygeoapi starlette application once pygeoapi configuration
# are set and prepare the objects to override some core behavior
from pygeoapi.starlette_app import APP as PYGEOAPI_APP
from pygeoapi.starlette_app import url_prefix
from starlette.applications import Starlette
Expand All @@ -113,16 +126,6 @@ async def custom_app_exception_handler(request, e):
]
)

pygeoapi_conf = Path.cwd() / os.environ["PYGEOAPI_CONFIG"]
pygeoapi_oapi = Path.cwd() / os.environ["PYGEOAPI_OPENAPI"]
with pygeoapi_oapi.open(mode="w") as oapi_file:
oapi_content = generate_openapi_document(
pygeoapi_conf,
output_format="yaml",
)
logger.debug(f"OpenAPI content: \n{oapi_content}")
oapi_file.write(oapi_content)

except FileNotFoundError:
logger.error("Please configure pygeoapi settings in .env properly")
raise
Expand Down
Loading

0 comments on commit 9e29bbf

Please sign in to comment.