Files
oc-catalog/controllers/general.go
2026-03-17 14:41:22 +01:00

144 lines
3.6 KiB
Go
Executable File

package controllers
import (
"context"
"fmt"
"net/http"
"oc-catalog/infrastructure"
"strings"
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"
)
// 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,
}
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()
}
// stringReadCloser wraps a strings.Reader to satisfy the multipart.File interface.
type stringReadCloser struct {
*strings.Reader
}
func (s *stringReadCloser) Close() error { return nil }
// parsePlantUMLText parses a raw PlantUML text body and returns the resulting Workflow.
func parsePlantUMLText(body []byte, req *tools.APIRequest) (*w.Workflow, error) {
newWorkflow := &w.Workflow{}
reader := &stringReadCloser{strings.NewReader(string(body))}
return newWorkflow.ExtractFromPlantUML(reader, req)
}
// @Title PostPlantUML
// @Description parse plantuml text and return the formed workflow object
// @Param body body string true "PlantUML text content"
// @Success 200 {object} models.workflow
// @Failure 406 {string} string "Bad request"
// @router /plantuml [post]
func (o *GeneralController) PostPlantUML() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
body := o.Ctx.Input.CopyBody(1000000)
req := &tools.APIRequest{Username: user, PeerID: peerID, Groups: groups}
wf, err := parsePlantUMLText(body, 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": wf, "code": 200, "error": nil}
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,
})
}()
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():
return
}
}
}).ServeHTTP(r, w)
}