From 1f426f98e57bd91ae5f5e97157ab8e95feec3cf0 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Sat, 28 Feb 2026 11:55:52 +0800 Subject: [PATCH] refactor(api): simplify instance management endpoints - Change delete endpoint to use request body instead of path parameter - Modify endpoint now takes field as path parameter and instance name in body - Update README to reflect API changes - Remove unused description field from software info - Fix error message in auth token lookup --- README.md | 6 ++++-- auth.go | 2 +- frpc.go | 23 +++++++++++++++++++++-- main.go | 1 - router.go | 14 ++++++-------- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 68857c9..0c8d810 100644 --- a/README.md +++ b/README.md @@ -206,7 +206,7 @@ All API responses are returned in JSON format: ### 4. Delete frpc Instance -**Endpoint:** `/frpcAct/instanceMgr/{instanceName}/delete` +**Endpoint:** `/frpcAct/instanceMgr/delete` **Method:** POST **Content-Type:** application/json **Auth Required:** Yes (token) @@ -214,6 +214,7 @@ All API responses are returned in JSON format: **Request:** ```json { + "instanceName": "my_frpc", "token": "your_token", "timeStamp": 1704067200000 } @@ -234,7 +235,7 @@ All API responses are returned in JSON format: ### 5. Modify frpc Instance -**Endpoint:** `/frpcAct/instanceMgr/{instanceName}/modify/{field}` +**Endpoint:** `/frpcAct/instanceMgr/modify/{field}` **Method:** POST **Content-Type:** application/json **Auth Required:** Yes (token) @@ -244,6 +245,7 @@ You can modify multiple fields at once: **Request:** ```json { + "instanceName": "my_frpc", "token": "your_token", "timeStamp": 1704067200000, "name": "new_name", diff --git a/auth.go b/auth.go index 03bee20..cc50489 100644 --- a/auth.go +++ b/auth.go @@ -103,7 +103,7 @@ func GetTokenInfo(userID int) (*TokenInfo, error) { tokenInfo, exists := tokenMap[userID] if !exists { - return nil, fmt.Errorf("Token not found for userID %d: %s", userID, tokenMap[userID].Token) + return nil, fmt.Errorf("Token not found for userID %d", userID) } return tokenInfo, nil diff --git a/frpc.go b/frpc.go index 2b5deae..921e342 100644 --- a/frpc.go +++ b/frpc.go @@ -224,7 +224,7 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) { }) } -func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request, instanceName string) { +func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method") return @@ -238,6 +238,19 @@ func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request, instanceName } defer r.Body.Close() + var reqMap map[string]interface{} + if err := json.Unmarshal(body, &reqMap); err != nil { + postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to unmarshal request body: %v", err)) + SendErrorResponse(w, http.StatusBadRequest, "Invalid request format") + return + } + + instanceName := getStringFromMap(reqMap, "instanceName") + if instanceName == "" { + SendErrorResponse(w, http.StatusBadRequest, "instanceName is required") + return + } + userID, _, err := ValidateRequestWithBody(w, r, body) if err != nil { postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to validate request body: %v", err)) @@ -304,7 +317,7 @@ func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request, instanceName }) } -func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request, instanceName string) { +func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request, field string) { if r.Method != http.MethodPost { postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Invalid request method: %s", r.Method)) SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method") @@ -326,6 +339,12 @@ func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request, instanceName return } + instanceName := getStringFromMap(reqMap, "instanceName") + if instanceName == "" { + SendErrorResponse(w, http.StatusBadRequest, "instanceName is required") + return + } + userID, _, err := ValidateRequestWithBody(w, r, body) if err != nil { postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to validate request body: %v", err)) diff --git a/main.go b/main.go index 822034d..e48f6ad 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,6 @@ func main() { Version: "0.0.1", Developer: "Madobi Nanami", BuildVer: 1, - Description: "A backend application for managing local frpc instances, allowing users to easily start, stop, restart, and perform daily maintenance operations on frpc instances. It also provides automated error handling, such as automatic restart when an instance crashes.", BuildType: "debug", } postLog.Info(fmt.Sprintf("%s %s (Build %d.%s) by %s", softwareInfo.Name, softwareInfo.Version, softwareInfo.BuildVer, softwareInfo.BuildType, softwareInfo.Developer)) diff --git a/router.go b/router.go index 281ba88..1111ac3 100644 --- a/router.go +++ b/router.go @@ -42,18 +42,16 @@ func setupRoutes() { return } - if strings.HasSuffix(remainingPath, "/delete") { - instanceName := strings.TrimSuffix(remainingPath, "/delete") - instanceName = strings.Trim(instanceName, "/") - DeleteInstanceHandler(w, r, instanceName) + if remainingPath == "delete" { + DeleteInstanceHandler(w, r) return } - if strings.Contains(remainingPath, "/modify/") { - parts := strings.SplitN(remainingPath, "/modify/", 2) + if strings.HasPrefix(remainingPath, "modify/") { + parts := strings.SplitN(remainingPath, "/", 2) if len(parts) == 2 { - instanceName := strings.Trim(parts[0], "/") - ModifyInstanceHandler(w, r, instanceName) + field := parts[1] + ModifyInstanceHandler(w, r, field) return } }