61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
from fastapi import APIRouter, HTTPException, status
|
|
|
|
from models.proxy import ProxyCreateInput, ProxyUpdateInput
|
|
from utils.proxy.manager import (
|
|
create_proxy,
|
|
delete_proxy,
|
|
get_proxy_by_port,
|
|
list_proxies as list_proxy_documents,
|
|
update_proxy,
|
|
)
|
|
|
|
router = APIRouter(
|
|
prefix="/proxy",
|
|
tags=["Proxy"]
|
|
)
|
|
|
|
|
|
@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}",
|
|
)
|
|
|
|
return await create_proxy(
|
|
proxy_port=payload.proxy_port,
|
|
name=payload.name,
|
|
service={"address": payload.service.address, "port": payload.service.port},
|
|
code=payload.code,
|
|
)
|
|
|
|
|
|
@router.put("/{proxy_port}/")
|
|
async def edit_proxy(proxy_port: int, payload: ProxyUpdateInput):
|
|
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")
|