http_syscfg_redis.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "path/filepath"
  9. "strconv"
  10. "redisdog/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. resp.Write(ERROR_RET(1, err.Error()))
  63. } else {
  64. ret, _ := json.Marshal(SyscfgRedisListRet{Errno: 0, Error: "", Data: rows})
  65. resp.Write(ret)
  66. }
  67. }
  68. func syscfg_redis_get(resp http.ResponseWriter, req *http.Request) {
  69. _, ok := checkLogin(resp, req)
  70. if !ok {
  71. return
  72. }
  73. req.ParseForm()
  74. idStr := req.Form.Get("id")
  75. if idStr == "" {
  76. resp.Write(ERROR_RET(1, "ID不能为空!"))
  77. return
  78. }
  79. id, err := strconv.Atoi(idStr)
  80. if err != nil {
  81. resp.Write(ERROR_RET(2, err.Error()))
  82. return
  83. }
  84. mdlRedisCfg := model.NewRedisCfg(Db)
  85. item, err := mdlRedisCfg.Get(int64(id))
  86. if err != nil {
  87. resp.Write(ERROR_RET(3, err.Error()))
  88. } else {
  89. ret, _ := json.Marshal(SyscfgRedisGetRet{Errno: 0, Error: "", Data: item})
  90. resp.Write(ret)
  91. }
  92. }
  93. func syscfg_redis_set(resp http.ResponseWriter, req *http.Request) {
  94. _, ok := checkLogin(resp, req)
  95. if !ok {
  96. return
  97. }
  98. req.ParseForm()
  99. Id := req.PostForm.Get("Id")
  100. Address := req.PostForm.Get("Address")
  101. Remark := req.PostForm.Get("Remark")
  102. Password := req.PostForm.Get("Password")
  103. MaxConnectWait := req.PostForm.Get("MaxConnectWait")
  104. MaxStatusFailed := req.PostForm.Get("MaxStatusFailed")
  105. MinMemoryFree := req.PostForm.Get("MinMemoryFree")
  106. StepMemoryIncrease := req.PostForm.Get("StepMemoryIncrease")
  107. MaxMemoryUsage := req.PostForm.Get("MaxMemoryUsage")
  108. MaxConnection := req.PostForm.Get("MaxConnection")
  109. MaxEviIncreased := req.PostForm.Get("MaxEviIncreased")
  110. MaxQPS := req.PostForm.Get("MaxQPS")
  111. MailList := req.PostForm.Get("MailList")
  112. Disabled := req.PostForm.Get("Disabled")
  113. if Address == "" {
  114. resp.Write(ERROR_RET(1, "Redis服务地址不能为空!"))
  115. return
  116. }
  117. if Remark == "" {
  118. resp.Write(ERROR_RET(2, "Redis备注名称不能为空!"))
  119. return
  120. }
  121. if MaxConnectWait == "" {
  122. resp.Write(ERROR_RET(3, "连接超时时长不能为空!"))
  123. return
  124. }
  125. if MaxStatusFailed == "" {
  126. resp.Write(ERROR_RET(4, "最大状态检测失败次数不能为空!"))
  127. return
  128. }
  129. if MinMemoryFree == "" {
  130. resp.Write(ERROR_RET(5, "最小可用内存空间不能为空!"))
  131. return
  132. }
  133. if StepMemoryIncrease == "" {
  134. resp.Write(ERROR_RET(6, "单次扩容大小不能为空!"))
  135. return
  136. }
  137. if MaxMemoryUsage == "" {
  138. resp.Write(ERROR_RET(7, "最大可用内存空间不能为空!"))
  139. return
  140. }
  141. if MaxConnection == "" {
  142. resp.Write(ERROR_RET(8, "最大并发连接数不能为空!"))
  143. return
  144. }
  145. if MaxEviIncreased == "" {
  146. resp.Write(ERROR_RET(9, "最大新增淘汰记录数不能为空!"))
  147. return
  148. }
  149. if MaxQPS == "" {
  150. resp.Write(ERROR_RET(10, "最大QPS值不能为空!"))
  151. return
  152. }
  153. if MailList == "" {
  154. resp.Write(ERROR_RET(11, "报警邮件接收邮箱列表不能为空!"))
  155. return
  156. }
  157. if Disabled == "" {
  158. resp.Write(ERROR_RET(12, "状态不能为空!"))
  159. return
  160. }
  161. IdInt, err := strconv.Atoi(Id)
  162. if err != nil {
  163. IdInt = 0
  164. }
  165. MaxConnectWaitInt, err := strconv.Atoi(MaxConnectWait)
  166. if err != nil {
  167. resp.Write(ERROR_RET(13, err.Error()))
  168. return
  169. }
  170. MaxStatusFailedInt, err := strconv.Atoi(MaxStatusFailed)
  171. if err != nil {
  172. resp.Write(ERROR_RET(14, err.Error()))
  173. return
  174. }
  175. MinMemoryFreeInt, err := strconv.Atoi(MinMemoryFree)
  176. if err != nil {
  177. resp.Write(ERROR_RET(15, err.Error()))
  178. return
  179. }
  180. StepMemoryIncreaseInt, err := strconv.Atoi(StepMemoryIncrease)
  181. if err != nil {
  182. resp.Write(ERROR_RET(16, err.Error()))
  183. return
  184. }
  185. MaxMemoryUsageInt, err := strconv.Atoi(MaxMemoryUsage)
  186. if err != nil {
  187. resp.Write(ERROR_RET(17, err.Error()))
  188. return
  189. }
  190. MaxConnectionInt, err := strconv.Atoi(MaxConnection)
  191. if err != nil {
  192. resp.Write(ERROR_RET(18, err.Error()))
  193. return
  194. }
  195. MaxEviIncreasedInt, err := strconv.Atoi(MaxEviIncreased)
  196. if err != nil {
  197. resp.Write(ERROR_RET(19, err.Error()))
  198. return
  199. }
  200. MaxQPSInt, err := strconv.Atoi(MaxQPS)
  201. if err != nil {
  202. resp.Write(ERROR_RET(20, err.Error()))
  203. return
  204. }
  205. DisabledInt, err := strconv.Atoi(Disabled)
  206. if err != nil {
  207. resp.Write(ERROR_RET(21, err.Error()))
  208. return
  209. }
  210. newRow := model.RedisCfgRow{
  211. Id: int64(IdInt),
  212. Address: Address,
  213. Remark: Remark,
  214. Password: Password,
  215. MaxConnectWait: int64(MaxConnectWaitInt),
  216. MaxStatusFailed: int64(MaxStatusFailedInt),
  217. MinMemoryFree: int64(MinMemoryFreeInt),
  218. StepMemoryIncrease: int64(StepMemoryIncreaseInt),
  219. MaxMemoryUsage: int64(MaxMemoryUsageInt),
  220. MaxConnection: int64(MaxConnectionInt),
  221. MaxEviIncreased: int64(MaxEviIncreasedInt),
  222. MaxQPS: int64(MaxQPSInt),
  223. MailList: MailList,
  224. Disabled: DisabledInt != 0,
  225. }
  226. mdlRedisCfg := model.NewRedisCfg(Db)
  227. if IdInt > 0 {
  228. affected, err := mdlRedisCfg.Update(&newRow)
  229. if err != nil {
  230. resp.Write(ERROR_RET(22, err.Error()))
  231. } else if affected > 0 {
  232. resp.Write(ERROR_RET(0, ""))
  233. } else {
  234. resp.Write(ERROR_RET(23, "更新失败!"))
  235. }
  236. } else {
  237. newRowId, err := mdlRedisCfg.Insert(&newRow)
  238. if err != nil {
  239. ret, _ := json.Marshal(ErrorRet{Errno: 22, Error: err.Error()})
  240. resp.Write(ret)
  241. resp.Write(ERROR_RET(22, err.Error()))
  242. } else if newRowId > 0 {
  243. ret, _ := json.Marshal(SyscfgRedisAddRet{Errno: 0, Error: "", Data: newRowId})
  244. resp.Write(ret)
  245. } else {
  246. resp.Write(ERROR_RET(23, "新增失败!"))
  247. }
  248. }
  249. }
  250. func syscfg_redis_del(resp http.ResponseWriter, req *http.Request) {
  251. _, ok := checkLogin(resp, req)
  252. if !ok {
  253. return
  254. }
  255. req.ParseForm()
  256. idStr := req.Form.Get("id")
  257. if idStr == "" {
  258. resp.Write(ERROR_RET(1, "ID不能为空!"))
  259. return
  260. }
  261. id, err := strconv.Atoi(idStr)
  262. if err != nil {
  263. resp.Write(ERROR_RET(2, err.Error()))
  264. return
  265. }
  266. mdlRedisCfg := model.NewRedisCfg(Db)
  267. affected, err := mdlRedisCfg.Delete(int64(id))
  268. if err != nil {
  269. resp.Write(ERROR_RET(3, err.Error()))
  270. } else if affected > 0 {
  271. resp.Write(ERROR_RET(0, ""))
  272. } else {
  273. resp.Write(ERROR_RET(4, "操作失败!"))
  274. }
  275. }