diff --git a/server.py b/server.py index 75ba294afbf4085456d9a13cef134633b4c8c2ac..cddf73a60b43dae28e2bef67d4db7289868e780b 100644 --- a/server.py +++ b/server.py @@ -6,10 +6,7 @@ import sys, requests app = Flask(__name__) -VERSION = '1.0.0' - -COUCHDB_PROTO = 'http' -COUCHDB_PORT = '5984' +VERSION = '1.1.0' FLASK_RUN_PORT = sys.argv[1] DATA_PATH = sys.argv[2] @@ -38,24 +35,27 @@ def x(p1, p2=''): The following works like a proxy server: A flask server URL of the form - "http://127.0.0.1:8081/5984/vl_db/000_SERVERS" + "http://127.0.0.1:8081/xxxx/vl_db/000_SERVERS" will be forwarded to the real CouchDB URL - "http://127.0.0.1:5984/vl_db/000_SERVERS" + "http://127.0.0.1:xxxx/vl_db/000_SERVERS" + +The part 'xxxx' can be any integer as a future port address for couchdb. see: https://stackoverflow.com/questions/6656363/proxying-to-another-web-service-with-flask ''' -@app.route('/{}/'.format(COUCHDB_PORT), \ +@app.route('/<int:real_port>/', \ methods=['GET','PUT','POST','HEAD','DELETE','OPTIONS']) -@app.route('/{}/<path:p>'.format(COUCHDB_PORT), +@app.route('/<int:real_port>/<path:p>', methods=['GET','PUT','POST','HEAD','DELETE','OPTIONS']) -def couchdb_proxy(p=''): +def couchdb_proxy(real_port, p=''): q = request.query_string.decode(); path = p if q: path = p + '?' + q host = urlparse(request.base_url).hostname - new_url = '{}://{}:{}/{}'.format(COUCHDB_PROTO, host, COUCHDB_PORT, path) + scheme = urlparse(request.base_url).scheme + new_url = '{}://{}:{}/{}'.format(scheme, host, real_port, path) app.logger.debug('[old couchdb url] ' + request.url) app.logger.debug('[new couchdb url] ' + new_url) resp = requests.request(