From d86087cf745863275e61f2ef26541408919bca91 Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Sat, 17 May 2025 23:27:17 +0330 Subject: [PATCH] feat: Add API endpoint to edit NormalSub subpath This commit introduces a new PUT endpoint `/api/v1/config/normalsub/edit_subpath` to allow modification of the NormalSub service's subpath. - Added `EditSubPathInputBody` Pydantic model for request validation. - Implemented the API endpoint in `normalsub.py` router, calling the corresponding `cli_api.edit_normalsub_subpath` function. - Includes error handling for validation and general exceptions. --- .../routers/api/v1/config/normalsub.py | 27 +++++++++++++++++-- .../routers/api/v1/schema/config/normalsub.py | 9 +++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/core/scripts/webpanel/routers/api/v1/config/normalsub.py b/core/scripts/webpanel/routers/api/v1/config/normalsub.py index 93e933f..3a97165 100644 --- a/core/scripts/webpanel/routers/api/v1/config/normalsub.py +++ b/core/scripts/webpanel/routers/api/v1/config/normalsub.py @@ -1,6 +1,6 @@ from fastapi import APIRouter, HTTPException from ..schema.response import DetailResponse -from ..schema.config.normalsub import StartInputBody +from ..schema.config.normalsub import StartInputBody, EditSubPathInputBody import cli_api router = APIRouter() @@ -51,4 +51,27 @@ async def normal_sub_stop_api(): except Exception as e: raise HTTPException(status_code=400, detail=f'Error: {str(e)}') -# TODO: Maybe would be nice to have a status endpoint + +@router.put('/edit_subpath', response_model=DetailResponse, summary='Edit NormalSub Subpath') +async def normal_sub_edit_subpath_api(body: EditSubPathInputBody): + """ + Edits the subpath for the NormalSub service. + + Args: + body (EditSubPathInputBody): The request body containing the new subpath. + + Returns: + DetailResponse: A response object containing a success message indicating + that the NormalSub subpath has been updated successfully. + + Raises: + HTTPException: If there is an error editing the NormalSub subpath, an + HTTPException with status code 400 and error details will be raised. + """ + try: + cli_api.edit_normalsub_subpath(body.subpath) + return DetailResponse(detail=f'Normalsub subpath updated to {body.subpath} successfully.') + except cli_api.InvalidInputError as e: + raise HTTPException(status_code=422, detail=f'Validation Error: {str(e)}') + except Exception as e: + raise HTTPException(status_code=400, detail=f'Error: {str(e)}') \ No newline at end of file diff --git a/core/scripts/webpanel/routers/api/v1/schema/config/normalsub.py b/core/scripts/webpanel/routers/api/v1/schema/config/normalsub.py index 10933a9..7f791bf 100644 --- a/core/scripts/webpanel/routers/api/v1/schema/config/normalsub.py +++ b/core/scripts/webpanel/routers/api/v1/schema/config/normalsub.py @@ -1,9 +1,8 @@ -from pydantic import BaseModel - -# The StartInputBody is the same as in /hysteria/core/scripts/webpanel/routers/api/v1/schema/config/singbox.py but for /normalsub endpoint -# I'm defining it separately because highly likely it'll be different - +from pydantic import BaseModel, Field class StartInputBody(BaseModel): domain: str port: int + +class EditSubPathInputBody(BaseModel): + subpath: str = Field(..., min_length=1, pattern=r"^[a-zA-Z0-9]+$", description="The new subpath, must be alphanumeric.") \ No newline at end of file