logfilelist.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. oracle : {{.db}} <br>
  25. mysql : {{.mysql}} <br>
  26. redis : {{.redis}} <br>
  27. <p>------------------------</p>
  28. {{.data}}
  29. <p>------------------------</p>
  30. `
  31. sFile := make([]string, 0)
  32. path := config.SerCfg.LogCfg.LogPath
  33. db := fmt.Sprintf("%s/***@%s:%s/%s",
  34. config.SerCfg.DbCfg.DbUser,
  35. config.SerCfg.DbCfg.DbAddress,
  36. config.SerCfg.DbCfg.DbPort,
  37. config.SerCfg.DbCfg.DbName)
  38. mysql := fmt.Sprintf("%s/***@%s:%v/%s",
  39. config.SerCfg.MySQLCfg.Username,
  40. config.SerCfg.MySQLCfg.Host,
  41. config.SerCfg.MySQLCfg.Port,
  42. config.SerCfg.MySQLCfg.DBName)
  43. redis := fmt.Sprintf("%s:%v", config.SerCfg.RedisCfg.Address, config.SerCfg.RedisCfg.Port)
  44. if d, err := ioutil.ReadDir(path); err == nil {
  45. for _, v := range d {
  46. if !v.IsDir() {
  47. lk := fmt.Sprintf(`<a href="%v/debug/download?filename=%v" target="_blank">%v</a> size:%.2f k`,
  48. host, v.Name(), v.Name(), float64(v.Size())/1024.0)
  49. lk2 := fmt.Sprintf(`<a href="%v/debug/log/view?filename=%v" target="_blank">查看</a>`, host, v.Name())
  50. lk = lk + " " + lk2
  51. sFile = append(sFile, lk)
  52. }
  53. }
  54. str := strings.Join(sFile, "\n<br>")
  55. if tmpl, err := template.New("index").Parse(templateText); err == nil {
  56. _ = tmpl.Execute(c.Writer, gin.H{"data": template.HTML(str), "db": template.HTML(db),
  57. "mysql": template.HTML(mysql), "redis": template.HTML(redis)})
  58. } else {
  59. c.String(400, "index err.")
  60. }
  61. } else {
  62. c.JSON(http.StatusBadRequest, gin.H{"message": "can't list the file"})
  63. }
  64. }