Skip to content

Commit 22f541c

Browse files
authored
feat: add alias field to Master and update handleInfo to manage alias via POST request
1 parent 87248f4 commit 22f541c

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

internal/master.go

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const swaggerUIHTML = `<!DOCTYPE html>
6767
// Master 实现主控模式功能
6868
type Master struct {
6969
Common // 继承通用功能
70+
alias string // 主控别名
7071
prefix string // API前缀
7172
version string // NP版本
7273
hostname string // 隧道名称
@@ -677,12 +678,33 @@ func (m *Master) handleSwaggerUI(w http.ResponseWriter, r *http.Request) {
677678

678679
// handleInfo 处理系统信息请求
679680
func (m *Master) handleInfo(w http.ResponseWriter, r *http.Request) {
680-
if r.Method != http.MethodGet {
681+
switch r.Method {
682+
case http.MethodGet:
683+
writeJSON(w, http.StatusOK, m.getMasterInfo())
684+
685+
case http.MethodPost:
686+
var reqData struct {
687+
Alias string `json:"alias"`
688+
}
689+
if err := json.NewDecoder(r.Body).Decode(&reqData); err != nil {
690+
httpError(w, "Invalid request body", http.StatusBadRequest)
691+
return
692+
}
693+
694+
// 更新alias字段
695+
m.alias = reqData.Alias
696+
697+
writeJSON(w, http.StatusOK, m.getMasterInfo())
698+
699+
default:
681700
httpError(w, "Method not allowed", http.StatusMethodNotAllowed)
682-
return
683701
}
702+
}
684703

704+
// getMasterInfo 获取完整的主控信息
705+
func (m *Master) getMasterInfo() map[string]any {
685706
info := map[string]any{
707+
"alias": m.alias,
686708
"os": runtime.GOOS,
687709
"arch": runtime.GOARCH,
688710
"cpu": -1,
@@ -718,7 +740,7 @@ func (m *Master) handleInfo(w http.ResponseWriter, r *http.Request) {
718740
info["sysup"] = sysInfo.SysUp
719741
}
720742

721-
writeJSON(w, http.StatusOK, info)
743+
return info
722744
}
723745

724746
// getLinuxSysInfo 获取Linux系统信息
@@ -1654,6 +1676,17 @@ func generateOpenAPISpec() string {
16541676
"401": {"description": "Unauthorized"},
16551677
"405": {"description": "Method not allowed"}
16561678
}
1679+
},
1680+
"post": {
1681+
"summary": "Update master alias",
1682+
"security": [{"ApiKeyAuth": []}],
1683+
"requestBody": {"required": true, "content": {"application/json": {"schema": {"$ref": "#/components/schemas/UpdateMasterAliasRequest"}}}},
1684+
"responses": {
1685+
"200": {"description": "Success", "content": {"application/json": {"schema": {"$ref": "#/components/schemas/MasterInfo"}}}},
1686+
"400": {"description": "Invalid input"},
1687+
"401": {"description": "Unauthorized"},
1688+
"405": {"description": "Method not allowed"}
1689+
}
16571690
}
16581691
},
16591692
"/tcping": {
@@ -1747,6 +1780,7 @@ func generateOpenAPISpec() string {
17471780
"MasterInfo": {
17481781
"type": "object",
17491782
"properties": {
1783+
"alias": {"type": "string", "description": "Master alias"},
17501784
"os": {"type": "string", "description": "Operating system"},
17511785
"arch": {"type": "string", "description": "System architecture"},
17521786
"cpu": {"type": "integer", "description": "CPU usage percentage"},
@@ -1768,6 +1802,11 @@ func generateOpenAPISpec() string {
17681802
"key": {"type": "string", "description": "Private key path"}
17691803
}
17701804
},
1805+
"UpdateMasterAliasRequest": {
1806+
"type": "object",
1807+
"required": ["alias"],
1808+
"properties": {"alias": {"type": "string", "description": "Master alias"}}
1809+
},
17711810
"TCPingResult": {
17721811
"type": "object",
17731812
"properties": {

0 commit comments

Comments
 (0)