CalebCometML commited on
Commit
1184b99
·
1 Parent(s): 47b670a

Update router.py

Browse files
Files changed (1) hide show
  1. router.py +63 -8
router.py CHANGED
@@ -1,15 +1,70 @@
1
  import requests
2
- from flask import Flask
3
  import socket
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- app = Flask(__name__)
6
  hostname = socket.gethostname()
7
  ip_address = socket.gethostbyname(hostname)
8
 
9
- @app.route('/kangas')
10
- def kangas():
11
- return requests.get(f"http://{ip_address}:7640").content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- @app.route('/')
14
- def streamlit():
15
- return requests.get(f"http://{ip_address}:7840").content
 
1
  import requests
2
+ from flask import Flask, Response, request
3
  import socket
4
+ from pathlib import Path
5
+ import streamlit.web.bootstrap
6
+ from streamlit import config as _config
7
+ import kangas as kg
8
+
9
+ proj_dir = Path(__file__).parent
10
+ filename = proj_dir / "app.py"
11
+
12
+ _config.set_option("server.headless", True)
13
+ _config.set_option("server.port", 7840)
14
+ args = []
15
 
 
16
  hostname = socket.gethostname()
17
  ip_address = socket.gethostbyname(hostname)
18
 
19
+ application = Flask(__name__)
20
+
21
+ @application.route('/kangas', strict_slashes=False)
22
+ @application.route('/kangas/<path:path>')
23
+ def kangas_proxy(path=None):
24
+ query_string = request.query_string.decode('utf-8')
25
+
26
+ if path is None and len(query_string) < 1:
27
+ resp = requests.get(f'http://{ip_address}:7640/')
28
+ else:
29
+ if path is None:
30
+ path = ''
31
+ resp = requests.get(f'http://{ip_address}:7640/{str(path)}?{query_string}')
32
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
33
+ headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
34
+ response = Response(resp.content, resp.status_code, headers)
35
+ return response
36
+
37
+ @application.route('/api/<path:path>')
38
+ def api_proxy(path):
39
+ query_string = request.query_string.decode('utf-8')
40
+
41
+ resp = requests.get(f'http://{ip_address}:7640/api/{path}?{query_string}')
42
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
43
+ headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
44
+ response = Response(resp.content, resp.status_code, headers)
45
+ return response
46
+
47
+ @application.route('/_next/<path:path>')
48
+ def next_proxy(path=None):
49
+ application.logger.info("PATH: %s" % path)
50
+ resp = requests.get(f'http://{ip_address}:7640/_next/%s' % path)
51
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
52
+ headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
53
+ response = Response(resp.content, resp.status_code, headers)
54
+ return response
55
+
56
+
57
+ @application.route('/<path>.png')
58
+ @application.route('/<path>.ico')
59
+ def image_proxy(path=None):
60
+ application.logger.info(path)
61
+ resp = requests.get(f'http://{ip_address}:7640/%s.png' % path)
62
+ excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection']
63
+ headers = [(name, value) for (name, value) in resp.raw.headers.items() if name.lower() not in excluded_headers]
64
+ response = Response(resp.content, resp.status_code, headers)
65
+ application.logger.info(response)
66
+ return response
67
+
68
 
69
+ if __name__ == "__main__":
70
+ application.run(debug=True)