|
| 1 | +package sites |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/0xJacky/Nginx-UI/internal/sitecheck" |
| 9 | + "github.com/0xJacky/Nginx-UI/model" |
| 10 | + "github.com/0xJacky/Nginx-UI/query" |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "github.com/spf13/cast" |
| 13 | + "github.com/uozi-tech/cosy" |
| 14 | + "github.com/uozi-tech/cosy/logger" |
| 15 | +) |
| 16 | + |
| 17 | +// GetSiteNavigation returns all sites for navigation dashboard |
| 18 | +func GetSiteNavigation(c *gin.Context) { |
| 19 | + service := sitecheck.GetService() |
| 20 | + sites := service.GetSites() |
| 21 | + |
| 22 | + c.JSON(http.StatusOK, gin.H{ |
| 23 | + "data": sites, |
| 24 | + }) |
| 25 | +} |
| 26 | + |
| 27 | +// GetSiteNavigationStatus returns the status of site checking service |
| 28 | +func GetSiteNavigationStatus(c *gin.Context) { |
| 29 | + service := sitecheck.GetService() |
| 30 | + |
| 31 | + c.JSON(http.StatusOK, gin.H{ |
| 32 | + "running": service.IsRunning(), |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +// UpdateSiteOrder updates the custom order of sites |
| 37 | +func UpdateSiteOrder(c *gin.Context) { |
| 38 | + var req struct { |
| 39 | + OrderedIds []uint64 `json:"ordered_ids" binding:"required"` |
| 40 | + } |
| 41 | + |
| 42 | + if !cosy.BindAndValid(c, &req) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + if err := updateSiteOrderBatchByIds(req.OrderedIds); err != nil { |
| 47 | + cosy.ErrHandler(c, err) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + c.JSON(http.StatusOK, gin.H{ |
| 52 | + "message": "Order updated successfully", |
| 53 | + }) |
| 54 | +} |
| 55 | + |
| 56 | +// updateSiteOrderBatchByIds updates site order in batch using IDs |
| 57 | +func updateSiteOrderBatchByIds(orderedIds []uint64) error { |
| 58 | + sc := query.SiteConfig |
| 59 | + |
| 60 | + for i, id := range orderedIds { |
| 61 | + if _, err := sc.Where(sc.ID.Eq(id)).Update(sc.CustomOrder, i); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// GetHealthCheck gets health check configuration for a site |
| 70 | +func GetHealthCheck(c *gin.Context) { |
| 71 | + id := cast.ToUint64(c.Param("id")) |
| 72 | + |
| 73 | + sc := query.SiteConfig |
| 74 | + siteConfig, err := sc.Where(sc.ID.Eq(id)).First() |
| 75 | + if err != nil { |
| 76 | + cosy.ErrHandler(c, err) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + ensureHealthCheckConfig(siteConfig) |
| 81 | + |
| 82 | + c.JSON(http.StatusOK, siteConfig) |
| 83 | +} |
| 84 | + |
| 85 | +// createDefaultHealthCheckConfig creates default health check configuration |
| 86 | +func createDefaultHealthCheckConfig() *model.HealthCheckConfig { |
| 87 | + return &model.HealthCheckConfig{ |
| 88 | + Protocol: "http", |
| 89 | + Method: "GET", |
| 90 | + Path: "/", |
| 91 | + ExpectedStatus: []int{200}, |
| 92 | + GRPCMethod: "Check", |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// ensureHealthCheckConfig ensures health check config is not nil |
| 97 | +func ensureHealthCheckConfig(siteConfig *model.SiteConfig) { |
| 98 | + if siteConfig.HealthCheckConfig == nil { |
| 99 | + siteConfig.HealthCheckConfig = createDefaultHealthCheckConfig() |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// UpdateHealthCheck updates health check configuration for a site |
| 104 | +func UpdateHealthCheck(c *gin.Context) { |
| 105 | + id := cast.ToUint64(c.Param("id")) |
| 106 | + |
| 107 | + var req model.SiteConfig |
| 108 | + |
| 109 | + if !cosy.BindAndValid(c, &req) { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + sc := query.SiteConfig |
| 114 | + siteConfig, err := sc.Where(sc.ID.Eq(id)).First() |
| 115 | + if err != nil { |
| 116 | + cosy.ErrHandler(c, err) |
| 117 | + return |
| 118 | + } |
| 119 | + |
| 120 | + siteConfig.HealthCheckEnabled = req.HealthCheckEnabled |
| 121 | + siteConfig.CheckInterval = req.CheckInterval |
| 122 | + siteConfig.Timeout = req.Timeout |
| 123 | + siteConfig.UserAgent = req.UserAgent |
| 124 | + siteConfig.MaxRedirects = req.MaxRedirects |
| 125 | + siteConfig.FollowRedirects = req.FollowRedirects |
| 126 | + siteConfig.CheckFavicon = req.CheckFavicon |
| 127 | + |
| 128 | + if req.HealthCheckConfig != nil { |
| 129 | + siteConfig.HealthCheckConfig = req.HealthCheckConfig |
| 130 | + } |
| 131 | + |
| 132 | + if err = query.SiteConfig.Save(siteConfig); err != nil { |
| 133 | + cosy.ErrHandler(c, err) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + c.JSON(http.StatusOK, gin.H{ |
| 138 | + "message": "Health check configuration updated successfully", |
| 139 | + }) |
| 140 | +} |
| 141 | + |
| 142 | +// TestHealthCheck tests a health check configuration without saving it |
| 143 | +func TestHealthCheck(c *gin.Context) { |
| 144 | + id := cast.ToUint64(c.Param("id")) |
| 145 | + |
| 146 | + var req struct { |
| 147 | + Config *model.HealthCheckConfig `json:"config" binding:"required"` |
| 148 | + } |
| 149 | + |
| 150 | + if !cosy.BindAndValid(c, &req) { |
| 151 | + return |
| 152 | + } |
| 153 | + |
| 154 | + // Get site config to determine the host for testing |
| 155 | + sc := query.SiteConfig |
| 156 | + siteConfig, err := sc.Where(sc.ID.Eq(id)).First() |
| 157 | + if err != nil { |
| 158 | + cosy.ErrHandler(c, err) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + // Create enhanced checker and test the configuration |
| 163 | + enhancedChecker := sitecheck.NewEnhancedSiteChecker() |
| 164 | + |
| 165 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 166 | + defer cancel() |
| 167 | + |
| 168 | + // Convert host to URL for testing |
| 169 | + testURL := siteConfig.Scheme + "://" + siteConfig.Host |
| 170 | + result, err := enhancedChecker.CheckSiteWithConfig(ctx, testURL, req.Config) |
| 171 | + |
| 172 | + if err != nil { |
| 173 | + logger.Errorf("Health check test failed for %s: %v", siteConfig.Host, err) |
| 174 | + c.JSON(http.StatusOK, gin.H{ |
| 175 | + "success": false, |
| 176 | + "error": err.Error(), |
| 177 | + "response_time": 0, |
| 178 | + }) |
| 179 | + return |
| 180 | + } |
| 181 | + |
| 182 | + success := result.Status == "online" |
| 183 | + errorMsg := "" |
| 184 | + if !success && result.Error != "" { |
| 185 | + errorMsg = result.Error |
| 186 | + } |
| 187 | + |
| 188 | + c.JSON(http.StatusOK, gin.H{ |
| 189 | + "success": success, |
| 190 | + "response_time": result.ResponseTime, |
| 191 | + "status": result.Status, |
| 192 | + "status_code": result.StatusCode, |
| 193 | + "error": errorMsg, |
| 194 | + }) |
| 195 | +} |
0 commit comments