http_index.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "html/template"
  6. "io"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "redisdog/model"
  11. )
  12. type StatRet struct {
  13. Errno int `json:"errno"`
  14. Error string `json:"error"`
  15. Data []*RedisInfo `json:"data"`
  16. }
  17. type InfoRet struct {
  18. Errno int `json:"errno"`
  19. Error string `json:"error"`
  20. Data *RedisInfo `json:"data"`
  21. }
  22. func index_default(resp http.ResponseWriter, req *http.Request) {
  23. sess, ok := checkLogin(resp, req)
  24. if !ok {
  25. return
  26. }
  27. //视图输出
  28. files := []string{
  29. filepath.Join(Cfg.TmplDir, "index.tmpl"),
  30. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  31. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  32. }
  33. tmpl, err := template.New("index.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  34. if err != nil {
  35. resp.WriteHeader(500)
  36. io.WriteString(resp, err.Error())
  37. return
  38. }
  39. tmpl.Execute(resp, struct {
  40. Sess *Session
  41. Req *http.Request
  42. Title string
  43. }{
  44. sess,
  45. req,
  46. "首页",
  47. })
  48. }
  49. func index_stats(resp http.ResponseWriter, req *http.Request) {
  50. _, ok := checkLogin(resp, req)
  51. if !ok {
  52. return
  53. }
  54. mdlRedisCfg := model.NewRedisCfg(Db)
  55. rows, err := mdlRedisCfg.GetAll(0)
  56. if err != nil {
  57. ret, _ := json.Marshal(ErrorRet{Errno: 0, Error: err.Error()})
  58. resp.Write(ret)
  59. return
  60. }
  61. stats := make([]*RedisInfo, 0)
  62. for _, row := range rows {
  63. stats = append(stats, queryRedisInfo(row))
  64. }
  65. ret, _ := json.Marshal(StatRet{Errno: 0, Error: "", Data: stats})
  66. resp.Write(ret)
  67. }
  68. func index_info(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 := int(0)
  80. _, err := fmt.Sscanf(idstr, "%d", &id)
  81. if err != nil {
  82. resp.Write(ERROR_RET(2, "ID的值不是整数!"))
  83. return
  84. }
  85. if id <= 0 {
  86. resp.Write(ERROR_RET(3, "ID的值必须大于0!"))
  87. return
  88. }
  89. mdlRedisCfg := model.NewRedisCfg(Db)
  90. row, err := mdlRedisCfg.Get(int64(id))
  91. if err != nil {
  92. resp.Write(ERROR_RET(4, err.Error()))
  93. } else {
  94. info := queryRedisInfo(row)
  95. if info.Error == "" {
  96. ret, _ := json.Marshal(InfoRet{Errno: 0, Error: "", Data: info})
  97. resp.Write(ret)
  98. } else {
  99. resp.Write(ERROR_RET(5, info.Error))
  100. }
  101. }
  102. }
  103. func index_favicon(resp http.ResponseWriter, req *http.Request) {
  104. file, err := os.OpenFile(filepath.Join(Cfg.ResourcesDir, "favicon.ico"), os.O_RDONLY, 0644)
  105. if err != nil {
  106. resp.WriteHeader(404)
  107. return
  108. }
  109. defer file.Close()
  110. resp.Header().Set("Content-Type", "image/x-icon")
  111. data := make([]byte, 1024)
  112. for {
  113. if l, err := file.Read(data); err == io.EOF {
  114. break
  115. } else if err != nil {
  116. break
  117. } else {
  118. resp.Write(data[0:l])
  119. }
  120. }
  121. }
  122. func index_forbidden(resp http.ResponseWriter, req *http.Request) {
  123. sess, ok := checkLogin(resp, req)
  124. if !ok {
  125. return
  126. }
  127. //视图输出
  128. files := []string{
  129. filepath.Join(Cfg.TmplDir, "forbidden.tmpl"),
  130. filepath.Join(Cfg.TmplDir, "header.tmpl"),
  131. filepath.Join(Cfg.TmplDir, "navbar.tmpl"),
  132. }
  133. tmpl, err := template.New("forbidden.tmpl").Funcs(TmplFuncMap).ParseFiles(files...)
  134. if err != nil {
  135. resp.WriteHeader(500)
  136. io.WriteString(resp, err.Error())
  137. return
  138. }
  139. tmpl.Execute(resp, struct {
  140. Sess *Session
  141. Req *http.Request
  142. Title string
  143. }{
  144. sess,
  145. req,
  146. "首页",
  147. })
  148. }