code / main.py
atonyxu
code
0660491
raw
history blame
963 Bytes
from fastapi import FastAPI
import subprocess
import time
import os
import signal
import sys
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Hello, World!"}
if __name__ == "__main__":
# Add the current directory to the Python path so that 'main' module can be found
script_dir = os.path.dirname(os.path.abspath(__file__))
if script_dir not in sys.path:
sys.path.insert(0, script_dir)
# Start the server in a separate process
server_process = subprocess.Popen(
['uvicorn', 'main:app', '--host', '0.0.0.0', '--port', '8080'],
cwd=script_dir # Set the working directory for the subprocess
)
try:
print("Server started. Will shutdown in 10 seconds.")
time.sleep(10) # Wait for 10 seconds
finally:
print("Shutting down server.")
# Gracefully shut down the server
server_process.send_signal(signal.SIGINT)
server_process.wait()