-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (68 loc) · 2.62 KB
/
main.py
File metadata and controls
83 lines (68 loc) · 2.62 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
import asyncio
import socket
import argparse
from pathlib import Path
import warnings
from ui.main_ui import create_ui_router
import uvicorn
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from nicegui import ui
from backend import CryoBoostBackend
import logging
import sys
sys.dont_write_bytecode = True
class SuppressPruneStorageError(logging.Filter):
def filter(self, record):
return "Request is not set" not in record.getMessage()
warnings.filterwarnings("ignore", message="Pydantic serializer warnings")
logging.getLogger("nicegui").addFilter(SuppressPruneStorageError())
logging.basicConfig(level=logging.DEBUG)
asyncio.get_event_loop().set_debug(True)
def setup_app():
"""Configures and returns the FastAPI app."""
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
ui.add_head_html('''
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/static/main.css">
''')
backend = CryoBoostBackend(Path.cwd())
create_ui_router(backend)
ui.run_with(app, title="CryoBoost Server", storage_secret="somestuff")
return app
def get_local_ip():
"""Get the local IP address"""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
return "localhost"
if __name__ in {"__main__", "__mp_main__"}:
parser = argparse.ArgumentParser(description='CryoBoost Server')
parser.add_argument('--port', type=int, default=8081, help='Port to run server on')
parser.add_argument('--host', type=str, default='0.0.0.0', help='Host to bind to')
args = parser.parse_args()
app = setup_app()
local_ip = get_local_ip()
hostname = socket.gethostname()
print("\n" + "="*60)
print("CryoBoost Server Starting")
print(f"Access URLs:")
print(f" Local: http://localhost:{args.port}")
print(f" Network: http://{local_ip}:{args.port}")
print("\nTo access in the browser from your local machine, establish port-forwarding from remote to your local terminal.")
print("\nRun this in a local terminal:")
print(f"ssh -f -N -L {args.port}:localhost:{args.port} $USER@{hostname}")
print("="*60 + "\n")
uvicorn.run(
app,
host=args.host,
port=args.port,
)