Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
Add CORS support
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jul 23, 2019
1 parent bb10d50 commit fa54dab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 12 additions & 0 deletions idunn/api/places.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
from elasticsearch import Elasticsearch
from apistar.exceptions import BadRequest, NotFound
from apistar.http import Response

from idunn import settings
from idunn.utils import prometheus
Expand All @@ -20,11 +21,13 @@ def validate_verbosity(verbosity):
})
return verbosity


def validate_lang(lang):
if not lang:
return settings['DEFAULT_LANGUAGE']
return lang.lower()


def get_place(id, es: Elasticsearch, indices: IndexNames, lang=None, type=None, verbosity=DEFAULT_VERBOSITY) -> Place:
"""Main handler that returns the requested place"""
verbosity = validate_verbosity(verbosity)
Expand Down Expand Up @@ -60,3 +63,12 @@ def get_place_latlon(lat: float, lon: float, es: Elasticsearch, lang=None, verbo
closest_place = None
place = Latlon(lat, lon, closest_address=closest_place)
return place.load_place(lang, verbosity)


def handle_option(id, es: Elasticsearch, indices: IndexNames, lang=None, type=None, verbosity=DEFAULT_VERBOSITY):
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': '*',
'Access-Control-Allow-Methods': 'GET',
}
return Response('', headers=headers)
11 changes: 9 additions & 2 deletions idunn/api/urls.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import os

from apistar import Route
from apistar_prometheus import expose_metrics, expose_metrics_multiprocess

from .pois import get_poi
from .places import get_place, get_place_latlon
from .places import get_place, get_place_latlon, handle_option
from .status import get_status
from .places_list import get_places_bbox, get_events_bbox
from .categories import get_all_categories
from .closest import closest_address


def get_metric_handler(settings):
"""Select the prometheus multiprocess mode or not"""
if settings['PROMETHEUS_MULTIPROC']:
return expose_metrics_multiprocess
return expose_metrics


def get_api_urls(settings):
"""Defines all endpoints
and handlers to build response
"""
metric_handler = get_metric_handler(settings)
return [
routes = [
Route('/metrics', 'GET', handler=metric_handler),
Route('/status', 'GET', handler=get_status),

Expand All @@ -40,3 +44,6 @@ def get_api_urls(settings):
# Kuzzle events
Route('/events', 'GET', handler=get_events_bbox),
]
if os.environ.get('ENABLE_OPTION_REQUESTS'):
routes.append(Route('/places/{whatever}', 'OPTION', handler=handle_option))
return routes

0 comments on commit fa54dab

Please sign in to comment.