43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
import socket, uvicorn, os
|
|
from fastapi import FastAPI
|
|
from api.openai_speech import router as speech_router
|
|
from api.voices import router as voice_router
|
|
from api.health import router as health_router
|
|
from pathlib import Path
|
|
|
|
app = FastAPI(title="XTTS2 Gateway")
|
|
|
|
app.include_router(speech_router, prefix="/v1/audio")
|
|
app.include_router(voice_router, prefix="/v1/voices")
|
|
app.include_router(health_router, prefix="/health")
|
|
|
|
PORT_FILE = Path("port.txt")
|
|
|
|
def find_free_port(start=8000, end=8100):
|
|
for port in range(start, end+1):
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
try:
|
|
s.bind(("0.0.0.0", port))
|
|
return port
|
|
except:
|
|
pass
|
|
raise RuntimeError("No free port")
|
|
|
|
def start():
|
|
env_port = os.environ.get("GATEWAY_PORT")
|
|
port = int(env_port) if env_port else 8000
|
|
|
|
# check
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
|
try:
|
|
s.bind(("0.0.0.0", port))
|
|
except:
|
|
port = find_free_port()
|
|
|
|
PORT_FILE.write_text(str(port))
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=port)
|
|
|
|
if __name__ == "__main__":
|
|
start()
|