Skip to content

Commit

Permalink
Take out server-side proxying infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
zmbc authored and goldpbear committed Oct 13, 2017
1 parent fa20320 commit c27d959
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 58 deletions.
8 changes: 8 additions & 0 deletions src/base/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,17 @@ <h3>
}
</script>

<<<<<<< fa20320487d039d0e61102467dc883750e35130c
<script src="{{STATIC_URL}}dist/bundle.js?ver=0.7.6.8"></script>
=======
<script src="{{STATIC_URL}}dist/bundle.js?ver=0.7.4.1"></script>

<!--
TODO: How did these work? Need to test
>>>>>>> Take out server-side proxying infrastructure
<script src="{{API_ROOT}}users/current?format=jsonp&callback=bootstrapCurrentUser"></script>
<script src="{{API_ROOT}}utils/session-key?format=jsonp&callback=setApiSessionCookie"></script>
-->

{% handlebarsjs '(.*)' precompile register_partials %}
{% handlebarsjs 'pages/*' precompile %}
Expand Down
4 changes: 0 additions & 4 deletions src/base/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),

url(r'^api/(.*)$', views.api, name='api_proxy'),
url(r'^users/(.*)$', views.users, name='auth_proxy'),
url(r'^download/(.*).csv$', views.csv_download, name='csv_proxy'),
url(r'^dataset/(?P<dataset_id>[^/]+)/(?P<path>.*)$', views.api, name='dataset_api_proxy'),
url(r'^place/(?P<place_id>[^/]+)$', views.index, name='place'),
url(r'^', views.index, name='index'),
)
64 changes: 10 additions & 54 deletions src/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,12 @@

log = logging.getLogger(__name__)


def make_api_root(dataset_root):
components = dataset_root.split('/')
if dataset_root.endswith('/'):
return '/'.join(components[:-4]) + '/'
else:
return '/'.join(components[:-3]) + '/'

def make_auth_root(dataset_root):
return make_api_root(dataset_root) + 'users/'

def make_resource_uri(resource, root):
resource = resource.strip('/')
root = root.rstrip('/')
uri = '%s/%s' % (root, resource)
return uri


class ShareaboutsApi (object):
def __init__(self, dataset_root):
self.dataset_root = dataset_root
self.auth_root = make_auth_root(dataset_root)
self.root = make_api_root(dataset_root)

def get(self, resource, default=None, **kwargs):
uri = make_resource_uri(resource, root=self.dataset_root)
res = requests.get(uri, params=kwargs,
headers={'Accept': 'application/json'})
return (res.text if res.status_code == 200 else default)

def current_user(self, default=u'null', **kwargs):
uri = make_resource_uri('current', root=self.auth_root)
res = requests.get(uri, headers={'Accept': 'application/json'}, **kwargs)

return (res.text if res.status_code == 200 else default)


@ensure_csrf_cookie
def index(request, place_id=None):
# Load app config settings
config = get_shareabouts_config(settings.SHAREABOUTS.get('CONFIG'))
config.update(settings.SHAREABOUTS.get('CONTEXT', {}))

# Get initial data for bootstrapping into the page.
dataset_root = settings.SHAREABOUTS.get('DATASET_ROOT')
if (dataset_root.startswith('file:')):
dataset_root = request.build_absolute_uri(reverse('api_proxy', args=('',)))
api = ShareaboutsApi(dataset_root=dataset_root)

# Get the content of the static pages linked in the menu.
pages_config = config.get('pages', [])
pages_config_json = json.dumps(pages_config)
Expand Down Expand Up @@ -101,24 +58,20 @@ def index(request, place_id=None):
"browser": {"name": "", "version": None},
"platform": {"name": "", "version": None}
})

place = None
if place_id and place_id != 'new':
place = api.get('places/' + place_id)
if place:
place = json.loads(place)

# Get initial data for bootstrapping into the page.
dataset_root = settings.SHAREABOUTS.get('DATASET_ROOT')
if (dataset_root.startswith('file:')):
dataset_root = request.build_absolute_uri(reverse('api_proxy', args=('',)))

context = {'config': config,

'user_token_json': user_token_json,
'pages_config': pages_config,
'pages_config_json': pages_config_json,
'user_agent_json': user_agent_json,
# Useful for customized meta tags
'place': place,

'API_ROOT': api.root,
'DATASET_ROOT': api.dataset_root,
'API_ROOT': dataset_root,
'DATASET_ROOT': dataset_root
}

return render(request, 'index.html', context)
Expand Down Expand Up @@ -362,6 +315,7 @@ def readonly_file_api(request, path, datafilename='data.json'):
return readonly_response(request, feature)
else:
raise Http404
<<<<<<< fa20320487d039d0e61102467dc883750e35130c


def api(request, path, **kwargs):
Expand Down Expand Up @@ -465,3 +419,5 @@ def csv_download(request, path):
response['Content-disposition'] = 'attachment; filename=' + filename

return response
=======
>>>>>>> Take out server-side proxying infrastructure

0 comments on commit c27d959

Please sign in to comment.