|
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__": |
|
|
|
script_dir = os.path.dirname(os.path.abspath(__file__)) |
|
if script_dir not in sys.path: |
|
sys.path.insert(0, script_dir) |
|
|
|
|
|
server_process = subprocess.Popen( |
|
['uvicorn', 'main:app', '--host', '0.0.0.0', '--port', '8080'], |
|
cwd=script_dir |
|
) |
|
|
|
try: |
|
print("Server started. Will shutdown in 10 seconds.") |
|
time.sleep(10) |
|
finally: |
|
print("Shutting down server.") |
|
|
|
server_process.send_signal(signal.SIGINT) |
|
server_process.wait() |
|
|