Permalink
Browse files

proxy/static web server to allow local development

  • Loading branch information...
1 parent 66c0bbe commit b08c96289e5239405bb22d646678911392fbe1f0 @andrewtrusty andrewtrusty committed Jun 2, 2013
Showing with 77 additions and 4 deletions.
  1. +5 −4 map.js
  2. +72 −0 server.py
View
@@ -1,5 +1,6 @@
var native_app = (window.location.href.indexOf('/Users/') !== -1 &&
- window.location.href.indexOf('/workspace/') !== -1);
+ window.location.href.indexOf('/workspace/') !== -1),
+ api_url = (native_app ? 'http://fallingfruit.org' : 'http://localhost:8080');
var FruitDrop = function() {
@@ -47,7 +48,7 @@ FruitDrop.prototype = {
$.ajax({
type: 'GET',
- url: 'http://fallingfruit.org/locations/markers.json',
+ url: api_url + '/locations/markers.json',
data: {
muni: 0,
nelat: bounds.getNorthEast().lat(),
@@ -56,7 +57,7 @@ FruitDrop.prototype = {
swlng: bounds.getSouthWest().lng()
},
/* -- for clusters --
- url: 'http://fallingfruit.org/locations/cluster.json',
+ url: api_url + '/locations/cluster.json',
data: {
method: 'grid',
grid: this._map.getZoom(),
@@ -113,7 +114,7 @@ FruitDrop.prototype = {
},
error: function() {
- alert('error!');
+ alert('error');
}
};
View
@@ -0,0 +1,72 @@
+#!/usr/bin/python
+
+import web
+import requests
+import os
+import sys
+
+
+urls = [
+ '/locations/.*', 'Proxy',
+ '/(.*)', 'Static'
+]
+
+
+class Proxy(object):
+ def GET(self):
+ path = web.ctx.fullpath
+
+ web.header('Content-Type', 'application/json')
+ url = 'http://fallingfruit.org' + path
+ r = requests.get(url)
+ return r.text
+
+
+class Static:
+ def GET(self, path):
+ types = {
+ "png":"image/png",
+ "jpg":"image/jpeg",
+ "jpeg":"image/jpeg",
+ "css":"text/css",
+ "gif":"image/gif",
+ "js":"text/javascript",
+ "html":"text/html; charset=utf-8"
+ }
+
+ base_dir = os.getcwd()
+
+ if path == '':
+ path = 'index.html'
+
+ if '?' in path:
+ path = path.split('?')[0]
+
+ folder = os.path.join(base_dir, os.path.dirname(path))
+ fn = os.path.basename(path)
+ full_path = os.path.join(folder, fn)
+
+ ext = None
+ if '.' in fn:
+ ext = fn.split('.')[-1].lower()
+
+ if '..' in path or ext not in types or fn not in os.listdir(folder):
+ raise web.notfound(full_path)
+
+ with open(full_path, 'rb') as fp:
+ web.header("Content-Type", types[ext])
+ return fp.read()
+
+
+def run():
+ web.config.debug = True
+
+ # specify ip/port to listen on via command line arguments
+ sys.argv = ['', '127.0.0.1:8080']
+
+ app = web.application(urls, globals(), autoreload=True)
+ app.run()
+
+
+if __name__ == '__main__':
+ run()

0 comments on commit b08c962

Please sign in to comment.