logviewer.go 2.6 KB

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