Files
oc-catalog/controllers/general.go

112 lines
2.3 KiB
Go
Raw Normal View History

2026-01-12 14:37:37 +01:00
package controllers
import (
2026-03-13 10:28:24 +01:00
"context"
2026-04-01 15:56:05 +02:00
"encoding/json"
2026-03-13 10:28:24 +01:00
"fmt"
2026-01-27 15:39:53 +01:00
"oc-catalog/infrastructure"
2026-01-12 14:37:37 +01:00
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"
2026-04-01 15:56:05 +02:00
"github.com/gorilla/websocket"
2026-01-12 14:37:37 +01:00
)
// Operations about compute
type GeneralController struct {
beego.Controller
}
// @Title GetAll
// @Description find compute by id
// @Param file formData file true "File to upload"
// @Success 200 {compute} models.workflow
// @Failure 406 {string} string "Bad request"
// @router / [get]
func (o *GeneralController) GetAll() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
file, _, err := o.Ctx.Request.FormFile("file")
if err != nil {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 406,
"error": err.Error(),
}
o.ServeJSON()
return
}
newWorkflow := &w.Workflow{}
req := &tools.APIRequest{
Username: user,
PeerID: peerID,
Groups: groups,
2026-03-18 09:58:20 +01:00
Admin: true,
2026-01-12 14:37:37 +01:00
}
newWorkflow, err = newWorkflow.ExtractFromPlantUML(file, req)
if err != nil {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 406,
"error": err.Error(),
}
o.ServeJSON()
return
}
o.Data["json"] = map[string]interface{}{
"data": newWorkflow,
"code": 200,
"error": nil,
}
o.ServeJSON()
}
2026-01-27 15:39:53 +01:00
2026-04-01 15:56:05 +02:00
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 {
if err := conn.ReadJSON(&discard); err != nil {
close(done)
return
2026-03-13 10:28:24 +01:00
}
2026-04-01 15:56:05 +02:00
}
}()
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
2026-03-12 16:20:47 +01:00
}
2026-04-01 15:56:05 +02:00
m := map[string]interface{}{}
if err := json.Unmarshal(msg, &m); err == nil {
if err := conn.WriteJSON(m); err != nil {
2026-03-13 10:28:24 +01:00
continue
}
2026-04-01 15:56:05 +02:00
} else {
continue
2026-01-27 15:39:53 +01:00
}
2026-04-01 15:56:05 +02:00
case <-done:
return
case <-ctx.Done():
return
2026-01-27 15:39:53 +01:00
}
2026-04-01 15:56:05 +02:00
}
2026-01-27 15:39:53 +01:00
}