Skip to content

Commit

Permalink
Improve pygeoapi configuration (#19)
Browse files Browse the repository at this point in the history
* Update pygeoapi configuration

* Update Docker entrypoint

Separate out source alignment from server startup

* Hide fix pygeoapi link in landing page
  • Loading branch information
webb-ben authored Sep 27, 2023
1 parent a51d16f commit 47d66b8
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 35 deletions.
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ services:
NLDI_DB_HOST: nldi-db
NLDI_DB_PORT: 5432
NLDI_DB_NAME: nldi
NLDI_DB_USERNAME: read_only_user
NLDI_DB_USERNAME: nldi
NLDI_DB_PASSWORD: changeMe
pygeoapiUrl: "https://labs.waterdata.usgs.gov/api/nldi/pygeoapi/"
PYGEOAPI_URL: https://labs.waterdata.usgs.gov/api/nldi/pygeoapi/
networks:
- nldi

Expand Down
5 changes: 4 additions & 1 deletion docker/default.source.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@
server:
url: ${NLDI_URL}
pretty_print: false
pygeoapi: true
data:
host: ${NLDI_DB_HOST}
port: ${NLDI_DB_PORT}
dbname: ${NLDI_DB_NAME}
user: ${NLDI_DB_USERNAME}
password: ${NLDI_DB_PASSWORD}

pygeoapi:
enabled: true
gdp_url: ${PYGEOAPI_URL}

logging:
level: ERROR
# logfile: /tmp/nldi.log
Expand Down
47 changes: 30 additions & 17 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,44 @@ entry_cmd=${1:-run}

# Shorthand
function error() {
echo "ERROR: $@"
exit -1
echo "ERROR: $@"
exit -1
}

# Workdir
cd ${NLDI_HOME}

echo "Trying to generate openapi.yml"
pygeoapi openapi generate ${PYGEOAPI_CONFIG} --output-file ${PYGEOAPI_OPENAPI}
nldi config align-sources ${NLDI_CONFIG}
nldi openapi generate ${NLDI_CONFIG} --output-file ${NLDI_OPENAPI}
case ${entry_cmd} in
# Align source table
align)
nldi config align-sources ${NLDI_CONFIG}
;;

[[ $? -ne 0 ]] && error "openapi.yml could not be generated ERROR"
# Run NLDI Server
run)
echo "Trying to generate openapi.yml"
pygeoapi openapi generate ${PYGEOAPI_CONFIG} --output-file ${PYGEOAPI_OPENAPI}
nldi openapi generate ${NLDI_CONFIG} --output-file ${NLDI_OPENAPI}

echo "openapi.yml generated continue to nldi"
[[ $? -ne 0 ]] && error "openapi.yml could not be generated ERROR"

# SCRIPT_NAME should not have value '/'
[[ "${SCRIPT_NAME}" = '/' ]] && export SCRIPT_NAME="" && echo "make SCRIPT_NAME empty from /"
echo "openapi.yml generated continue to nldi"

echo "Start gunicorn name=${CONTAINER_NAME} on ${CONTAINER_HOST}:${CONTAINER_PORT} with ${WSGI_WORKERS} workers and SCRIPT_NAME=${SCRIPT_NAME}"
exec gunicorn --workers ${WSGI_WORKERS} \
--worker-class=${WSGI_WORKER_CLASS} \
--timeout ${WSGI_WORKER_TIMEOUT} \
--name=${CONTAINER_NAME} \
--bind ${CONTAINER_HOST}:${CONTAINER_PORT} \
nldi.flask_app:APP
# SCRIPT_NAME should not have value '/'
[[ "${SCRIPT_NAME}" = '/' ]] && export SCRIPT_NAME="" && echo "make SCRIPT_NAME empty from /"

echo "Start gunicorn name=${CONTAINER_NAME} on ${CONTAINER_HOST}:${CONTAINER_PORT} with ${WSGI_WORKERS} workers and SCRIPT_NAME=${SCRIPT_NAME}"
exec gunicorn --workers ${WSGI_WORKERS} \
--worker-class=${WSGI_WORKER_CLASS} \
--timeout ${WSGI_WORKER_TIMEOUT} \
--name=${CONTAINER_NAME} \
--bind ${CONTAINER_HOST}:${CONTAINER_PORT} \
nldi.flask_app:APP
;;

