Skip to content

Commit a9244ef

Browse files
gerektoolhyDarius Damalakasdragonsinth
authored
Add "raw request gRPCurl" output (#241)
Implements [#3 Copy as "grpcurl"](#3). **Example:** ![image](https://user-images.githubusercontent.com/131102/233935060-f16c9128-50cb-43d9-b286-e69b5059c5e0.png) **With highlights of the changes parts:** ![image](https://user-images.githubusercontent.com/131102/233935026-5b6ece0a-0084-483f-b354-bceb4d20d6b0.png) --------- Co-authored-by: Darius Damalakas <[email protected]> Co-authored-by: Scott Blum <[email protected]>
1 parent b8c1e1f commit a9244ef

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ run `make install`.
7676
If you encounter compile errors, you could have out-dated versions of `grpcui`'s
7777
dependencies. You can update the dependencies by running `make updatedeps`.
7878

79+
### Running without install
80+
81+
```
82+
go run ./cmd/grpcui/grpcui.go -plaintext localhost:9019
83+
```
84+
7985
## Usage
8086
The usage doc for the tool explains the numerous options:
8187
```shell
@@ -250,4 +256,3 @@ protoc --proto_path=. \
250256
The `--descriptor_set_out` argument is what tells `protoc` to produce a protoset,
251257
and the `--include_imports` argument is necessary for the protoset to contain
252258
everything that `grpcui` needs to process and understand the schema.
253-

internal/resources/webform/webform-sample.css

+12
Original file line numberDiff line numberDiff line change
@@ -745,3 +745,15 @@ button#grpc-history-clear, button#grpc-history-save {
745745
#grpc-history-list .history-item-panel .history-detail-metadata td {
746746
padding-left: 12px;
747747
}
748+
749+
.grpc-request-raw-container {
750+
margin-top: 1em;
751+
margin-bottom: 1em;
752+
}
753+
754+
.grpc-curl-panel {
755+
background-color: #fcfcfc;
756+
border: 1px solid #dddddd;
757+
padding: 1em;
758+
margin-bottom: 1em;
759+
}

internal/resources/webform/webform-template.html

+14-3
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h3>Examples</h3>
4646
<div id="grpc-request-response">
4747
<ul>
4848
<li id="#grpc-request-tab-button"><a href="#grpc-request-tab">Request Form</a></li>
49-
<li id="#grpc-request-raw-tab-button"><a href="#grpc-request-raw-tab">Raw Request (JSON)</a></li>
49+
<li id="#grpc-request-raw-tab-button"><a href="#grpc-request-raw-tab">Raw Request</a></li>
5050
<li id="#grpc-response-tab-button"><a href="#grpc-response-tab">Response</a></li>
5151
<li id="#grpc-history-tab-button"><a href="#grpc-history-tab">History</a></li>
5252
</ul>
@@ -70,8 +70,18 @@ <h3>Request Timeout</h3>
7070
<button class="grpc-invoke" disabled>Invoke</button>
7171
</div>
7272
<div class="grpc-tabcontent" id="grpc-request-raw-tab">
73-
<textarea id="grpc-request-raw-text"></textarea><br/>
74-
<button class="grpc-invoke" disabled>Invoke</button>
73+
<div class="grpc-request-raw-container">
74+
<h3>Request payload</h3>
75+
<textarea id="grpc-request-raw-text"></textarea><br/>
76+
<button class="grpc-invoke" disabled>Invoke</button>
77+
</div>
78+
<hr>
79+
<div class="grpc-request-raw-container">
80+
<h3>gRPCurl</h3>
81+
<div id="grpc-curl" class="grpc-curl-panel">
82+
<span><pre id="grpc-curl-text"></pre></span>
83+
</div>
84+
</div>
7585
</div>
7686
<div class="grpc-tabcontent" id="grpc-response-tab">
7787
<h3>Response Headers</h3>
@@ -132,6 +142,7 @@ <h3>Response Trailers</h3>
132142
{"name": "{{.Name}}", "value": "{{.Value}}"},
133143
{{- end}}
134144
];
145+
window.target = {{ .Target }};
135146

136147
initGRPCForm(services, svcDescs, mtdDescs, '{{ .InvokeURI }}', '{{ .MetadataURI }}', {{ .Debug }}, headers);
137148
})();

internal/resources/webform/webform.js

+17-4
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ window.initGRPCForm = function(services, svcDescs, mtdDescs, invokeURI, metadata
154154
// set raw request text
155155
updateJSONRequest(requestObj);
156156

157+
// init grpcCurl text
158+
updateCurlCommand(requestObj);
159+
157160
// enable the invoke button
158161
resetInvoke(true);
159162

@@ -2087,14 +2090,24 @@ window.initGRPCForm = function(services, svcDescs, mtdDescs, invokeURI, metadata
20872090
}
20882091
}
20892092

2090-
var jsonRawTextArea = $("#grpc-request-raw-text");
2093+
let gRPCurlTextArea = $("#grpc-curl-text");
2094+
function updateCurlCommand(requestDataJson) {
2095+
let service = $("#grpc-service").val();
2096+
let method = $("#grpc-method").val();
2097+
gRPCurlTextArea.html(`<div>grpcurl -plaintext -d '${requestDataJson}' ${window.target} ${service}.${method}</div>`);
2098+
}
20912099

2100+
var jsonRawTextArea = $("#grpc-request-raw-text");
20922101
function updateJSONRequest(req) {
2093-
jsonRawTextArea.val(JSON.stringify(req, null, 2));
2102+
let requestDataJson = JSON.stringify(req, null, 2);
2103+
jsonRawTextArea.val(requestDataJson);
2104+
updateCurlCommand(requestDataJson);
20942105
}
20952106

20962107
function validateJSON() {
2097-
var reqObj = JSON.parse($("#grpc-request-raw-text").val());
2108+
let requestDataJson = jsonRawTextArea.val();
2109+
updateCurlCommand(requestDataJson);
2110+
var reqObj = JSON.parse(requestDataJson);
20982111
rebuildRequestForm(reqObj, false);
20992112
}
21002113

@@ -2748,7 +2761,7 @@ window.initGRPCForm = function(services, svcDescs, mtdDescs, invokeURI, metadata
27482761
formServiceSelected(() => {
27492762
$("#grpc-method").val(item.method);
27502763
formMethodSelected(() => {
2751-
jsonRawTextArea.val(JSON.stringify(item.request.data, null, 2));
2764+
updateJSONRequest(item.request.data)
27522765
validateJSON();
27532766
// remove all rows
27542767
$("tr").remove('.metadataRow');

standalone/standalone.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func Handler(ch grpcdynamic.Channel, target string, methods []*desc.MethodDescri
7878
DefaultMetadata: uiOpts.defaultMetadata,
7979
Debug: uiOpts.debug,
8080
}
81-
webFormHTML := grpcui.WebFormContentsWithOptions("invoke", "metadata", methods, formOpts)
81+
webFormHTML := grpcui.WebFormContentsWithOptions("invoke", "metadata", target, methods, formOpts)
8282
indexContents := getIndexContents(uiOpts.indexTmpl, target, webFormHTML, uiOpts.tmplResources)
8383
indexResource := newResource("/", indexContents, "text/html; charset=utf-8", false)
8484
indexResource.MustRevalidate = true

webform.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ var (
5656
//
5757
// The returned HTML form requires that the contents of WebFormScript() have
5858
// already been loaded as a script in the page.
59-
func WebFormContents(invokeURI, metadataURI string, descs []*desc.MethodDescriptor) []byte {
60-
return WebFormContentsWithOptions(invokeURI, metadataURI, descs, WebFormOptions{})
59+
func WebFormContents(invokeURI, metadataURI string, target string, descs []*desc.MethodDescriptor) []byte {
60+
return WebFormContentsWithOptions(invokeURI, metadataURI, target, descs, WebFormOptions{})
6161
}
6262

6363
// WebFormOptions contains optional arguments when creating a gRPCui web form.
@@ -76,7 +76,7 @@ type WebFormOptions struct {
7676
// accepts an additional argument, options. This can be used to toggle the JS
7777
// code into debug logging and can also be used to define the set of metadata to
7878
// show in the web form by default (empty if unspecified).
79-
func WebFormContentsWithOptions(invokeURI, metadataURI string, descs []*desc.MethodDescriptor, opts WebFormOptions) []byte {
79+
func WebFormContentsWithOptions(invokeURI, metadataURI string, target string, descs []*desc.MethodDescriptor, opts WebFormOptions) []byte {
8080
type metadataEntry struct {
8181
Name, Value string
8282
}
@@ -89,13 +89,15 @@ func WebFormContentsWithOptions(invokeURI, metadataURI string, descs []*desc.Met
8989
MtdDescs map[string]string
9090
DefaultMetadata []metadataEntry
9191
Debug bool
92+
Target string
9293
}{
9394
InvokeURI: invokeURI,
9495
MetadataURI: metadataURI,
9596
SvcDescs: map[string]string{},
9697
Methods: map[string][]string{},
9798
MtdDescs: map[string]string{},
9899
Debug: os.Getenv("GRPC_WEBFORM_DEBUG") != "",
100+
Target: target,
99101
}
100102

101103
if opts.Debug != nil {

0 commit comments

Comments
 (0)