Skip to content
Snippets Groups Projects
Commit edf0c8e4 authored by Rolf Niepraschk's avatar Rolf Niepraschk
Browse files

real hostname; compacting

parent 58b17b45
No related branches found
No related tags found
No related merge requests found
from flask import Flask, Response, request, send_from_directory
from flask_cors import CORS
from flask_cors import CORS
from urllib.parse import urlparse
import requests, json
import re
app = Flask(__name__)
COUCHDB_PROTO = 'http'
COUCHDB_HOST = '127.0.0.1'
COUCHDB_PORT = '5984'
DB = 'vl_db'
......@@ -16,7 +15,6 @@ app.url_map.strict_slashes = False
CORS(app)
## =================== URLs to the Web Apps ======================
@app.route('/<fn>', methods=['GET'])
def html(fn):
app.logger.debug('hit html folder: ' + fn)
......@@ -41,17 +39,32 @@ def img(fn):
def lib(fn):
app.logger.debug('hit lib folder: ' + fn)
return send_from_directory('data/lib', fn)
'''
The following works like a proxy server:
A flask server URL of the form
"http://127.0.0.1:5000/5984/vl_db/000_SERVERS"
will be forwarded to the real CouchDB URL
"http://127.0.0.1:5984/vl_db/000_SERVERS"
## ------------------------------------
## CouchDB Proxy
## from: http://host/5984/foo/bar
## to: http://127.0.0.1:5984/foo/bar
## ------------------------------------
# TODO: evtl besser make_response(...)?
def couchdb_proxy_1(path):
new_url = '{}://{}:{}/{}'.format(COUCHDB_PROTO, COUCHDB_HOST, \
COUCHDB_PORT, path)
app.logger.debug('redirect to: ' + new_url)
see:
https://stackoverflow.com/questions/6656363/proxying-to-another-web-service-with-flask
'''
@app.route('/{}/'.format(COUCHDB_PORT), \
methods=['GET','PUT','POST','HEAD','DELETE','OPTIONS'])
@app.route('/{}/<path:p>'.format(COUCHDB_PORT),
methods=['GET','PUSH','POST','DELETE'])
def couchdb_proxy(p=''):
q = request.query_string.decode();
if q:
path = p + '?' + q
else:
path = p
host = urlparse(request.base_url).hostname
new_url = '{}://{}:{}/{}'.format(COUCHDB_PROTO, host, COUCHDB_PORT, path)
app.logger.debug('[old couchdb url] ' + request.url)
app.logger.debug('[new couchdb url] ' + new_url)
resp = requests.request(
method=request.method,
url=new_url,
......@@ -65,19 +78,6 @@ def couchdb_proxy_1(path):
if name.lower() not in excluded_headers]
response = Response(resp.content, resp.status_code, headers)
return response
@app.route('/{}/'.format(COUCHDB_PORT), \
methods=['GET','PUT','POST','HEAD','DELETE','OPTIONS'])
@app.route('/{}/<path:path>'.format(COUCHDB_PORT),
methods=['GET','PUSH','POST','DELETE'])
def couchdb_proxy(path=''):
app.logger.debug('original request: ' + request.url)
q = request.query_string.decode();
if q:
p = path + '?' + q
else:
p = path
return couchdb_proxy_1(p);
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment