mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
feat(wip): node selector supports sse
This commit is contained in:
parent
ed0dca6820
commit
bc70567dc1
31 changed files with 176 additions and 166 deletions
|
@ -1,6 +1,9 @@
|
|||
package cluster
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"github.com/0xJacky/Nginx-UI/internal/analytic"
|
||||
"github.com/0xJacky/Nginx-UI/internal/cluster"
|
||||
|
@ -11,7 +14,9 @@ import (
|
|||
"github.com/spf13/cast"
|
||||
"github.com/uozi-tech/cosy"
|
||||
"gorm.io/gorm"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetEnvironment(c *gin.Context) {
|
||||
|
@ -44,6 +49,85 @@ func GetEnvironmentList(c *gin.Context) {
|
|||
}).PagingList()
|
||||
}
|
||||
|
||||
func GetAllEnabledEnvironment(c *gin.Context) {
|
||||
api.SetSSEHeaders(c)
|
||||
notify := c.Writer.CloseNotify()
|
||||
|
||||
interval := 10
|
||||
|
||||
type respEnvironment struct {
|
||||
*model.Environment
|
||||
Status bool `json:"status"`
|
||||
}
|
||||
|
||||
f := func() (any, bool) {
|
||||
return cosy.Core[model.Environment](c).
|
||||
SetFussy("name").
|
||||
SetTransformer(func(m *model.Environment) any {
|
||||
resp := respEnvironment{
|
||||
Environment: m,
|
||||
Status: analytic.GetNode(m).Status,
|
||||
}
|
||||
return resp
|
||||
}).ListAllData()
|
||||
}
|
||||
|
||||
getHash := func(data any) string {
|
||||
bytes, _ := json.Marshal(data)
|
||||
hash := sha256.New()
|
||||
hash.Write(bytes)
|
||||
hashSum := hash.Sum(nil)
|
||||
return hex.EncodeToString(hashSum)
|
||||
}
|
||||
|
||||
dataHash := ""
|
||||
|
||||
{
|
||||
data, ok := f()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
c.SSEvent("message", data)
|
||||
dataHash = getHash(data)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-time.After(time.Duration(interval) * time.Second):
|
||||
data, ok := f()
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// if data is not changed, send heartbeat
|
||||
if dataHash == getHash(data) {
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
c.SSEvent("heartbeat", "")
|
||||
return false
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
dataHash = getHash(data)
|
||||
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
c.SSEvent("message", data)
|
||||
return false
|
||||
})
|
||||
case <-time.After(30 * time.Second):
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
c.SSEvent("heartbeat", "")
|
||||
return false
|
||||
})
|
||||
case <-notify:
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func AddEnvironment(c *gin.Context) {
|
||||
cosy.Core[model.Environment](c).SetValidRules(gin.H{
|
||||
"name": "required",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue