package main import ( "encoding/json" "fmt" "html/template" "io" "net/http" "path/filepath" "strconv" "cnphper.com/model" ) type SyscfgRedisListRet struct { Errno int `json:"errno"` Error string `json:"error"` Data []*model.RedisCfgRow `json:"data"` } type SyscfgRedisGetRet struct { Errno int `json:"errno"` Error string `json:"error"` Data *model.RedisCfgRow `json:"data"` } type SyscfgRedisAddRet struct { Errno int `json:"errno"` Error string `json:"error"` Data int64 `json:"data"` } func syscfg_redis(resp http.ResponseWriter, req *http.Request) { sess, ok := checkLogin(resp, req) if !ok { return } //视图输出 files := []string{ filepath.Join(Cfg.TmplDir, "syscfg", "redis.tmpl"), filepath.Join(Cfg.TmplDir, "header.tmpl"), filepath.Join(Cfg.TmplDir, "navbar.tmpl"), } tmpl, err := template.New("redis.tmpl").Funcs(TmplFuncMap).ParseFiles(files...) if err != nil { io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error())) } else { tmpl.Execute(resp, struct { Sess *Session Req *http.Request Title string }{ sess, req, "Redis列表管理", }) } } func syscfg_redis_list(resp http.ResponseWriter, req *http.Request) { _, ok := checkLogin(resp, req) if !ok { return } req.ParseForm() mdlRedisCfg := model.NewRedisCfg(Db) rows, err := mdlRedisCfg.GetAll(-1) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 1, Error: err.Error()}) resp.Write(json) } else { json, _ := json.Marshal(SyscfgRedisListRet{Errno: 0, Error: "", Data: rows}) resp.Write(json) } } func syscfg_redis_get(resp http.ResponseWriter, req *http.Request) { _, ok := checkLogin(resp, req) if !ok { return } req.ParseForm() idStr := req.Form.Get("id") if idStr == "" { json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"}) resp.Write(json) return } id, err := strconv.Atoi(idStr) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()}) resp.Write(json) return } mdlRedisCfg := model.NewRedisCfg(Db) item, err := mdlRedisCfg.Get(int64(id)) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 3, Error: err.Error()}) resp.Write(json) } else { json, _ := json.Marshal(SyscfgRedisGetRet{Errno: 0, Error: "", Data: item}) resp.Write(json) } } func syscfg_redis_set(resp http.ResponseWriter, req *http.Request) { _, ok := checkLogin(resp, req) if !ok { return } req.ParseForm() Id := req.PostForm.Get("Id") Address := req.PostForm.Get("Address") Remark := req.PostForm.Get("Remark") Password := req.PostForm.Get("Password") MaxConnectWait := req.PostForm.Get("MaxConnectWait") MaxStatusFailed := req.PostForm.Get("MaxStatusFailed") MinMemoryFree := req.PostForm.Get("MinMemoryFree") MinMemoryFreePC := req.PostForm.Get("MinMemoryFreePC") MaxMemoryUsage := req.PostForm.Get("MaxMemoryUsage") MaxConnection := req.PostForm.Get("MaxConnection") MaxEviIncreased := req.PostForm.Get("MaxEviIncreased") MaxQPS := req.PostForm.Get("MaxQPS") MailList := req.PostForm.Get("MailList") Disabled := req.PostForm.Get("Disabled") if Address == "" { json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "Redis服务地址不能为空!"}) resp.Write(json) return } if Remark == "" { json, _ := json.Marshal(ErrorRet{Errno: 2, Error: "Redis备注名称不能为空!"}) resp.Write(json) return } if MaxConnectWait == "" { json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "连接超时时长不能为空!"}) resp.Write(json) return } if MaxStatusFailed == "" { json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "最大状态检测失败次数不能为空!"}) resp.Write(json) return } if MinMemoryFree == "" { json, _ := json.Marshal(ErrorRet{Errno: 5, Error: "最小可用内存空间不能为空!"}) resp.Write(json) return } if MinMemoryFreePC == "" { json, _ := json.Marshal(ErrorRet{Errno: 6, Error: "最小可用内存空间百分比不能为空!"}) resp.Write(json) return } if MaxMemoryUsage == "" { json, _ := json.Marshal(ErrorRet{Errno: 7, Error: "最大可用内存空间不能为空!"}) resp.Write(json) return } if MaxConnection == "" { json, _ := json.Marshal(ErrorRet{Errno: 8, Error: "最大并发连接数不能为空!"}) resp.Write(json) return } if MaxEviIncreased == "" { json, _ := json.Marshal(ErrorRet{Errno: 9, Error: "最大新增淘汰记录数不能为空!"}) resp.Write(json) return } if MaxQPS == "" { json, _ := json.Marshal(ErrorRet{Errno: 10, Error: "最大QPS值不能为空!"}) resp.Write(json) return } if MailList == "" { json, _ := json.Marshal(ErrorRet{Errno: 11, Error: "报警邮件接收邮箱列表不能为空!"}) resp.Write(json) return } if Disabled == "" { json, _ := json.Marshal(ErrorRet{Errno: 12, Error: "状态不能为空!"}) resp.Write(json) return } IdInt, err := strconv.Atoi(Id) if err != nil { IdInt = 0 } MaxConnectWaitInt, err := strconv.Atoi(MaxConnectWait) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 13, Error: err.Error()}) resp.Write(json) return } MaxStatusFailedInt, err := strconv.Atoi(MaxStatusFailed) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 14, Error: err.Error()}) resp.Write(json) return } MinMemoryFreeInt, err := strconv.Atoi(MinMemoryFree) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 15, Error: err.Error()}) resp.Write(json) return } MinMemoryFreePCInt, err := strconv.Atoi(MinMemoryFreePC) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 16, Error: err.Error()}) resp.Write(json) return } MaxMemoryUsageInt, err := strconv.Atoi(MaxMemoryUsage) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 17, Error: err.Error()}) resp.Write(json) return } MaxConnectionInt, err := strconv.Atoi(MaxConnection) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 18, Error: err.Error()}) resp.Write(json) return } MaxEviIncreasedInt, err := strconv.Atoi(MaxEviIncreased) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 19, Error: err.Error()}) resp.Write(json) return } MaxQPSInt, err := strconv.Atoi(MaxQPS) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 20, Error: err.Error()}) resp.Write(json) return } DisabledInt, err := strconv.Atoi(Disabled) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 21, Error: err.Error()}) resp.Write(json) return } newRow := model.RedisCfgRow{ Id: int64(IdInt), Address: Address, Remark: Remark, Password: Password, MaxConnectWait: int64(MaxConnectWaitInt), MaxStatusFailed: int64(MaxStatusFailedInt), MinMemoryFree: int64(MinMemoryFreeInt), MinMemoryFreePC: int64(MinMemoryFreePCInt), MaxMemoryUsage: int64(MaxMemoryUsageInt), MaxConnection: int64(MaxConnectionInt), MaxEviIncreased: int64(MaxEviIncreasedInt), MaxQPS: int64(MaxQPSInt), MailList: MailList, Disabled: DisabledInt != 0, } mdlRedisCfg := model.NewRedisCfg(Db) if IdInt > 0 { affected, err := mdlRedisCfg.Update(&newRow) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 22, Error: err.Error()}) resp.Write(json) } else if affected > 0 { json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""}) resp.Write(json) } else { json, _ := json.Marshal(ErrorRet{Errno: 23, Error: "更新失败!"}) resp.Write(json) } } else { newRowId, err := mdlRedisCfg.Insert(&newRow) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 22, Error: err.Error()}) resp.Write(json) } else if newRowId > 0 { json, _ := json.Marshal(SyscfgRedisAddRet{Errno: 0, Error: "", Data: newRowId}) resp.Write(json) } else { json, _ := json.Marshal(ErrorRet{Errno: 23, Error: "新增失败!"}) resp.Write(json) } } } func syscfg_redis_del(resp http.ResponseWriter, req *http.Request) { _, ok := checkLogin(resp, req) if !ok { return } req.ParseForm() idStr := req.Form.Get("id") if idStr == "" { json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"}) resp.Write(json) return } id, err := strconv.Atoi(idStr) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()}) resp.Write(json) return } mdlRedisCfg := model.NewRedisCfg(Db) affected, err := mdlRedisCfg.Delete(int64(id)) if err != nil { json, _ := json.Marshal(ErrorRet{Errno: 3, Error: err.Error()}) resp.Write(json) } else if affected > 0 { json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""}) resp.Write(json) } else { json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "操作失败!"}) resp.Write(json) } }