Simplify but Complete Catalog
This commit is contained in:
@@ -2,15 +2,15 @@ package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"oc-catalog/infrastructure"
|
||||
|
||||
oclib "cloud.o-forge.io/core/oc-lib"
|
||||
w "cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||
tools "cloud.o-forge.io/core/oc-lib/tools"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
"golang.org/x/net/websocket"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// Operations about compute
|
||||
@@ -63,47 +63,49 @@ func (o *GeneralController) GetAll() {
|
||||
o.ServeJSON()
|
||||
}
|
||||
|
||||
func Websocket(ctx context.Context, user string, groups []string, dataType int, r http.ResponseWriter, w *http.Request) {
|
||||
websocket.Handler(func(ws *websocket.Conn) {
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
var discard interface{}
|
||||
for {
|
||||
if websocket.JSON.Receive(ws, &discard) != nil {
|
||||
close(done)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
defer func() {
|
||||
ws.Close()
|
||||
if ch, ok := infrastructure.SearchStream[user]; ok {
|
||||
close(ch)
|
||||
infrastructure.SearchMu.Lock()
|
||||
delete(infrastructure.SearchStream, user)
|
||||
infrastructure.SearchMu.Unlock()
|
||||
}
|
||||
fmt.Println("CLOSE !")
|
||||
infrastructure.EmitNATS(user, nil, tools.PropalgationMessage{
|
||||
Action: tools.PB_CLOSE_SEARCH,
|
||||
DataType: dataType,
|
||||
})
|
||||
}()
|
||||
func Websocket(ctx context.Context, user string, groups []string, dataType int, conn *websocket.Conn) {
|
||||
defer conn.Close()
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
var discard interface{}
|
||||
for {
|
||||
select {
|
||||
case msg, ok := <-infrastructure.SearchStream[user]:
|
||||
fmt.Println("FOR", msg, ok)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if websocket.JSON.Send(ws, msg) != nil {
|
||||
continue
|
||||
}
|
||||
case <-done:
|
||||
return
|
||||
case <-ctx.Done():
|
||||
if err := conn.ReadJSON(&discard); err != nil {
|
||||
close(done)
|
||||
return
|
||||
}
|
||||
}
|
||||
}).ServeHTTP(r, w)
|
||||
}()
|
||||
defer func() {
|
||||
if ch, ok := infrastructure.SearchStream[user]; ok {
|
||||
close(ch)
|
||||
infrastructure.SearchMu.Lock()
|
||||
delete(infrastructure.SearchStream, user)
|
||||
infrastructure.SearchMu.Unlock()
|
||||
}
|
||||
fmt.Println("CLOSE !")
|
||||
infrastructure.EmitNATS(user, nil, tools.PropalgationMessage{
|
||||
Action: tools.PB_CLOSE_SEARCH,
|
||||
DataType: dataType,
|
||||
})
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case msg, ok := <-infrastructure.SearchStream[user]:
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
m := map[string]interface{}{}
|
||||
if err := json.Unmarshal(msg, &m); err == nil {
|
||||
if err := conn.WriteJSON(m); err != nil {
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
case <-done:
|
||||
return
|
||||
case <-ctx.Done():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user