http_syscfg_misc.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 SysCfgMiscGetRet struct {
  13. Errno int `json:"errno"`
  14. Error string `json:"error"`
  15. Data map[string]string `json:"data"`
  16. }
  17. func syscfg_misc(resp http.ResponseWriter, req *http.Request) {
  18. sess, ok := checkLogin(resp, req)
  19. if !ok {
  20. return
  21. }
  22. if !sess.Account.IsSuper {
  23. resp.Header().Set("Location", "/index/forbidden")
  24. resp.WriteHeader(302)
  25. return
  26. }
  27. //视图输出
  28. files := []string{
  29. filepath.Join(Cfg.TmplDir, "syscfg", "misc.tmpl"),
  30. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  31. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  32. }
  33. tmpl, err := template.New("misc.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  34. if err != nil {
  35. io.WriteString(resp, fmt.Sprintf("Error: %s\n", err.Error()))
  36. } else {
  37. tmpl.Execute(resp, struct {
  38. Sess *Session
  39. Req *http.Request
  40. Title string
  41. }{
  42. sess,
  43. req,
  44. "其它配置",
  45. })
  46. }
  47. }
  48. func syscfg_misc_get(resp http.ResponseWriter, req *http.Request) {
  49. sess, ok := checkLogin(resp, req)
  50. if !ok {
  51. return
  52. }
  53. if !sess.Account.IsSuper {
  54. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  55. resp.Write(ret)
  56. return
  57. }
  58. mdl := model.NewSysCfg(Db)
  59. values, err := mdl.GetByKeys([]string{"monitor_loop_interval", "monitor_mail_interval", "log_kept_days"})
  60. if err != nil {
  61. resp.Write(ERROR_RET(1, err.Error()))
  62. } else {
  63. ret, _ := json.Marshal(SysCfgMiscGetRet{Errno: 0, Error: "", Data: values})
  64. resp.Write(ret)
  65. }
  66. }
  67. func syscfg_misc_set(resp http.ResponseWriter, req *http.Request) {
  68. sess, ok := checkLogin(resp, req)
  69. if !ok {
  70. return
  71. }
  72. if !sess.Account.IsSuper {
  73. ret, _ := json.Marshal(ErrorRet{Errno: 404, Error: "Forbidden"})
  74. resp.Write(ret)
  75. return
  76. }
  77. req.ParseForm()
  78. moniotrLoopIntervalStr := req.PostForm.Get("monitor_loop_interval")
  79. if moniotrLoopIntervalStr == "" {
  80. resp.Write(ERROR_RET(1, "检查间隔不能为空"))
  81. return
  82. }
  83. monitorMailIntervalStr := req.PostForm.Get("monitor_mail_interval")
  84. if monitorMailIntervalStr == "" {
  85. resp.Write(ERROR_RET(2, "报警邮件发送间隔不能为空"))
  86. return
  87. }
  88. logKeptDaysStr := req.PostForm.Get("log_kept_days")
  89. if logKeptDaysStr == "" {
  90. resp.Write(ERROR_RET(3, "日志保留天数不能为空"))
  91. return
  92. }
  93. if _, err := strconv.Atoi(moniotrLoopIntervalStr); err != nil {
  94. resp.Write(ERROR_RET(4, "检查间隔不是有效的数字值"))
  95. return
  96. }
  97. if _, err := strconv.Atoi(monitorMailIntervalStr); err != nil {
  98. resp.Write(ERROR_RET(5, "报警邮件发送间隔不是有效的数字值"))
  99. return
  100. }
  101. if _, err := strconv.Atoi(logKeptDaysStr); err != nil {
  102. resp.Write(ERROR_RET(6, "日志保留天数不是有效的数字值"))
  103. return
  104. }
  105. mdl := model.NewSysCfg(Db)
  106. if _, err := mdl.Set("monitor_loop_interval", moniotrLoopIntervalStr); err != nil {
  107. resp.Write(ERROR_RET(7, "操作失败(monitor_loop_interval)"))
  108. }
  109. if _, err := mdl.Set("monitor_mail_interval", monitorMailIntervalStr); err != nil {
  110. resp.Write(ERROR_RET(8, "操作失败(monitor_mail_interval)"))
  111. }
  112. if _, err := mdl.Set("log_kept_days", logKeptDaysStr); err != nil {
  113. resp.Write(ERROR_RET(9, "操作失败(log_kept_days)"))
  114. }
  115. SYSLOG("WARN", fmt.Sprintf("管理员#%d %s %s修改了其它配置", sess.Account.Id, sess.Account.Name, sess.Account.Account))
  116. //更新配置缓存
  117. err := SysCfg.Load()
  118. if err != nil {
  119. resp.Write(ERROR_RET(7, fmt.Sprintf("配置更新成功,但未能成功刷新:%s", err.Error())))
  120. } else {
  121. resp.Write(ERROR_RET(0, ""))
  122. }
  123. }