Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add api-catalog support #60

Merged
merged 4 commits into from
Jan 19, 2025
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
9 changes: 9 additions & 0 deletions services/home/app/config.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import logging
import os

base_url = 'https://demo.pygeoapi.io'

# local config, change on server for real config
config = {
'loglevel': int(os.getenv('ADMIN_LOG_LEVEL', logging.DEBUG))
}

services = [
{'href': '/master', 'title': "pygeoapi - latest GitHub 'master' version"},
{'href': '/stable', 'title': 'pygeoapi - latest stable version'},
{'href': '/cite', 'title': "pygeoapi - CITE endpoint - latest GitHub 'master' version"}, # noqa
{'href': '/covid-19', 'title': "pygeoapi - COVID-19 endpoint - latest GitHub 'master' version"} # noqa
]
43 changes: 38 additions & 5 deletions services/home/app/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from datetime import datetime
from functools import wraps, update_wrapper

from config import config
from config import base_url, config, services
from flask import Flask, render_template, make_response, send_from_directory


if __name__ != '__main__':
# When run with WSGI in Apache we need to extend the PYTHONPATH to find Python modules relative to index.py
# When run with WSGI in Apache we need to extend the PYTHONPATH
# to find Python modules relative to index.py
sys.path.insert(0, os.path.dirname(__file__))

app = Flask(__name__)
Expand All @@ -30,7 +31,7 @@ def nocache(view):
def no_cache(*args, **kwargs):
response = make_response(view(*args, **kwargs))
response.headers['Last-Modified'] = datetime.now()
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0' # noqa
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '-1'
return response
Expand All @@ -45,6 +46,37 @@ def home():
return page('home')


# API catalog
@app.route('/api-catalog.json')
@app.route('/.well-known/api-catalog')
@nocache
def api_catalog():
response = {
'linkset': []
}

for service in services:
url = f"{base_url}{service['href']}"
response['linkset'].append({
'anchor': url,
'service-desc': [{
'href': f'{url}/openapi?f=json',
'title': f"{service['title']} (JSON)",
'type': 'application/vnd.oai.openapi+json'
}],
'service-doc': [{
'href': f'{url}/openapi?f=html',
'title': f"{service['title']} (HTML)",
'type': 'text/html'
}],
})

r = make_response(response)
r.mimetype = 'application/linkset+json'

return r


# Specific page
@app.route('/<string:page_name>')
@nocache
Expand All @@ -69,12 +101,13 @@ def page(page_name):
else:
page_file = '%s%s' % (page_name, '.html')

result = render_template(page_file)
except Exception as e:
result = render_template(page_file, services=services)
except Exception:
result = render_template('error.html'), 404

return result


if __name__ == '__main__':
# Run as main via python index.py
app.run(debug=True, host='0.0.0.0')
17 changes: 6 additions & 11 deletions services/home/app/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,16 @@ <h2>Welcome to pygeoapi Demo Server</h2>
<table style="width: auto;" class="table table-bordered table-condensed table-responsive">
<thead>
<tr>
<th width="50%">Demonstrations</th>
<th width="50%">Demonstrations (<a href="{{ url_for('api_catalog') }}" rel="api-catalog">API Catalog</a>)</th>
</tr>
</thead>

{% for service in services %}
<tr>
<td><a target="_blank" href="/master" class="btn btn-success">pygeoapi - latest GitHub 'master' version</a></td>
</tr>
<tr>
<td><a target="_blank" href="/stable" class="btn btn-success">pygeoapi - latest stable version</a></td>
</tr>
<tr>
<td><a target="_blank" href="/cite" class="btn btn-success">pygeoapi - CITE endpoint - latest GitHub 'master' version</a></td>
</tr>
<tr>
<td><a target="_blank" href="/covid-19" class="btn btn-success">pygeoapi - COVID-19 endpoint - latest GitHub 'master' version</a></td>
<td><a target="_blank" href="{{ service['href'] }}" class="btn btn-success">{{ service['title'] }}</a></td>
</tr>
{% endfor %}

<tr>
<td><a target="_blank" href="https://opengeogroep.github.io/ogc-api-features-testclient/src/index.html" class="btn btn-success">Test Client - by OpenGeoGroep</a></td>
</tr>
Expand Down