-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.py
75 lines (64 loc) · 2.34 KB
/
run.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
import subprocess
import signal
import os
import time
import http.server
import ssl
from threading import Thread
# Function to run the frontend HTTPS server
def run_frontend():
# Change directory to the frontend build folder
os.chdir('./frontend/build')
# Define the handler and server
handler = http.server.SimpleHTTPRequestHandler
httpd = http.server.HTTPServer(('0.0.0.0', 443), handler)
# Wrap the server socket with SSL
try:
httpd.socket = ssl.wrap_socket(
httpd.socket,
certfile='/etc/letsencrypt/live/debategpt.emileamaj.xyz/fullchain.pem',
keyfile='/etc/letsencrypt/live/debategpt.emileamaj.xyz/privkey.pem',
server_side=True
)
print("Frontend HTTPS server running on port 443")
httpd.serve_forever()
except Exception as e:
print(f"Failed to start HTTPS server: {e}")
os._exit(1)
# Commands to start the backend server
backend_cmd = (
"uvicorn main:app "
"--host 0.0.0.0 "
"--port 8000 "
"--ssl-keyfile=/etc/letsencrypt/live/debategpt.emileamaj.xyz/privkey.pem "
"--ssl-certfile=/etc/letsencrypt/live/debategpt.emileamaj.xyz/fullchain.pem"
)
# Function to run the backend server
def run_backend():
try:
print("Starting backend server...")
subprocess.run(backend_cmd, shell=True, cwd='./backend', check=True)
except subprocess.CalledProcessError as e:
print(f"Backend server failed: {e}")
os._exit(1)
# Start the frontend server in a separate thread
frontend_thread = Thread(target=run_frontend, daemon=True)
frontend_thread.start()
# Start the backend server in the main thread
backend_process = subprocess.Popen(backend_cmd, shell=True, cwd='./backend')
# Create a list to hold all the spawned processes
processes = [backend_process]
# Function to handle SIGTERM and SIGINT signals
def signal_handler(sig, frame):
print("Shutting down servers...")
for process in processes:
if process.poll() is None: # Check if process is still running
process.terminate() # If it is, terminate it
os._exit(0) # Exit the script
# Register the signal handler
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
# Keep the script running
print("Servers started. Press Ctrl+C to shut down.")
while True:
time.sleep(1)