-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathrun_tunnel.py
More file actions
60 lines (48 loc) · 1.67 KB
/
run_tunnel.py
File metadata and controls
60 lines (48 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import threading
from flask import Flask, request
from python.helpers import runtime, dotenv, process
from python.helpers.print_style import PrintStyle
from python.api.tunnel import Tunnel
# initialize the internal Flask server
app = Flask("app")
app.config["JSON_SORT_KEYS"] = False # Disable key sorting in jsonify
def run():
# Suppress only request logs but keep the startup messages
from werkzeug.serving import WSGIRequestHandler
from werkzeug.serving import make_server
PrintStyle().print("Starting tunnel server...")
class NoRequestLoggingWSGIRequestHandler(WSGIRequestHandler):
def log_request(self, code="-", size="-"):
pass # Override to suppress request logging
# Get configuration from environment
tunnel_api_port = runtime.get_tunnel_api_port()
host = (
runtime.get_arg("host") or dotenv.get_dotenv_value("WEB_UI_HOST") or "localhost"
)
server = None
lock = threading.Lock()
tunnel = Tunnel(app, lock)
# handle api request
@app.route("/", methods=["POST"])
async def handle_request():
return await tunnel.handle_request(request=request) # type: ignore
try:
server = make_server(
host=host,
port=tunnel_api_port,
app=app,
request_handler=NoRequestLoggingWSGIRequestHandler,
threaded=True,
)
process.set_server(server)
# server.log_startup()
server.serve_forever()
finally:
# Clean up tunnel if it was started
if tunnel:
tunnel.stop()
# run the internal server
if __name__ == "__main__":
runtime.initialize()
dotenv.load_dotenv()
run()