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.
This commit is contained in:
Whispering Wind
2025-05-17 23:27:17 +03:30
committed by GitHub
parent edcc4e32e2
commit d86087cf74
2 changed files with 29 additions and 7 deletions

View File

@ -1,6 +1,6 @@
from fastapi import APIRouter, HTTPException from fastapi import APIRouter, HTTPException
from ..schema.response import DetailResponse from ..schema.response import DetailResponse
from ..schema.config.normalsub import StartInputBody from ..schema.config.normalsub import StartInputBody, EditSubPathInputBody
import cli_api import cli_api
router = APIRouter() router = APIRouter()
@ -51,4 +51,27 @@ async def normal_sub_stop_api():
except Exception as e: except Exception as e:
raise HTTPException(status_code=400, detail=f'Error: {str(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)}')

View File

@ -1,9 +1,8 @@
from pydantic import BaseModel from pydantic import BaseModel, Field
# 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
class StartInputBody(BaseModel): class StartInputBody(BaseModel):
domain: str domain: str
port: int port: int
class EditSubPathInputBody(BaseModel):
subpath: str = Field(..., min_length=1, pattern=r"^[a-zA-Z0-9]+$", description="The new subpath, must be alphanumeric.")