tasks.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package self_check
  2. import (
  3. "github.com/0xJacky/Nginx-UI/internal/helper"
  4. "github.com/0xJacky/Nginx-UI/internal/translation"
  5. "github.com/elliotchance/orderedmap/v3"
  6. "github.com/uozi-tech/cosy"
  7. )
  8. type Task struct {
  9. Key string
  10. Name *translation.Container
  11. Description *translation.Container
  12. CheckFunc func() error
  13. FixFunc func() error
  14. }
  15. type ReportStatus string
  16. const (
  17. ReportStatusSuccess ReportStatus = "success"
  18. ReportStatusWarning ReportStatus = "warning"
  19. ReportStatusError ReportStatus = "error"
  20. )
  21. type Report struct {
  22. Name *translation.Container `json:"name"`
  23. Description *translation.Container `json:"description,omitempty"`
  24. Fixable bool `json:"fixable"`
  25. Err *cosy.Error `json:"err,omitempty"`
  26. Status ReportStatus `json:"status"`
  27. }
  28. type Reports []*Report
  29. var selfCheckTasks = []*Task{
  30. {
  31. Key: "Directory-Sites",
  32. Name: translation.C("Sites directory exists"),
  33. Description: translation.C("Check if the " +
  34. "sites-available and sites-enabled directories are " +
  35. "under the nginx configuration directory"),
  36. CheckFunc: CheckSitesDirectory,
  37. FixFunc: FixSitesDirectory,
  38. },
  39. {
  40. Key: "Directory-Streams",
  41. Name: translation.C("Streams directory exists"),
  42. Description: translation.C("Check if the " +
  43. "streams-available and streams-enabled directories are " +
  44. "under the nginx configuration directory"),
  45. CheckFunc: CheckStreamDirectory,
  46. FixFunc: FixStreamDirectory,
  47. },
  48. {
  49. Key: "NginxConf-Sites-Enabled",
  50. Name: translation.C("Nginx.conf includes sites-enabled directory"),
  51. Description: translation.C("Check if the nginx.conf includes the " +
  52. "sites-enabled directory"),
  53. CheckFunc: CheckNginxConfIncludeSites,
  54. FixFunc: FixNginxConfIncludeSites,
  55. },
  56. {
  57. Key: "NginxConf-Streams-Enabled",
  58. Name: translation.C("Nginx.conf includes streams-enabled directory"),
  59. Description: translation.C("Check if the nginx.conf includes the " +
  60. "streams-enabled directory"),
  61. CheckFunc: CheckNginxConfIncludeStreams,
  62. FixFunc: FixNginxConfIncludeStreams,
  63. },
  64. {
  65. Key: "NginxConf-ConfD",
  66. Name: translation.C("Nginx.conf includes conf.d directory"),
  67. Description: translation.C("Check if the nginx.conf includes the " +
  68. "conf.d directory"),
  69. CheckFunc: CheckNginxConfIncludeConfD,
  70. FixFunc: FixNginxConfIncludeConfD,
  71. },
  72. {
  73. Key: "NginxConf-Directory",
  74. Name: translation.C("Nginx configuration directory exists"),
  75. Description: translation.C("Check if the nginx configuration directory exists"),
  76. CheckFunc: CheckConfigDir,
  77. },
  78. {
  79. Key: "NginxConf-Entry-File",
  80. Name: translation.C("Nginx configuration entry file exists"),
  81. Description: translation.C("Check if the nginx configuration entry file exists"),
  82. CheckFunc: CheckConfigEntryFile,
  83. },
  84. {
  85. Key: "NginxPID-Path",
  86. Name: translation.C("Nginx PID path exists"),
  87. Description: translation.C("Check if the nginx PID path exists"),
  88. CheckFunc: CheckPIDPath,
  89. },
  90. {
  91. Key: "NginxAccessLog-Path",
  92. Name: translation.C("Nginx access log path exists"),
  93. Description: translation.C("Check if the nginx access log path exists"),
  94. CheckFunc: CheckAccessLogPath,
  95. },
  96. {
  97. Key: "NginxErrorLog-Path",
  98. Name: translation.C("Nginx error log path exists"),
  99. Description: translation.C("Check if the nginx error log path exists"),
  100. CheckFunc: CheckErrorLogPath,
  101. },
  102. }
  103. var selfCheckTaskMap = orderedmap.NewOrderedMap[string, *Task]()
  104. func init() {
  105. for _, task := range selfCheckTasks {
  106. selfCheckTaskMap.Set(task.Key, task)
  107. }
  108. if helper.InNginxUIOfficialDocker() {
  109. selfCheckTasks = append(selfCheckTasks, &Task{
  110. Name: translation.C("Docker socket exists"),
  111. Description: translation.C("Check if the docker socket exists."),
  112. CheckFunc: CheckDockerSocket,
  113. })
  114. }
  115. }