Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,46 @@
CONFIG_PATH = "config/config.yaml"

class ConfigUpdate(BaseModel):
"""
Pydantic model for updating configuration sections.

Attributes:
section (str): The name of the configuration section to update (e.g., "translation_settings").
data (Dict): A dictionary containing the key-value pairs to update within that section.
"""
section: str
data: Dict

@app.get("/config")
def get_config():
"""
Retrieves the current application configuration from config/config.yaml.

Raises:
HTTPException: If the config file is not found (status_code=404).

Returns:
Dict: The loaded configuration as a dictionary.
"""
if not os.path.exists(CONFIG_PATH):
raise HTTPException(status_code=404, detail="Config file not found")
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
return yaml.safe_load(f)

@app.post("/config")
def update_config(update: ConfigUpdate):
"""
Updates a specific section of the application configuration.

If the section exists, its data is updated. If the section does not exist,
it is created. The updated configuration is then saved back to config/config.yaml.

Args:
update (ConfigUpdate): An object containing the section to update and the data.

Returns:
Dict: A status message indicating success.
"""
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
config = yaml.safe_load(f)

Expand All @@ -50,24 +78,54 @@ def update_config(update: ConfigUpdate):

# WebSocket để truyền progress
class ConnectionManager:
"""
Manages active WebSocket connections for broadcasting progress updates.
"""
def __init__(self):
self.active_connections: List[WebSocket] = []

async def connect(self, websocket: WebSocket):
"""
Establishes a new WebSocket connection and adds it to the active connections.

Args:
websocket (WebSocket): The WebSocket object for the new connection.
"""
await websocket.accept()
self.active_connections.append(websocket)

def disconnect(self, websocket: WebSocket):
"""
Removes a WebSocket connection from the active connections.

Args:
websocket (WebSocket): The WebSocket object to disconnect.
"""
self.active_connections.remove(websocket)

async def broadcast(self, message: str):
"""
Broadcasts a message to all active WebSocket connections.

Args:
message (str): The message to be sent to all connected clients.
"""
for connection in self.active_connections:
await connection.send_text(message)

manager = ConnectionManager()

@app.websocket("/ws/progress")
async def websocket_endpoint(websocket: WebSocket):
"""
WebSocket endpoint for real-time progress updates.

Clients can connect to this endpoint to receive broadcast messages
about the application's progress (e.g., translation status).

Args:
websocket (WebSocket): The incoming WebSocket connection.
"""
await manager.connect(websocket)
try:
while True:
Expand Down