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
This commit is contained in:
2026-02-28 11:55:52 +08:00
parent e3b3a3aa98
commit aa70e7c0f0
5 changed files with 32 additions and 14 deletions

View File

@@ -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",

View File

@@ -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

23
frpc.go
View File

@@ -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))

View File

@@ -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))

View File

@@ -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
}
}