logfilelist.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/10/15 16:28
  4. * @Modify : 2021/10/15 16:28
  5. */
  6. package other
  7. import (
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "html/template"
  11. "io/ioutil"
  12. "mtp2_if/config"
  13. "net/http"
  14. "strings"
  15. )
  16. // LogList 列出日志文件
  17. func LogList(c *gin.Context) {
  18. host := c.Request.Host
  19. if !strings.Contains(host, "http://") {
  20. host = "http://" + host
  21. }
  22. const templateText = `
  23. <h1>日志文件列表</h1>
  24. {{.db}}
  25. <p>------------------------</p>
  26. {{.data}}
  27. <p>------------------------</p>
  28. `
  29. sFile := make([]string, 0)
  30. path := config.SerCfg.LogCfg.LogPath
  31. db := fmt.Sprintf("%s/***@%s:%s/%s",
  32. config.SerCfg.DbCfg.DbUser,
  33. config.SerCfg.DbCfg.DbAddress,
  34. config.SerCfg.DbCfg.DbPort,
  35. config.SerCfg.DbCfg.DbName)
  36. if d, err := ioutil.ReadDir(path); err == nil {
  37. for _, v := range d {
  38. if !v.IsDir() {
  39. lk := fmt.Sprintf(`<a href="%v/debug/download?filename=%v" target="_blank">%v</a> size:%.2f k`,
  40. host, v.Name(), v.Name(), float64(v.Size())/1024.0)
  41. sFile = append(sFile, lk)
  42. }
  43. }
  44. str := strings.Join(sFile, "\n<br>")
  45. if tmpl, err := template.New("index").Parse(templateText); err == nil {
  46. _ = tmpl.Execute(c.Writer, gin.H{"data": template.HTML(str), "db": template.HTML(db)})
  47. } else {
  48. c.String(400, "index err.")
  49. }
  50. } else {
  51. c.JSON(http.StatusBadRequest, gin.H{"message": "can't list the file"})
  52. }
  53. }