logviewer.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/arozos/mod/filesystem/arozfs"
  11. "imuslab.com/arozos/mod/utils"
  12. )
  13. type ViewerOption struct {
  14. RootFolder string //The root folder to scan for log
  15. Extension string //The extension the root files use, include the . in your ext (e.g. .log)
  16. }
  17. type Viewer struct {
  18. option *ViewerOption
  19. }
  20. type LogFile struct {
  21. Title string
  22. Filename string
  23. Fullpath string
  24. Filesize int64
  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. js, _ := json.Marshal(logFiles)
  36. utils.SendJSONResponse(w, string(js))
  37. }
  38. // Read log of a given catergory and filename
  39. //Require GET varaible: file and catergory
  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. st, err := os.Stat(path)
  76. if err != nil {
  77. return nil
  78. }
  79. logList = append(logList, &LogFile{
  80. Title: strings.TrimSuffix(filepath.Base(path), filepath.Ext(path)),
  81. Filename: filepath.Base(path),
  82. Fullpath: fullpath,
  83. Filesize: st.Size(),
  84. })
  85. result[catergory] = logList
  86. }
  87. return nil
  88. })
  89. return result
  90. }
  91. func (v *Viewer) LoadLogFile(catergory string, filename string) (string, error) {
  92. logFilepath := filepath.Join(v.option.RootFolder, catergory, filename)
  93. if utils.FileExists(logFilepath) {
  94. //Load it
  95. content, err := os.ReadFile(logFilepath)
  96. if err != nil {
  97. return "", err
  98. }
  99. return string(content), nil
  100. } else {
  101. return "", errors.New("log file not found")
  102. }
  103. }