Files
thePlug/app/api/proxy/router.py
reloia f9e33e2847
Build and Push to Registry / build (push) Successful in 29s
refactor(proxy): move proxy logic to services.mitmproxy and update Docker service model
- Rename DockerService to DockerContainer and add folder attribute
- Move proxy-related modules (models, validator) to services.mitmproxy
- Update imports and references to new module locations
- Refactor Docker utilities to use DockerContainer and improve service discovery
- Add manager.py with create_proxy utility stub
- Initialize discovered Docker services at app startup and warn if none found
2026-04-13 00:24:20 +02:00

90 lines
2.5 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, status
from core.security import require_authenticated_user
from models.proxy import ProxyCreateInput, ProxyUpdateInput
from repositories.proxy import (
create_proxy,
delete_proxy,
get_proxy_by_port,
update_proxy,
)
from repositories.proxy import (
list_proxies as list_proxy_documents,
)
from services.mitmproxy.validator import ProxyCodeValidationError, validate_proxy_code
router = APIRouter(
prefix="/proxy",
tags=["Proxy"],
dependencies=[Depends(require_authenticated_user)],
)
async def _validate_code_or_raise(code: str) -> None:
try:
await validate_proxy_code(code)
except ProxyCodeValidationError as error:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
detail=error.to_http_detail(),
) from error
@router.get("")
async def list_proxies_handler():
return await list_proxy_documents()
@router.get("/{proxy_port}")
async def get_proxy(proxy_port: int):
proxy = await get_proxy_by_port(proxy_port)
if proxy is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Proxy not found",
)
return proxy
@router.post("", status_code=status.HTTP_201_CREATED)
async def create_proxy_handler(payload: ProxyCreateInput):
existing_proxy = await get_proxy_by_port(payload.proxy_port)
if existing_proxy is not None:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"Proxy already exists on port {payload.proxy_port}",
)
await _validate_code_or_raise(payload.code)
return await create_proxy(
proxy_port=payload.proxy_port,
name=payload.name,
service={"port": payload.service.port},
code=payload.code,
)
@router.put("/{proxy_port}")
async def edit_proxy(proxy_port: int, payload: ProxyUpdateInput):
if payload.code is not None:
await _validate_code_or_raise(payload.code)
updated_proxy = await update_proxy(proxy_port, payload)
if updated_proxy is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Proxy not found",
)
return updated_proxy
@router.delete("/{proxy_port}", status_code=status.HTTP_204_NO_CONTENT)
async def remove_proxy(proxy_port: int):
deleted = await delete_proxy(proxy_port)
if not deleted:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Proxy not found",
)