-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
app_server_node_connection.go
112 lines (93 loc) · 2.58 KB
/
app_server_node_connection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package generator
import (
"encoding/json"
"fmt"
"net/http"
"github.com/EliCDavis/polyform/nodes"
"github.com/EliCDavis/polyform/refutil"
)
type DeleteNodeConnectionRequest struct {
NodeId string `json:"nodeId"`
InPortName string `json:"inPortName"`
}
type DeleteNodeConnectionResponse struct {
}
type CreateNodeConnectionRequest struct {
NodeOutId string `json:"nodeOutId"`
OutPortName string `json:"outPortName"`
NodeInId string `json:"nodeInId"`
InPortName string `json:"inPortName"`
}
type CreateNodeConnectionResponse struct {
}
func (as *AppServer) NodeConnectionEndpoint(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response any
var err error
switch r.Method {
case "POST":
createRequest, castErr := readJSON[CreateNodeConnectionRequest](r.Body)
if castErr != nil {
w.WriteHeader(http.StatusInternalServerError)
writeJSONError(w, castErr)
return
}
response, err = as.nodeConnectionEndpoint_post(createRequest)
case "DELETE":
createRequest, castErr := readJSON[DeleteNodeConnectionRequest](r.Body)
if castErr != nil {
w.WriteHeader(http.StatusInternalServerError)
writeJSONError(w, castErr)
return
}
response, err = as.nodeConnectionEndpoint_delete(createRequest)
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
writeJSONError(w, err)
} else {
data, err := json.Marshal(response)
if err != nil {
panic(err)
}
w.Write(data)
}
}
func (as *AppServer) nodeConnectionEndpoint_post(req CreateNodeConnectionRequest) (resp CreateNodeConnectionResponse, err error) {
defer func() {
if recErr := recover(); recErr != nil {
err = fmt.Errorf("panic recover: %v", recErr)
}
}()
inNode := as.app.nodeFromID(req.NodeInId)
outNode := as.app.nodeFromID(req.NodeOutId)
outPortVals := refutil.CallFuncValuesOfType(outNode, req.OutPortName)
// log.Printf("%#v", inNode)
// log.Printf("%#v", outNode)
// log.Printf("%#v", outPortVals)
ref := outPortVals[0].(nodes.NodeOutputReference)
inNode.SetInput(
req.InPortName,
nodes.Output{
NodeOutput: ref,
},
)
as.incModelVersion()
return CreateNodeConnectionResponse{}, nil
}
func (as *AppServer) nodeConnectionEndpoint_delete(req DeleteNodeConnectionRequest) (resp DeleteNodeConnectionResponse, err error) {
defer func() {
if recErr := recover(); recErr != nil {
err = fmt.Errorf("panic recover: %v", recErr)
}
}()
inNode := as.app.nodeFromID(req.NodeId)
inNode.SetInput(
req.InPortName,
nodes.Output{
NodeOutput: nil,
},
)
as.incModelVersion()
return DeleteNodeConnectionResponse{}, nil
}