syscfg_redis.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "path/filepath"
  9. "strconv"
  10. "cnphper.com/model"
  11. )
  12. type SyscfgRedisListRet struct {
  13. Errno int `json:"errno"`
  14. Error string `json:"error"`
  15. Data []*model.RedisCfgRow `json:"data"`
  16. }
  17. type SyscfgRedisGetRet struct {
  18. Errno int `json:"errno"`
  19. Error string `json:"error"`
  20. Data *model.RedisCfgRow `json:"data"`
  21. }
  22. type SyscfgRedisAddRet struct {
  23. Errno int `json:"errno"`
  24. Error string `json:"error"`
  25. Data int64 `json:"data"`
  26. }
  27. func syscfg_redis(resp http.ResponseWriter, req *http.Request) {
  28. sess, ok := checkLogin(resp, req)
  29. if !ok {
  30. return
  31. }
  32. //视图输出
  33. files := []string{
  34. filepath.Join(Cfg.TmplDir, "syscfg", "redis.tmpl"),
  35. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  36. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  37. }
  38. tmpl, err := template.New("redis.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  39. if err != nil {
  40. io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error()))
  41. } else {
  42. tmpl.Execute(resp, struct {
  43. Sess *Session
  44. Req *http.Request
  45. Title string
  46. }{
  47. sess,
  48. req,
  49. "Redis列表管理",
  50. })
  51. }
  52. }
  53. func syscfg_redis_list(resp http.ResponseWriter, req *http.Request) {
  54. _, ok := checkLogin(resp, req)
  55. if !ok {
  56. return
  57. }
  58. req.ParseForm()
  59. mdlRedisCfg := model.NewRedisCfg(Db)
  60. rows, err := mdlRedisCfg.GetAll(-1)
  61. if err != nil {
  62. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: err.Error()})
  63. resp.Write(json)
  64. } else {
  65. json, _ := json.Marshal(SyscfgRedisListRet{Errno: 0, Error: "", Data: rows})
  66. resp.Write(json)
  67. }
  68. }
  69. func syscfg_redis_get(resp http.ResponseWriter, req *http.Request) {
  70. _, ok := checkLogin(resp, req)
  71. if !ok {
  72. return
  73. }
  74. req.ParseForm()
  75. idStr := req.Form.Get("id")
  76. if idStr == "" {
  77. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"})
  78. resp.Write(json)
  79. return
  80. }
  81. id, err := strconv.Atoi(idStr)
  82. if err != nil {
  83. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()})
  84. resp.Write(json)
  85. return
  86. }
  87. mdlRedisCfg := model.NewRedisCfg(Db)
  88. item, err := mdlRedisCfg.Get(int64(id))
  89. if err != nil {
  90. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: err.Error()})
  91. resp.Write(json)
  92. } else {
  93. json, _ := json.Marshal(SyscfgRedisGetRet{Errno: 0, Error: "", Data: item})
  94. resp.Write(json)
  95. }
  96. }
  97. func syscfg_redis_set(resp http.ResponseWriter, req *http.Request) {
  98. _, ok := checkLogin(resp, req)
  99. if !ok {
  100. return
  101. }
  102. req.ParseForm()
  103. Id := req.PostForm.Get("Id")
  104. Address := req.PostForm.Get("Address")
  105. Remark := req.PostForm.Get("Remark")
  106. Password := req.PostForm.Get("Password")
  107. MaxConnectWait := req.PostForm.Get("MaxConnectWait")
  108. MaxStatusFailed := req.PostForm.Get("MaxStatusFailed")
  109. MinMemoryFree := req.PostForm.Get("MinMemoryFree")
  110. MinMemoryFreePC := req.PostForm.Get("MinMemoryFreePC")
  111. MaxMemoryUsage := req.PostForm.Get("MaxMemoryUsage")
  112. MaxConnection := req.PostForm.Get("MaxConnection")
  113. MaxEviIncreased := req.PostForm.Get("MaxEviIncreased")
  114. MaxQPS := req.PostForm.Get("MaxQPS")
  115. MailList := req.PostForm.Get("MailList")
  116. Disabled := req.PostForm.Get("Disabled")
  117. if Address == "" {
  118. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "Redis服务地址不能为空!"})
  119. resp.Write(json)
  120. return
  121. }
  122. if Remark == "" {
  123. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: "Redis备注名称不能为空!"})
  124. resp.Write(json)
  125. return
  126. }
  127. if MaxConnectWait == "" {
  128. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: "连接超时时长不能为空!"})
  129. resp.Write(json)
  130. return
  131. }
  132. if MaxStatusFailed == "" {
  133. json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "最大状态检测失败次数不能为空!"})
  134. resp.Write(json)
  135. return
  136. }
  137. if MinMemoryFree == "" {
  138. json, _ := json.Marshal(ErrorRet{Errno: 5, Error: "最小可用内存空间不能为空!"})
  139. resp.Write(json)
  140. return
  141. }
  142. if MinMemoryFreePC == "" {
  143. json, _ := json.Marshal(ErrorRet{Errno: 6, Error: "最小可用内存空间百分比不能为空!"})
  144. resp.Write(json)
  145. return
  146. }
  147. if MaxMemoryUsage == "" {
  148. json, _ := json.Marshal(ErrorRet{Errno: 7, Error: "最大可用内存空间不能为空!"})
  149. resp.Write(json)
  150. return
  151. }
  152. if MaxConnection == "" {
  153. json, _ := json.Marshal(ErrorRet{Errno: 8, Error: "最大并发连接数不能为空!"})
  154. resp.Write(json)
  155. return
  156. }
  157. if MaxEviIncreased == "" {
  158. json, _ := json.Marshal(ErrorRet{Errno: 9, Error: "最大新增淘汰记录数不能为空!"})
  159. resp.Write(json)
  160. return
  161. }
  162. if MaxQPS == "" {
  163. json, _ := json.Marshal(ErrorRet{Errno: 10, Error: "最大QPS值不能为空!"})
  164. resp.Write(json)
  165. return
  166. }
  167. if MailList == "" {
  168. json, _ := json.Marshal(ErrorRet{Errno: 11, Error: "报警邮件接收邮箱列表不能为空!"})
  169. resp.Write(json)
  170. return
  171. }
  172. if Disabled == "" {
  173. json, _ := json.Marshal(ErrorRet{Errno: 12, Error: "状态不能为空!"})
  174. resp.Write(json)
  175. return
  176. }
  177. IdInt, err := strconv.Atoi(Id)
  178. if err != nil {
  179. IdInt = 0
  180. }
  181. MaxConnectWaitInt, err := strconv.Atoi(MaxConnectWait)
  182. if err != nil {
  183. json, _ := json.Marshal(ErrorRet{Errno: 13, Error: err.Error()})
  184. resp.Write(json)
  185. return
  186. }
  187. MaxStatusFailedInt, err := strconv.Atoi(MaxStatusFailed)
  188. if err != nil {
  189. json, _ := json.Marshal(ErrorRet{Errno: 14, Error: err.Error()})
  190. resp.Write(json)
  191. return
  192. }
  193. MinMemoryFreeInt, err := strconv.Atoi(MinMemoryFree)
  194. if err != nil {
  195. json, _ := json.Marshal(ErrorRet{Errno: 15, Error: err.Error()})
  196. resp.Write(json)
  197. return
  198. }
  199. MinMemoryFreePCInt, err := strconv.Atoi(MinMemoryFreePC)
  200. if err != nil {
  201. json, _ := json.Marshal(ErrorRet{Errno: 16, Error: err.Error()})
  202. resp.Write(json)
  203. return
  204. }
  205. MaxMemoryUsageInt, err := strconv.Atoi(MaxMemoryUsage)
  206. if err != nil {
  207. json, _ := json.Marshal(ErrorRet{Errno: 17, Error: err.Error()})
  208. resp.Write(json)
  209. return
  210. }
  211. MaxConnectionInt, err := strconv.Atoi(MaxConnection)
  212. if err != nil {
  213. json, _ := json.Marshal(ErrorRet{Errno: 18, Error: err.Error()})
  214. resp.Write(json)
  215. return
  216. }
  217. MaxEviIncreasedInt, err := strconv.Atoi(MaxEviIncreased)
  218. if err != nil {
  219. json, _ := json.Marshal(ErrorRet{Errno: 19, Error: err.Error()})
  220. resp.Write(json)
  221. return
  222. }
  223. MaxQPSInt, err := strconv.Atoi(MaxQPS)
  224. if err != nil {
  225. json, _ := json.Marshal(ErrorRet{Errno: 20, Error: err.Error()})
  226. resp.Write(json)
  227. return
  228. }
  229. DisabledInt, err := strconv.Atoi(Disabled)
  230. if err != nil {
  231. json, _ := json.Marshal(ErrorRet{Errno: 21, Error: err.Error()})
  232. resp.Write(json)
  233. return
  234. }
  235. newRow := model.RedisCfgRow{
  236. Id: int64(IdInt),
  237. Address: Address,
  238. Remark: Remark,
  239. Password: Password,
  240. MaxConnectWait: int64(MaxConnectWaitInt),
  241. MaxStatusFailed: int64(MaxStatusFailedInt),
  242. MinMemoryFree: int64(MinMemoryFreeInt),
  243. MinMemoryFreePC: int64(MinMemoryFreePCInt),
  244. MaxMemoryUsage: int64(MaxMemoryUsageInt),
  245. MaxConnection: int64(MaxConnectionInt),
  246. MaxEviIncreased: int64(MaxEviIncreasedInt),
  247. MaxQPS: int64(MaxQPSInt),
  248. MailList: MailList,
  249. Disabled: DisabledInt != 0,
  250. }
  251. mdlRedisCfg := model.NewRedisCfg(Db)
  252. if IdInt > 0 {
  253. affected, err := mdlRedisCfg.Update(&newRow)
  254. if err != nil {
  255. json, _ := json.Marshal(ErrorRet{Errno: 22, Error: err.Error()})
  256. resp.Write(json)
  257. } else if affected > 0 {
  258. json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
  259. resp.Write(json)
  260. } else {
  261. json, _ := json.Marshal(ErrorRet{Errno: 23, Error: "更新失败!"})
  262. resp.Write(json)
  263. }
  264. } else {
  265. newRowId, err := mdlRedisCfg.Insert(&newRow)
  266. if err != nil {
  267. json, _ := json.Marshal(ErrorRet{Errno: 22, Error: err.Error()})
  268. resp.Write(json)
  269. } else if newRowId > 0 {
  270. json, _ := json.Marshal(SyscfgRedisAddRet{Errno: 0, Error: "", Data: newRowId})
  271. resp.Write(json)
  272. } else {
  273. json, _ := json.Marshal(ErrorRet{Errno: 23, Error: "新增失败!"})
  274. resp.Write(json)
  275. }
  276. }
  277. }
  278. func syscfg_redis_del(resp http.ResponseWriter, req *http.Request) {
  279. _, ok := checkLogin(resp, req)
  280. if !ok {
  281. return
  282. }
  283. req.ParseForm()
  284. idStr := req.Form.Get("id")
  285. if idStr == "" {
  286. json, _ := json.Marshal(ErrorRet{Errno: 1, Error: "ID不能为空!"})
  287. resp.Write(json)
  288. return
  289. }
  290. id, err := strconv.Atoi(idStr)
  291. if err != nil {
  292. json, _ := json.Marshal(ErrorRet{Errno: 2, Error: err.Error()})
  293. resp.Write(json)
  294. return
  295. }
  296. mdlRedisCfg := model.NewRedisCfg(Db)
  297. affected, err := mdlRedisCfg.Delete(int64(id))
  298. if err != nil {
  299. json, _ := json.Marshal(ErrorRet{Errno: 3, Error: err.Error()})
  300. resp.Write(json)
  301. } else if affected > 0 {
  302. json, _ := json.Marshal(ErrorRet{Errno: 0, Error: ""})
  303. resp.Write(json)
  304. } else {
  305. json, _ := json.Marshal(ErrorRet{Errno: 4, Error: "操作失败!"})
  306. resp.Write(json)
  307. }
  308. }