|
| 1 | +package upstream |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "sort" |
| 6 | + |
| 7 | + "github.com/0xJacky/Nginx-UI/internal/upstream" |
| 8 | + "github.com/0xJacky/Nginx-UI/model" |
| 9 | + "github.com/0xJacky/Nginx-UI/query" |
| 10 | + "github.com/gin-gonic/gin" |
| 11 | + "github.com/uozi-tech/cosy" |
| 12 | + "github.com/uozi-tech/cosy/logger" |
| 13 | +) |
| 14 | + |
| 15 | +// SocketInfo represents a socket with its configuration and health status |
| 16 | +type SocketInfo struct { |
| 17 | + Socket string `json:"socket"` // host:port |
| 18 | + Host string `json:"host"` // hostname/IP |
| 19 | + Port string `json:"port"` // port number |
| 20 | + Type string `json:"type"` // proxy_pass, grpc_pass, or upstream |
| 21 | + IsConsul bool `json:"is_consul"` // whether this is a consul service |
| 22 | + UpstreamName string `json:"upstream_name"` // which upstream this belongs to (if any) |
| 23 | + LastCheck string `json:"last_check"` // last time health check was performed |
| 24 | + Status *upstream.Status `json:"status"` // health check status |
| 25 | + Enabled bool `json:"enabled"` // whether health check is enabled |
| 26 | +} |
| 27 | + |
| 28 | +// GetSocketList returns all sockets with their configuration and health status |
| 29 | +func GetSocketList(c *gin.Context) { |
| 30 | + service := upstream.GetUpstreamService() |
| 31 | + |
| 32 | + // Get all target infos |
| 33 | + targets := service.GetTargetInfos() |
| 34 | + |
| 35 | + // Get availability map |
| 36 | + availabilityMap := service.GetAvailabilityMap() |
| 37 | + |
| 38 | + // Get all socket configurations from database |
| 39 | + u := query.UpstreamConfig |
| 40 | + configs, err := u.Find() |
| 41 | + if err != nil { |
| 42 | + cosy.ErrHandler(c, err) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + // Create a map for quick lookup of enabled status |
| 47 | + configMap := make(map[string]bool) |
| 48 | + for _, config := range configs { |
| 49 | + configMap[config.Socket] = config.Enabled |
| 50 | + } |
| 51 | + |
| 52 | + // Build response |
| 53 | + result := make([]SocketInfo, 0, len(targets)) |
| 54 | + for _, target := range targets { |
| 55 | + socketAddr := formatSocketAddress(target.Host, target.Port) |
| 56 | + |
| 57 | + // Get enabled status from database, default to true if not found |
| 58 | + enabled := true |
| 59 | + if val, exists := configMap[socketAddr]; exists { |
| 60 | + enabled = val |
| 61 | + } |
| 62 | + |
| 63 | + // Get health status |
| 64 | + var status *upstream.Status |
| 65 | + if s, exists := availabilityMap[socketAddr]; exists { |
| 66 | + status = s |
| 67 | + } |
| 68 | + |
| 69 | + // Find which upstream this belongs to |
| 70 | + upstreamName := findUpstreamForSocket(service, target.ProxyTarget) |
| 71 | + |
| 72 | + info := SocketInfo{ |
| 73 | + Socket: socketAddr, |
| 74 | + Host: target.Host, |
| 75 | + Port: target.Port, |
| 76 | + Type: target.Type, |
| 77 | + IsConsul: target.IsConsul, |
| 78 | + UpstreamName: upstreamName, |
| 79 | + LastCheck: target.LastSeen.Format("2006-01-02 15:04:05"), |
| 80 | + Status: status, |
| 81 | + Enabled: enabled, |
| 82 | + } |
| 83 | + result = append(result, info) |
| 84 | + } |
| 85 | + |
| 86 | + // Sort by socket address for stable ordering |
| 87 | + sort.Slice(result, func(i, j int) bool { |
| 88 | + return result[i].Socket < result[j].Socket |
| 89 | + }) |
| 90 | + |
| 91 | + c.JSON(http.StatusOK, gin.H{ |
| 92 | + "data": result, |
| 93 | + }) |
| 94 | +} |
| 95 | + |
| 96 | +// UpdateSocketConfigRequest represents the request body for updating socket config |
| 97 | +type UpdateSocketConfigRequest struct { |
| 98 | + Enabled bool `json:"enabled"` |
| 99 | +} |
| 100 | + |
| 101 | +// UpdateSocketConfig updates the enabled status of a socket |
| 102 | +func UpdateSocketConfig(c *gin.Context) { |
| 103 | + socket := c.Param("socket") |
| 104 | + |
| 105 | + var req UpdateSocketConfigRequest |
| 106 | + if err := c.ShouldBindJSON(&req); err != nil { |
| 107 | + cosy.ErrHandler(c, err) |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + u := query.UpstreamConfig |
| 112 | + |
| 113 | + // Check if config exists |
| 114 | + config, err := u.Where(u.Socket.Eq(socket)).First() |
| 115 | + if err != nil { |
| 116 | + // Create new config if not found |
| 117 | + config = &model.UpstreamConfig{ |
| 118 | + Socket: socket, |
| 119 | + Enabled: req.Enabled, |
| 120 | + } |
| 121 | + if err := u.Create(config); err != nil { |
| 122 | + logger.Error("Failed to create socket config:", err) |
| 123 | + cosy.ErrHandler(c, err) |
| 124 | + return |
| 125 | + } |
| 126 | + } else { |
| 127 | + // Update existing config |
| 128 | + if _, err := u.Where(u.Socket.Eq(socket)).Update(u.Enabled, req.Enabled); err != nil { |
| 129 | + logger.Error("Failed to update socket config:", err) |
| 130 | + cosy.ErrHandler(c, err) |
| 131 | + return |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + c.JSON(http.StatusOK, gin.H{ |
| 136 | + "message": "Socket config updated successfully", |
| 137 | + }) |
| 138 | +} |
| 139 | + |
| 140 | +// findUpstreamForSocket finds which upstream a socket belongs to |
| 141 | +func findUpstreamForSocket(service *upstream.Service, target upstream.ProxyTarget) string { |
| 142 | + socketAddr := formatSocketAddress(target.Host, target.Port) |
| 143 | + upstreams := service.GetAllUpstreamDefinitions() |
| 144 | + |
| 145 | + for name, upstream := range upstreams { |
| 146 | + for _, server := range upstream.Servers { |
| 147 | + serverAddr := formatSocketAddress(server.Host, server.Port) |
| 148 | + if serverAddr == socketAddr { |
| 149 | + return name |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + return "" |
| 154 | +} |
| 155 | + |
0 commit comments