*)
error "unknown command arg: must be run (default) or align"
;;
esac

echo "END /entrypoint.sh"
24 changes: 16 additions & 8 deletions nldi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,14 @@ def flowline_lookup(self) -> FlowlineLookup:
def pygeoapi_lookup(self) -> PygeoapiLookup:
"""pygeoapi Lookup Provider"""
if self._pygeoapi_lookup is None:
try:
PYGEOAPI_URL = self.config['pygeoapi']['gdp_url']
except KeyError:
PYGEOAPI_URL = 'https://labs.waterdata.usgs.gov/api/nldi/pygeoapi' # noqa
self._pygeoapi_lookup = \
self.load_plugin('PygeoapiLookup',
catchment_lookup=self.catchment_lookup)
catchment_lookup=self.catchment_lookup,
pygeoapi_url=PYGEOAPI_URL)
return self._pygeoapi_lookup

@pre_process
Expand Down Expand Up @@ -197,13 +202,16 @@ def landing_page(self, request: Union[APIRequest, Any]
}]
}

if self.config['server']['pygeoapi'] is True:
content['links'].append({
'rel': 'data',
'type': 'text/html',
'title': 'pygeoapi for the NLDI',
'href': f'{self.base_url}/pygeoapi?f=html'
})
try:
if self.config['pygeoapi']['enabled'] is True:
content['links'].append({
'rel': 'data',
'type': 'text/html',
'title': 'pygeoapi for the NLDI',
'href': f'{self.base_url}/pygeoapi?f=html'
})
except KeyError:
LOGGER.debug('Omitting pygeoapi link')

return headers, HTTPStatus.OK, to_json(content, self.pretty_print)

Expand Down
9 changes: 6 additions & 3 deletions nldi/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ def get_navigation(source_name=None, identifier=None, nav_mode=None, data_source
request, source_name, identifier, nav_mode, data_source))


if CONFIG['server']['pygeoapi'] is True:
from pygeoapi.flask_app import BLUEPRINT as PYGEOAPI_BLUEPRINT
APP.register_blueprint(PYGEOAPI_BLUEPRINT, url_prefix='/pygeoapi')
try:
if CONFIG['pygeoapi']['enabled'] is True:
from pygeoapi.flask_app import BLUEPRINT as PYGEOAPI_BLUEPRINT
APP.register_blueprint(PYGEOAPI_BLUEPRINT, url_prefix='/pygeoapi')
except KeyError:
LOGGER.info('Not including pygeoapi templates')

APP.register_blueprint(BLUEPRINT)
6 changes: 2 additions & 4 deletions nldi/lookup/pygeoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

import json
import logging
import os
from requests import Session as HTTPSession
from shapely import wkt
from sqlalchemy import func
Expand All @@ -42,8 +41,6 @@

LOGGER = logging.getLogger(__name__)

PYGEOAPI_URL = os.getenv(
'PYGEOAPI_URL', 'https://labs.waterdata.usgs.gov/api/nldi/pygeoapi')
DEFAULT_PROPS = {
'identifier': '', 'navigation': '', 'measure': '',
'reachcode': '', 'name': '', 'source': 'provided',
Expand All @@ -66,6 +63,7 @@ def __init__(self, provider_def):
"""
LOGGER.debug('Initialising Pygeoapi Lookup.')
self.catchment_lookup = provider_def['catchment_lookup']
self.pygeoapi_url = provider_def['pygeoapi_url']
self.base_url = provider_def['base_url']
self.http = HTTPSession()

Expand All @@ -90,7 +88,7 @@ def get_hydrolocation(self, coords: str) -> dict:
}

LOGGER.debug('Making OGC API - Processes request')
url = url_join(PYGEOAPI_URL, 'processes/nldi-flowtrace/execution')
url = url_join(self.pygeoapi_url, 'processes/nldi-flowtrace/execution')
response = self._get_response(url, data=data)

LOGGER.debug('Getting feature intersection')
Expand Down

0 comments on commit 47d66b8

Please sign in to comment.