logviewer.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. catergory, err := utils.GetPara(r, "catergory")
  46. if err != nil {
  47. utils.SendErrorResponse(w, "invalid catergory given")
  48. return
  49. }
  50. content, err := v.LoadLogFile(strings.TrimSpace(filepath.Base(catergory)), strings.TrimSpace(filepath.Base(filename)))
  51. if err != nil {
  52. utils.SendErrorResponse(w, err.Error())
  53. return
  54. }
  55. utils.SendTextResponse(w, content)
  56. }
  57. /*
  58. Log Access Functions
  59. */
  60. func (v *Viewer) ListLogFiles(showFullpath bool) map[string][]*LogFile {
  61. result := map[string][]*LogFile{}
  62. filepath.WalkDir(v.option.RootFolder, func(path string, di fs.DirEntry, err error) error {
  63. if filepath.Ext(path) == v.option.Extension {
  64. catergory := filepath.Base(filepath.Dir(path))
  65. logList, ok := result[catergory]
  66. if !ok {
  67. //this catergory hasn't been scanned before.
  68. logList = []*LogFile{}
  69. }
  70. fullpath := filepath.ToSlash(path)
  71. if !showFullpath {
  72. fullpath = ""
  73. }
  74. st, err := os.Stat(path)
  75. if err != nil {
  76. return nil
  77. }
  78. logList = append(logList, &LogFile{
  79. Title: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)),
  80. Filename: filepath.Base(path),
  81. Fullpath: fullpath,
  82. Filesize: st.Size(),
  83. })
  84. result[catergory] = logList
  85. }
  86. return nil
  87. })
  88. return result
  89. }
  90. func (v *Viewer) LoadLogFile(catergory string, filename string) (string, error) {
  91. logFilepath := filepath.Join(v.option.RootFolder, catergory, filename)
  92. if utils.FileExists(logFilepath) {
  93. //Load it
  94. content, err := os.ReadFile(logFilepath)
  95. if err != nil {
  96. return "", err
  97. }
  98. return string(content), nil
  99. } else {
  100. return "", errors.New("log file not found")
  101. }
  102. }