config_args.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package nginx
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/logger"
  4. "github.com/0xJacky/Nginx-UI/settings"
  5. "os/exec"
  6. "path/filepath"
  7. "regexp"
  8. )
  9. func getNginxV() string {
  10. out, err := exec.Command("nginx", "-V").CombinedOutput()
  11. if err != nil {
  12. logger.Error(err)
  13. return ""
  14. }
  15. return string(out)
  16. }
  17. func GetConfPath(dir ...string) (confPath string) {
  18. if settings.NginxSettings.ConfigDir == "" {
  19. out := getNginxV()
  20. r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
  21. match := r.FindStringSubmatch(out)
  22. if len(match) < 1 {
  23. logger.Error("nginx.GetConfPath len(match) < 1")
  24. return ""
  25. }
  26. confPath = match[1]
  27. } else {
  28. confPath = settings.NginxSettings.ConfigDir
  29. }
  30. return filepath.Join(confPath, filepath.Join(dir...))
  31. }
  32. func GetPIDPath() (path string) {
  33. if settings.NginxSettings.PIDPath == "" {
  34. out := getNginxV()
  35. r, _ := regexp.Compile("--pid-path=(.*.pid)")
  36. match := r.FindStringSubmatch(out)
  37. if len(match) < 1 {
  38. logger.Error("nginx.GetPIDPath len(match) < 1")
  39. return ""
  40. }
  41. path = match[1]
  42. } else {
  43. path = settings.NginxSettings.PIDPath
  44. }
  45. return path
  46. }
  47. func GetSbinPath() (path string) {
  48. out := getNginxV()
  49. r, _ := regexp.Compile("--sbin-path=(\\S+)")
  50. match := r.FindStringSubmatch(out)
  51. if len(match) < 1 {
  52. logger.Error("nginx.GetPIDPath len(match) < 1")
  53. return ""
  54. }
  55. path = match[1]
  56. return path
  57. }
  58. func GetAccessLogPath() (path string) {
  59. if settings.NginxSettings.AccessLogPath == "" {
  60. out := getNginxV()
  61. r, _ := regexp.Compile("--http-log-path=(\\S+)")
  62. match := r.FindStringSubmatch(out)
  63. if len(match) < 1 {
  64. logger.Error("nginx.GetAccessLogPath len(match) < 1")
  65. return ""
  66. }
  67. path = match[1]
  68. } else {
  69. path = settings.NginxSettings.AccessLogPath
  70. }
  71. return path
  72. }
  73. func GetErrorLogPath() string {
  74. if settings.NginxSettings.ErrorLogPath == "" {
  75. out := getNginxV()
  76. r, _ := regexp.Compile("--error-log-path=(\\S+)")
  77. match := r.FindStringSubmatch(out)
  78. if len(match) < 1 {
  79. logger.Error("nginx.GetErrorLogPath len(match) < 1")
  80. return ""
  81. }
  82. return match[1]
  83. } else {
  84. return settings.NginxSettings.ErrorLogPath
  85. }
  86. }