ready to write UI
This commit is contained in:
66
UI/gunicorn.conf.py
Normal file
66
UI/gunicorn.conf.py
Normal file
@ -0,0 +1,66 @@
|
||||
import os
|
||||
import subprocess
|
||||
import signal
|
||||
import atexit
|
||||
from flask import Flask, render_template
|
||||
from dotenv import load_dotenv
|
||||
import socket
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
# Get configuration from environment variables
|
||||
backend_ip = os.getenv('BACKEND_IP')
|
||||
backend_port = os.getenv('BACKEND_PORT')
|
||||
backend_log_level = os.getenv('BACKEND_LOG_LEVEL')
|
||||
backend_data_directory = os.getenv('BACKEND_DATA_DIRECTORY')
|
||||
backend_log_file = os.getenv('BACKEND_LOG_FILE')
|
||||
backend_path = os.getenv('BACKEND_PATH')
|
||||
|
||||
# Command to start the backend process
|
||||
backend_command = [
|
||||
backend_path,
|
||||
"-d", backend_data_directory,
|
||||
"-l", backend_log_file,
|
||||
"--level", backend_log_level,
|
||||
"server",
|
||||
"--port", backend_port,
|
||||
"--address", backend_ip
|
||||
]
|
||||
|
||||
backend_process = None
|
||||
|
||||
def start_backend():
|
||||
global backend_process
|
||||
# Try to acquire the lock without blocking, timeout if another process holds the lock
|
||||
print("Starting backend process...")
|
||||
backend_process = subprocess.Popen(backend_command, preexec_fn = os.setsid)
|
||||
print(f"Backend process started with PID: {backend_process.pid}")
|
||||
def on_starting(server):
|
||||
# 这里放置启动后台进程的逻辑
|
||||
start_backend()
|
||||
|
||||
def when_ready(server):
|
||||
print("Server is ready. Spawning workers")
|
||||
def send_exit_command(ip, port):
|
||||
try:
|
||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
|
||||
sock.connect((ip, int(port)))
|
||||
sock.sendall(b'exit\n')
|
||||
response = sock.recv(1024) # Optional: read response from server
|
||||
print(f"Received response: {response.decode('utf-8')}")
|
||||
except Exception as e:
|
||||
print(f"Failed to send exit command: {e}")
|
||||
|
||||
def stop_backend():
|
||||
global backend_process
|
||||
if backend_process is not None:
|
||||
print(f"Sending exit command to backend process at {backend_ip}:{backend_port}...")
|
||||
send_exit_command(backend_ip, backend_port)
|
||||
print(f"Waiting for backend process with PID: {backend_process.pid} to terminate...")
|
||||
backend_process.wait()
|
||||
print("Backend process stopped.")
|
||||
backend_process = None
|
||||
|
||||
def on_exit(server):
|
||||
stop_backend()
|
63
UI/main.py
63
UI/main.py
@ -0,0 +1,63 @@
|
||||
import os
|
||||
import subprocess
|
||||
import signal
|
||||
import atexit
|
||||
from flask import Flask, render_template
|
||||
from dotenv import load_dotenv
|
||||
import time
|
||||
import socket
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Get configuration from environment variables
|
||||
backend_ip = os.getenv('BACKEND_IP')
|
||||
backend_port = os.getenv('BACKEND_PORT')
|
||||
backend_log_level = os.getenv('BACKEND_LOG_LEVEL')
|
||||
backend_data_directory = os.getenv('BACKEND_DATA_DIRECTORY')
|
||||
backend_log_file = os.getenv('BACKEND_LOG_FILE')
|
||||
backend_path = os.getenv('BACKEND_PATH')
|
||||
|
||||
# Command to start the backend process
|
||||
backend_command = [
|
||||
backend_path,
|
||||
"-d", backend_data_directory,
|
||||
"-l", backend_log_file,
|
||||
"--level", backend_log_level,
|
||||
"server",
|
||||
"--port", backend_port,
|
||||
"--address", backend_ip
|
||||
]
|
||||
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((backend_ip, int(backend_port)))
|
||||
def ExecCommand(command):
|
||||
command=command+'\n'
|
||||
sock.sendall(command.encode('utf-8'))
|
||||
response = sock.recv(1024*1024)
|
||||
response= response.decode('utf-8')
|
||||
_, _, response = response.partition(" ")
|
||||
return response
|
||||
|
||||
# Signal handler for graceful shutdown
|
||||
def signal_handler(signum, frame):
|
||||
# stop_backend()
|
||||
time.sleep(10)
|
||||
exit(0)
|
||||
|
||||
# Register signal handlers
|
||||
signal.signal(signal.SIGINT, signal_handler)
|
||||
signal.signal(signal.SIGTERM, signal_handler)
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route('/about')
|
||||
def about():
|
||||
return render_template('about.html')
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
|
8
UI/templates/about.html
Normal file
8
UI/templates/about.html
Normal file
@ -0,0 +1,8 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}About Us - Train Ticket Booking{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>About Us</h1>
|
||||
<p>This is a demo site for booking train tickets.</p>
|
||||
{% endblock %}
|
27
UI/templates/base.html
Normal file
27
UI/templates/base.html
Normal file
@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{% block title %}Train Ticket Booking{% endblock %}</title>
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-light">
|
||||
<a class="navbar-brand" href="#">Train Tickets</a>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item {% if request.endpoint == 'home' %}active{% endif %}">
|
||||
<a class="nav-link" href="/">Home</a>
|
||||
</li>
|
||||
<li class="nav-item {% if request.endpoint == 'about' %}active{% endif %}">
|
||||
<a class="nav-link" href="/about">About</a>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</nav>
|
||||
<div class="container mt-5">
|
||||
{% block content %}{% endblock %}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
8
UI/templates/index.html
Normal file
8
UI/templates/index.html
Normal file
@ -0,0 +1,8 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block title %}Home - Train Ticket Booking{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1>Welcome to the Train Ticket Booking System</h1>
|
||||
<p>Use this site to book your train tickets easily.</p>
|
||||
{% endblock %}
|
Reference in New Issue
Block a user