File size: 963 Bytes
4cbbf07
1c11206
 
 
 
0660491
4cbbf07
 
 
0660491
4cbbf07
1c11206
 
 
 
0660491
 
 
 
 
1c11206
0660491
 
 
 
 
1c11206
 
 
 
 
 
 
0660491
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
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()