-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_mini.py
84 lines (67 loc) · 2.47 KB
/
run_mini.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import panel as pn
from dashboard import create_mini_dashboard
import os
from urllib.parse import urlparse
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(levelname)s: %(message)s'
)
logger = logging.getLogger(__name__)
def get_allowed_origins():
"""Get allowed origins for websocket connections."""
allowed_origins = set()
# Always include Digital Ocean app domain
allowed_origins.add('pyfinder-app-6r72q.ondigitalocean.app')
# Get other origins from environment variables
if os.getenv('APP_DOMAIN'):
allowed_origins.add(os.getenv('APP_DOMAIN'))
if os.getenv('BOKEH_ALLOW_WS_ORIGIN'):
origins = os.getenv('BOKEH_ALLOW_WS_ORIGIN').split(',')
allowed_origins.update(origin.strip() for origin in origins if origin.strip())
# Development origins
dev_origins = {'localhost:5006', '0.0.0.0:5006', '127.0.0.1:5006'}
allowed_origins.update(dev_origins)
# Clean origins
cleaned_origins = set()
for origin in allowed_origins:
if '://' in origin:
origin = origin.split('://', 1)[1]
origin = origin.split('/')[0]
cleaned_origins.add(origin)
logger.info("Allowed Origins:")
for origin in sorted(cleaned_origins):
logger.info(f" - {origin}")
return list(cleaned_origins)
def main():
# Initialize Panel
pn.extension(sizing_mode="stretch_width")
# Server configuration
port = int(os.getenv('PORT', '5006'))
host = os.getenv('HOST', '0.0.0.0')
allowed_origins = get_allowed_origins()
logger.info("Server Configuration:")
logger.info(f"Host: {host}")
logger.info(f"Port: {port}")
server_kwargs = {
'port': port,
'address': host,
'allow_websocket_origin': allowed_origins,
'show': False,
'websocket_max_message_size': int(os.getenv('BOKEH_WEBSOCKET_MAX_MESSAGE_SIZE', 20*1024*1024)),
'check_unused_sessions': 1000,
'keep_alive_milliseconds': 1000,
'num_procs': 1
}
try:
# Set Bokeh environment variable
os.environ['BOKEH_ALLOW_WS_ORIGIN'] = ','.join(allowed_origins)
# Start server with auto-config dashboard
logger.info("Starting Panel server...")
pn.serve(create_mini_dashboard(), **server_kwargs)
except Exception as e:
logger.error(f"Error starting server: {str(e)}")
raise
if __name__ == "__main__":
main()