/
render.go
153 lines (139 loc) · 4.69 KB
/
render.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package component_protocol
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/bundle"
"github.com/erda-project/erda/modules/openapi/api/apis"
protocol "github.com/erda-project/erda/modules/openapi/component-protocol"
_ "github.com/erda-project/erda/modules/openapi/component-protocol/scenarios/action/components/actionForm"
"github.com/erda-project/erda/modules/openapi/hooks/posthandle"
"github.com/erda-project/erda/modules/openapi/i18n"
"github.com/erda-project/erda/pkg/http/httpclient"
"github.com/erda-project/erda/pkg/http/httpserver"
"github.com/erda-project/erda/pkg/http/httpserver/errorresp"
i18npkg "github.com/erda-project/erda/pkg/i18n"
"github.com/erda-project/erda/pkg/strutil"
)
var Render = apis.ApiSpec{
Path: "/api/component-protocol/actions/render",
Scheme: "http",
Method: "POST",
Custom: protocolRender,
RequestType: apistructs.ComponentProtocolRequest{},
ResponseType: apistructs.ComponentProtocolResponse{},
Doc: "某场景下,用户操作,触发后端业务逻辑,重新渲染协议",
CheckLogin: true,
CheckToken: true,
IsOpenAPI: true,
}
func protocolRender(w http.ResponseWriter, r *http.Request) {
req := apistructs.ComponentProtocolRequest{}
d := json.NewDecoder(r.Body)
if err := d.Decode(&req); err != nil {
err := fmt.Errorf("decode compent render request failed, error: %v", err)
logrus.Errorf("%s, buffered:%v", err.Error(), d.Buffered())
_ = errorresp.ErrWrite(err, w)
return
}
logrus.Infof("request header:%v", r.Header)
var bundleOpts = []bundle.Option{
bundle.WithHTTPClient(
httpclient.New(
httpclient.WithTimeout(time.Second, time.Second*90),
httpclient.WithEnableAutoRetry(false),
)),
bundle.WithAllAvailableClients(),
}
bdl := bundle.New(bundleOpts...)
i18nPrinter := i18n.I18nPrinter(r)
// get locale from request
locale := i18npkg.GetLocaleNameByRequest(r)
// UserID 来自session, OrgID 来自url xxx-org.xx
i, _ := GetIdentity(r)
ctxBdl := protocol.ContextBundle{
Bdl: bdl,
I18nPrinter: i18nPrinter,
Identity: i,
InParams: req.InParams,
Locale: locale,
}
ctx := context.Background()
ctx1 := context.WithValue(ctx, protocol.GlobalInnerKeyCtxBundle.String(), ctxBdl)
err := protocol.RunScenarioRender(ctx1, &req)
if err != nil {
err := fmt.Errorf("run scenario render failed: %v", err)
logrus.Errorf("%s, scenario: %+v, event: %+v", err.Error(), req.Scenario, req.Event)
_ = errorresp.ErrWrite(err, w)
return
}
rsp := RenderResponse(&req)
httpserver.WriteJSON(w, rsp)
}
func RenderResponse(req *apistructs.ComponentProtocolRequest) *apistructs.ComponentProtocolResponse {
rsp := apistructs.ComponentProtocolResponse{
Header: apistructs.Header{
Success: true,
},
Data: apistructs.ComponentProtocolResponseData{
Scenario: req.Scenario,
Protocol: *req.Protocol,
},
}
userIDs := protocol.GetGlobalStateKV(req.Protocol, protocol.GlobalInnerKeyUserIDs.String())
if userIDs != nil {
rsp.UserIDs = userIDs.([]string)
rsp.UserIDs = strutil.DedupSlice(rsp.UserIDs, true)
delete(*req.Protocol.GlobalState, protocol.GlobalInnerKeyUserIDs.String())
}
if rsp.UserIDs != nil {
userInfo, err := posthandle.GetUsers(rsp.UserIDs, false)
if err != nil {
logrus.Warnf("component protocol get users failed, err:%v", err)
}
rsp.UserInfo = userInfo
}
err := protocol.GetGlobalStateKV(req.Protocol, protocol.GlobalInnerKeyError.String())
if err != nil {
errStr, ok := err.(string)
if ok && len(errStr) > 0 {
rsp.Error = apistructs.ErrorResponse{
Code: strconv.Itoa(http.StatusInternalServerError),
Msg: err.(string),
}
rsp.Success = false
}
}
return &rsp
}
func GetIdentity(r *http.Request) (i apistructs.Identity, err error) {
uid := r.Header.Get("User-ID")
oid := r.Header.Get("Org-ID")
if uid == "" || oid == "" {
if uid == "" {
err = fmt.Errorf("failed to get user id in http header")
} else {
err = fmt.Errorf("failed to get org id in http header")
}
}
i.OrgID = oid
i.UserID = uid
return
}