From edf0c8e416a8052fac2778dadef0a3cf9399e71e Mon Sep 17 00:00:00 2001
From: Rolf Niepraschk <Rolf.Niepraschk@ptb.de>
Date: Wed, 11 Nov 2020 08:13:53 +0100
Subject: [PATCH] real hostname; compacting

---
 server.py | 54 +++++++++++++++++++++++++++---------------------------
 1 file changed, 27 insertions(+), 27 deletions(-)

diff --git a/server.py b/server.py
index 72b7fdd..dc85cf3 100644
--- a/server.py
+++ b/server.py
@@ -1,13 +1,12 @@
 
 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)
-- 
GitLab