http_syscfg_misc.go 3.2 KB

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