logviewer.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package logviewer
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/fs"
  6. "net/http"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "imuslab.com/zoraxy/mod/utils"
  11. )
  12. type ViewerOption struct {
  13. RootFolder string //The root folder to scan for log
  14. Extension string //The extension the root files use, include the . in your ext (e.g. .log)
  15. }
  16. type Viewer struct {
  17. option *ViewerOption
  18. }
  19. type LogFile struct {
  20. Title string
  21. Filename string
  22. Fullpath string
  23. Filesize int64
  24. }
  25. func NewLogViewer(option *ViewerOption) *Viewer {
  26. return &Viewer{option: option}
  27. }
  28. /*
  29. Log Request Handlers
  30. */
  31. //List all the log files in the log folder. Return in map[string]LogFile format
  32. func (v *Viewer) HandleListLog(w http.ResponseWriter, r *http.Request) {
  33. logFiles := v.ListLogFiles(false)
  34. js, _ := json.Marshal(logFiles)
  35. utils.SendJSONResponse(w, string(js))
  36. }
  37. // Read log of a given catergory and filename
  38. // Require GET varaible: file and catergory
  39. func (v *Viewer) HandleReadLog(w http.ResponseWriter, r *http.Request) {
  40. filename, err := utils.GetPara(r, "file")
  41. if err != nil {
  42. utils.SendErrorResponse(w, "invalid filename given")
  43. return
  44. }
  45. content, err := v.LoadLogFile(strings.TrimSpace(filepath.Base(filename)))
  46. if err != nil {
  47. utils.SendErrorResponse(w, err.Error())
  48. return
  49. }
  50. utils.SendTextResponse(w, content)
  51. }
  52. /*
  53. Log Access Functions
  54. */
  55. func (v *Viewer) ListLogFiles(showFullpath bool) map[string][]*LogFile {
  56. result := map[string][]*LogFile{}
  57. filepath.WalkDir(v.option.RootFolder, func(path string, di fs.DirEntry, err error) error {
  58. if filepath.Ext(path) == v.option.Extension {
  59. catergory := filepath.Base(filepath.Dir(path))
  60. logList, ok := result[catergory]
  61. if !ok {
  62. //this catergory hasn't been scanned before.
  63. logList = []*LogFile{}
  64. }
  65. fullpath := filepath.ToSlash(path)
  66. if !showFullpath {
  67. fullpath = ""
  68. }
  69. st, err := os.Stat(path)
  70. if err != nil {
  71. return nil
  72. }
  73. logList = append(logList, &LogFile{
  74. Title: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)),
  75. Filename: filepath.Base(path),
  76. Fullpath: fullpath,
  77. Filesize: st.Size(),
  78. })
  79. result[catergory] = logList
  80. }
  81. return nil
  82. })
  83. return result
  84. }
  85. func (v *Viewer) LoadLogFile(filename string) (string, error) {
  86. filename = filepath.ToSlash(filename)
  87. filename = strings.ReplaceAll(filename, "../", "")
  88. logFilepath := filepath.Join(v.option.RootFolder, filename)
  89. if utils.FileExists(logFilepath) {
  90. //Load it
  91. content, err := os.ReadFile(logFilepath)
  92. if err != nil {
  93. return "", err
  94. }
  95. return string(content), nil
  96. } else {
  97. return "", errors.New("log file not found")
  98. }
  99. }