logger.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/astaxie/beego/logs"
  6. "nginx-ui/server/models"
  7. nginx2 "nginx-ui/server/nginx"
  8. )
  9. type LoggerController struct {
  10. BaseController
  11. }
  12. // Post file upload
  13. // POST /file
  14. func (c *LoggerController) Post() {
  15. nginx, err := c.CheckNginxPermission()
  16. if err != nil {
  17. return
  18. }
  19. logs.Info("get ", nginx.Id)
  20. var req models.LoggerReq
  21. err = json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  22. if err != nil {
  23. logs.Error(err, string(c.Ctx.Input.RequestBody))
  24. c.ErrorJson(err)
  25. return
  26. }
  27. if req.MaxLines <= 0 {
  28. req.MaxLines = 1000
  29. }
  30. var cmd = fmt.Sprintf("cat %s", req.FileName)
  31. if req.End <= 0 {
  32. cmd = fmt.Sprintf("%s | head -n %d", cmd, req.MaxLines)
  33. } else if req.Start >= 0 {
  34. cmd = fmt.Sprintf("%s | head -n %d | tail -n +%d", cmd, req.End, req.Start)
  35. } else {
  36. cmd = fmt.Sprintf("%s | tail -n %d", cmd, req.MaxLines)
  37. }
  38. logs.Info("get logs cmd: ", cmd)
  39. ins := nginx2.GetInstance(nginx)
  40. resp, err := ins.Run(cmd)
  41. if err != nil {
  42. c.ErrorJson(err)
  43. return
  44. }
  45. c.setData(resp).json()
  46. }