1
0

logviewer.go 2.6 KB

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