logfilelist.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. var (
  17. VERSION string
  18. GITHASH string
  19. )
  20. // LogList 列出日志文件
  21. func LogList(c *gin.Context) {
  22. host := c.Request.Host
  23. if !strings.Contains(host, "http://") {
  24. host = "http://" + host
  25. }
  26. const templateText = `
  27. <h1>go查询服务信息</h1>
  28. <a href="{{.host}}/debug/update" title="点击后从自动构建服务拉取版本升级并重启GO服务, 请谨慎点击" target="_blank">点击升级到最新版本</a>
  29. <p>===========================</p>
  30. version : {{.version}}<br>
  31. git hash: {{.githash}}<br>
  32. oracle : {{.db}} <br>
  33. mysql : {{.mysql}} <br>
  34. redis : {{.redis}} <br>
  35. <br>
  36. <a href="{{.host}}/swagger/index.html" target="_blank">点击打开swagger</a>
  37. <br><br>
  38. <a href="{{.host}}/debug/token" title="仅当调试模式内网段才能成功设置" target="_blank">点击设置token(用于swagger查询)</a>
  39. <h1>日志文件列表</h1>
  40. <p>------------------------</p>
  41. {{.data}}
  42. <p>------------------------</p>
  43. `
  44. sFile := make([]string, 0)
  45. path := config.SerCfg.LogCfg.LogPath
  46. db := fmt.Sprintf("%s/***@%s:%s/%s",
  47. config.SerCfg.DbCfg.DbUser,
  48. config.SerCfg.DbCfg.DbAddress,
  49. config.SerCfg.DbCfg.DbPort,
  50. config.SerCfg.DbCfg.DbName)
  51. mysql := fmt.Sprintf("%s/***@%s:%v/%s",
  52. config.SerCfg.MySQLCfg.Username,
  53. config.SerCfg.MySQLCfg.Host,
  54. config.SerCfg.MySQLCfg.Port,
  55. config.SerCfg.MySQLCfg.DBName)
  56. redis := fmt.Sprintf("%s:%v", config.SerCfg.RedisCfg.Address, config.SerCfg.RedisCfg.Port)
  57. if d, err := ioutil.ReadDir(path); err == nil {
  58. for _, v := range d {
  59. if !v.IsDir() {
  60. lk := fmt.Sprintf(`<a href="%v/debug/download?filename=%v" target="_blank">%v</a> size:%.2f k`,
  61. host, v.Name(), v.Name(), float64(v.Size())/1024.0)
  62. lk2 := fmt.Sprintf(`<a href="%v/debug/log/view?filename=%v" target="_blank">查看</a>`, host, v.Name())
  63. lk = lk + " " + lk2
  64. sFile = append(sFile, lk)
  65. }
  66. }
  67. str := strings.Join(sFile, "\n<br>")
  68. if tmpl, err := template.New("index").Parse(templateText); err == nil {
  69. _ = tmpl.Execute(c.Writer, gin.H{"data": template.HTML(str), "db": template.HTML(db),
  70. "mysql": template.HTML(mysql), "redis": template.HTML(redis),
  71. "version": template.HTML(VERSION), "githash": template.HTML(GITHASH)})
  72. } else {
  73. c.String(400, "index err.")
  74. }
  75. } else {
  76. c.JSON(http.StatusBadRequest, gin.H{"message": "can't list the file"})
  77. }
  78. }