diff --git a/features/dbrest/dbrest.go b/features/dbrest/dbrest.go index 609c9e16..d3d74fc6 100644 --- a/features/dbrest/dbrest.go +++ b/features/dbrest/dbrest.go @@ -9,6 +9,7 @@ import ( "strings" "time" + "ems.agt/lib/core/utils/ctx" "ems.agt/lib/dborm" "ems.agt/lib/global" "ems.agt/lib/log" @@ -692,7 +693,7 @@ func DbStop(w http.ResponseWriter, r *http.Request) { var bodyArgs struct { ID string `json:"ID" validate:"required"` } - err := services.ShouldBindJSON(r, &bodyArgs) + err := ctx.ShouldBindJSON(r, &bodyArgs) if err != nil { log.Error("io.ReadAll is failed:", err) services.ResponseErrorWithJson(w, 400, err.Error()) diff --git a/features/file/file.go b/features/file/file.go index 31628403..815dacaa 100644 --- a/features/file/file.go +++ b/features/file/file.go @@ -5,6 +5,7 @@ import ( "net/http" "path/filepath" + "ems.agt/lib/core/utils/ctx" "ems.agt/lib/dborm" "ems.agt/lib/file" "ems.agt/lib/log" @@ -178,7 +179,7 @@ func DiskList(w http.ResponseWriter, r *http.Request) { func ListFiles(w http.ResponseWriter, r *http.Request) { // json 請求參數獲取 var bodyArgs FileOption - err := services.ShouldBindJSON(r, &bodyArgs) + err := ctx.ShouldBindJSON(r, &bodyArgs) if err != nil || dborm.DbClient.XEngine == nil { services.ResponseErrorWithJson(w, 400, err.Error()) return diff --git a/features/maintenance/maintenance.go b/features/maintenance/maintenance.go index 489d7475..e297671e 100644 --- a/features/maintenance/maintenance.go +++ b/features/maintenance/maintenance.go @@ -10,6 +10,7 @@ import ( "runtime" "time" + "ems.agt/lib/core/utils/ctx" "ems.agt/lib/dborm" "ems.agt/lib/log" "ems.agt/lib/services" @@ -169,16 +170,6 @@ func Pref(w http.ResponseWriter, r *http.Request) { services.ResponseWithJson(w, http.StatusOK, data) } -// GET http://192.168.21.183:3040/api/rest/maintenance/v1/prefLog?pageNum=1&pageSize=10 -func PrefLog(w http.ResponseWriter, r *http.Request) { - pageNumb, pageSize := services.GetPageNumSize(r) - result, err := dborm.DbClient.XEngine.QueryString("SELECT * FROM sys_perf_data limit ?,?", (pageNumb-1)*pageSize, pageSize) - if err != nil { - services.ResponseInternalServerError500ProcessError(w, err) - } - services.ResponseWithJson(w, http.StatusOK, result) -} - // POST http://192.168.21.183:3040/api/rest/maintenance/v1/config func Config(w http.ResponseWriter, r *http.Request) { // json 請求參數獲取 @@ -186,7 +177,7 @@ func Config(w http.ResponseWriter, r *http.Request) { Key string `json:"key"` Value string `json:"value"` } - err := services.ShouldBindJSON(r, &bodyArgs) + err := ctx.ShouldBindJSON(r, &bodyArgs) if err != nil { log.Error("io.ReadAll is failed:", err) services.ResponseNotFound404UriNotExist(w, r) @@ -292,7 +283,7 @@ func TopOps(w http.ResponseWriter, r *http.Request) { Ops string `json:"ops"` Pid string `json:"pid"` } - err := services.ShouldBindJSON(r, &bodyArgs) + err := ctx.ShouldBindJSON(r, &bodyArgs) if err != nil { log.Error("io.ReadAll is failed:", err) services.ResponseNotFound404UriNotExist(w, r) diff --git a/features/monitor/monitor/monitor.go b/features/monitor/monitor/monitor.go index 31d150f5..7ce950dc 100644 --- a/features/monitor/monitor/monitor.go +++ b/features/monitor/monitor/monitor.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "ems.agt/lib/core/utils/ctx" "ems.agt/lib/dborm" "ems.agt/lib/services" "ems.agt/restagent/config" @@ -89,7 +90,7 @@ func Iooptions(w http.ResponseWriter, r *http.Request) { func LoadMonitor(w http.ResponseWriter, r *http.Request) { // json 請求參數獲取 var bodyArgs MonitorSearch - err := services.ShouldBindJSON(r, &bodyArgs) + err := ctx.ShouldBindJSON(r, &bodyArgs) if err != nil || dborm.DbClient.XEngine == nil { services.ResponseErrorWithJson(w, 400, err.Error()) return diff --git a/features/monitor/psnet/psnet.go b/features/monitor/psnet/psnet.go index 3dfa56b0..78877bcc 100644 --- a/features/monitor/psnet/psnet.go +++ b/features/monitor/psnet/psnet.go @@ -6,6 +6,7 @@ import ( "net/http" "time" + "ems.agt/lib/core/utils/ctx" "ems.agt/lib/log" "ems.agt/lib/services" "ems.agt/lib/wsinfo" @@ -48,7 +49,7 @@ func StopProcess(w http.ResponseWriter, r *http.Request) { var bodyArgs struct { PID int32 `json:"PID" validate:"required"` } - err := services.ShouldBindJSON(r, &bodyArgs) + err := ctx.ShouldBindJSON(r, &bodyArgs) if err != nil { log.Error("io.ReadAll is failed:", err) services.ResponseErrorWithJson(w, 400, err.Error()) @@ -74,7 +75,7 @@ func Ping(w http.ResponseWriter, r *http.Request) { Host string `json:"host" validate:"required"` Port string `json:"port" validate:"required"` } - err := services.ShouldBindJSON(r, &bodyArgs) + err := ctx.ShouldBindJSON(r, &bodyArgs) if err != nil { log.Error("io.ReadAll is failed:", err) services.ResponseErrorWithJson(w, 400, err.Error()) diff --git a/lib/services/requset.go b/lib/services/requset.go deleted file mode 100644 index 8fa8e00c..00000000 --- a/lib/services/requset.go +++ /dev/null @@ -1,45 +0,0 @@ -package services - -import ( - "encoding/json" - "io" - "net/http" - "strconv" - - "ems.agt/lib/global" -) - -// 读取json请求结构团体 -func ShouldBindJSON(r *http.Request, args any) error { - body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen)) - if err != nil { - return err - } - err = json.Unmarshal(body, args) - return err -} - -// Get 查詢分頁值 -func GetPageNumSize(r *http.Request) (int, int) { - pageNumStr := r.URL.Query().Get("pageNum") - num := 1 - if v, err := strconv.Atoi(pageNumStr); err == nil && v > 0 { - if v >= 100 { - num = 100 - } else { - num = v - } - } - - pageSizeStr := r.URL.Query().Get("pageSize") - size := 0 - if v, err := strconv.Atoi(pageSizeStr); err == nil && v > 0 { - if v >= 60 { - size = 60 - } else { - size = v - } - } - - return num, size -}