list.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package sites
  2. import (
  3. "net/http"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/0xJacky/Nginx-UI/internal/config"
  8. "github.com/0xJacky/Nginx-UI/internal/nginx"
  9. "github.com/0xJacky/Nginx-UI/model"
  10. "github.com/0xJacky/Nginx-UI/query"
  11. "github.com/gin-gonic/gin"
  12. "github.com/samber/lo"
  13. "github.com/spf13/cast"
  14. "github.com/uozi-tech/cosy"
  15. )
  16. func GetSiteList(c *gin.Context) {
  17. name := c.Query("name")
  18. enabled := c.Query("enabled")
  19. orderBy := c.Query("sort_by")
  20. sort := c.DefaultQuery("order", "desc")
  21. querySiteCategoryId := cast.ToUint64(c.Query("site_category_id"))
  22. configFiles, err := os.ReadDir(nginx.GetConfPath("sites-available"))
  23. if err != nil {
  24. cosy.ErrHandler(c, err)
  25. return
  26. }
  27. enabledConfig, err := os.ReadDir(nginx.GetConfPath("sites-enabled"))
  28. if err != nil {
  29. cosy.ErrHandler(c, err)
  30. return
  31. }
  32. s := query.Site
  33. sTx := s.Preload(s.SiteCategory)
  34. if querySiteCategoryId != 0 {
  35. sTx.Where(s.SiteCategoryID.Eq(querySiteCategoryId))
  36. }
  37. sites, err := sTx.Find()
  38. if err != nil {
  39. cosy.ErrHandler(c, err)
  40. return
  41. }
  42. sitesMap := lo.SliceToMap(sites, func(item *model.Site) (string, *model.Site) {
  43. return filepath.Base(item.Path), item
  44. })
  45. enabledConfigMap := make(map[string]bool)
  46. for i := range enabledConfig {
  47. enabledConfigMap[enabledConfig[i].Name()] = true
  48. }
  49. var configs []config.Config
  50. for i := range configFiles {
  51. file := configFiles[i]
  52. fileInfo, _ := file.Info()
  53. if file.IsDir() {
  54. continue
  55. }
  56. // name filter
  57. if name != "" && !strings.Contains(file.Name(), name) {
  58. continue
  59. }
  60. // status filter
  61. if enabled != "" {
  62. if enabled == "true" && !enabledConfigMap[file.Name()] {
  63. continue
  64. }
  65. if enabled == "false" && enabledConfigMap[file.Name()] {
  66. continue
  67. }
  68. }
  69. var (
  70. siteCategoryId uint64
  71. siteCategory *model.SiteCategory
  72. )
  73. if site, ok := sitesMap[file.Name()]; ok {
  74. siteCategoryId = site.SiteCategoryID
  75. siteCategory = site.SiteCategory
  76. }
  77. // site category filter
  78. if querySiteCategoryId != 0 && siteCategoryId != querySiteCategoryId {
  79. continue
  80. }
  81. configs = append(configs, config.Config{
  82. Name: file.Name(),
  83. ModifiedAt: fileInfo.ModTime(),
  84. Size: fileInfo.Size(),
  85. IsDir: fileInfo.IsDir(),
  86. Enabled: enabledConfigMap[file.Name()],
  87. SiteCategoryID: siteCategoryId,
  88. SiteCategory: siteCategory,
  89. })
  90. }
  91. configs = config.Sort(orderBy, sort, configs)
  92. c.JSON(http.StatusOK, gin.H{
  93. "data": configs,
  94. })
  95. }