- Introduce async `change_port` to update service port and reload docker compose - Add `run_docker_command` for running docker compose commands asynchronously - Implement `_get_random_port` utility for selecting available ports - Refactor service discovery and port change logic to use new async methods - Update proxy and manager logic to support new port change workflow
65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
import os
|
|
import sys
|
|
from contextlib import asynccontextmanager
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
|
|
from api.auth.router import router as auth_router
|
|
from api.proxy.router import router as proxy_router
|
|
from core.config import settings
|
|
from core.mongodb import close_mongodb, init_mongodb
|
|
from core.redis import close_redis, init_redis
|
|
from core.security import hash_password
|
|
from integrations.docker import get_all_services_in_dir
|
|
from repositories.auth import ensure_auth_indexes, ensure_single_account
|
|
from repositories.proxy import ensure_proxy_indexes
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(_: FastAPI):
|
|
await init_mongodb()
|
|
await init_redis()
|
|
await ensure_auth_indexes()
|
|
await ensure_single_account(
|
|
username=settings.AUTH_USERNAME,
|
|
password_hash=hash_password(settings.AUTH_PASSWORD),
|
|
)
|
|
await ensure_proxy_indexes()
|
|
|
|
home_dir = str(Path.home())
|
|
app.state.services = get_all_services_in_dir(home_dir) or []
|
|
if not app.state.services:
|
|
warning = (
|
|
"\n" + "!" * 60 + "\n"
|
|
" WARNING: No services found in home directory!\n"
|
|
f" Scanned: {home_dir}\n"
|
|
+ "!" * 60 + "\n"
|
|
)
|
|
print(warning, file=sys.stderr)
|
|
|
|
try:
|
|
yield
|
|
finally:
|
|
await close_redis()
|
|
await close_mongodb()
|
|
|
|
|
|
DEBUG = os.getenv("DEBUG", "false").lower() == "true"
|
|
|
|
app = FastAPI(
|
|
title="thePlugAPI",
|
|
lifespan=lifespan,
|
|
docs_url="/docs" if DEBUG else None,
|
|
redoc_url="/redoc" if DEBUG else None,
|
|
openapi_url="/openapi.json" if DEBUG else None,
|
|
)
|
|
|
|
app.include_router(auth_router)
|
|
app.include_router(proxy_router)
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {"Hello": "World!"}
|