-
Notifications
You must be signed in to change notification settings - Fork 764
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #212 from stanley-cheung/commonjs-example
Added commonjs-example Dockerfile
- Loading branch information
Showing
12 changed files
with
281 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
net/grpc/gateway/docker/commonjs_client_example/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
FROM grpc-web:prereqs | ||
|
||
ARG EXAMPLE_DIR=/github/grpc-web/net/grpc/gateway/examples/echo | ||
|
||
RUN cd /github/grpc-web/packages/grpc-web && \ | ||
rm -rf node_modules && \ | ||
npm install && \ | ||
npm run build && \ | ||
npm link | ||
|
||
RUN protoc -I=$EXAMPLE_DIR echo.proto \ | ||
--plugin=protoc-gen-grpc-web=\ | ||
/github/grpc-web/javascript/net/grpc/web/protoc-gen-grpc-web \ | ||
--js_out=import_style=commonjs:\ | ||
$EXAMPLE_DIR/commonjs-example \ | ||
--grpc-web_out=import_style=commonjs,mode=grpcweb,out=echo_grpc_pb.js:\ | ||
$EXAMPLE_DIR/commonjs-example | ||
|
||
RUN cd $EXAMPLE_DIR/commonjs-example && \ | ||
rm -rf node_modules && \ | ||
npm install && \ | ||
npm link grpc-web && \ | ||
./node_modules/.bin/browserify client.js > out.js | ||
|
||
EXPOSE 8081 | ||
CMD ["nginx"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const {EchoRequest, | ||
ServerStreamingEchoRequest} = require('./echo_pb.js'); | ||
const {EchoServiceClient} = require('./echo_grpc_pb.js'); | ||
const {EchoApp} = require('../echoapp.js'); | ||
const grpc = {}; | ||
grpc.web = require('grpc-web'); | ||
|
||
var echoService = new EchoServiceClient('http://localhost:8080', null, null); | ||
|
||
var echoApp = new EchoApp( | ||
echoService, | ||
{ | ||
EchoRequest: EchoRequest, | ||
ServerStreamingEchoRequest: ServerStreamingEchoRequest | ||
}, | ||
{ | ||
checkGrpcStatusCode: function(status) { | ||
if (status.code != grpc.web.StatusCode.OK) { | ||
EchoApp.addRightMessage('Error code: '+status.code+' "'+status.details+'"'); | ||
} | ||
} | ||
} | ||
); | ||
|
||
echoApp.load(); |
40 changes: 40 additions & 0 deletions
40
net/grpc/gateway/examples/echo/commonjs-example/echo_commonjs_test.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!-- Copyright 2018 Google LLC --> | ||
|
||
<!-- Licensed under the Apache License, Version 2.0 (the "License"); --> | ||
<!-- you may not use this file except in compliance with the License. --> | ||
<!-- You may obtain a copy of the License at --> | ||
|
||
<!-- https://www.apache.org/licenses/LICENSE-2.0 --> | ||
|
||
<!-- Unless required by applicable law or agreed to in writing, software --> | ||
<!-- distributed under the License is distributed on an "AS IS" BASIS, --> | ||
<!-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --> | ||
<!-- See the License for the specific language governing permissions and --> | ||
<!-- limitations under the License. --> | ||
|
||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Echo Example</title> | ||
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> | ||
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> | ||
<script src="./out.js"></script> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="row" id="first"> | ||
<div class="form-group"> | ||
<div class="input-group"> | ||
<input type="text" class="form-control" id="msg"> | ||
<span class="input-group-btn"> | ||
<button class="btn btn-primary" type="button" id="send">Send | ||
</button> | ||
</span> | ||
</div> | ||
<p class="help-block">Example: "Hello", "4 Hello"</p> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |
10 changes: 10 additions & 0 deletions
10
net/grpc/gateway/examples/echo/commonjs-example/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "grpc-web-commonjs-example", | ||
"version": "0.1.0", | ||
"description": "gRPC-Web CommonJS client example", | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"browserify": "16", | ||
"google-protobuf": "3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
const echoapp = {}; | ||
|
||
echoapp.EchoApp = function(echoService, ctors, handlers) { | ||
this.echoService = echoService; | ||
this.ctors = ctors; | ||
this.handlers = handlers; | ||
}; | ||
|
||
echoapp.EchoApp.INTERVAL = 500; // ms | ||
echoapp.EchoApp.MAX_STREAM_MESSAGES = 50; | ||
|
||
echoapp.EchoApp.addMessage = function(message, cssClass) { | ||
$("#first").after( | ||
$("<div/>").addClass("row").append( | ||
$("<h2/>").append( | ||
$("<span/>").addClass("label " + cssClass).text(message)))); | ||
}; | ||
|
||
echoapp.EchoApp.addLeftMessage = function(message) { | ||
this.addMessage(message, "label-primary pull-left"); | ||
}; | ||
|
||
echoapp.EchoApp.addRightMessage = function(message) { | ||
this.addMessage(message, "label-default pull-right"); | ||
}; | ||
|
||
echoapp.EchoApp.prototype.echo = function(msg) { | ||
echoapp.EchoApp.addLeftMessage(msg); | ||
var unaryRequest = new this.ctors.EchoRequest(); | ||
unaryRequest.setMessage(msg); | ||
this.echoService.echo(unaryRequest, {"custom-header-1": "value1"}, | ||
function(err, response) { | ||
if (err) { | ||
echoapp.EchoApp.addRightMessage('Error code: '+err.code+' "'+err.message+'"'); | ||
} else { | ||
setTimeout(function () { | ||
echoapp.EchoApp.addRightMessage(response.getMessage()); | ||
}, echoapp.EchoApp.INTERVAL); | ||
} | ||
}); | ||
}; | ||
|
||
echoapp.EchoApp.prototype.echoError = function(msg) { | ||
echoapp.EchoApp.addLeftMessage(msg); | ||
var unaryRequest = new this.ctors.EchoRequest(); | ||
unaryRequest.setMessage(msg); | ||
this.echoService.echoAbort(unaryRequest, {}, function(err, response) { | ||
if (err) { | ||
echoapp.EchoApp.addRightMessage('Error code: '+err.code+' "'+err.message+'"'); | ||
} | ||
}); | ||
}; | ||
|
||
echoapp.EchoApp.prototype.repeatEcho = function(msg, count) { | ||
echoapp.EchoApp.addLeftMessage(msg); | ||
if (count > echoapp.EchoApp.MAX_STREAM_MESSAGES) { | ||
count = echoapp.EchoApp.MAX_STREAM_MESSAGES; | ||
} | ||
var streamRequest = new this.ctors.ServerStreamingEchoRequest(); | ||
streamRequest.setMessage(msg); | ||
streamRequest.setMessageCount(count); | ||
streamRequest.setMessageInterval(echoapp.EchoApp.INTERVAL); | ||
|
||
var stream = this.echoService.serverStreamingEcho( | ||
streamRequest, | ||
{"custom-header-1": "value1"}); | ||
var self = this; | ||
stream.on('data', function(response) { | ||
echoapp.EchoApp.addRightMessage(response.getMessage()); | ||
}); | ||
stream.on('status', function(status) { | ||
self.handlers.checkGrpcStatusCode(status); | ||
if (status.metadata) { | ||
console.log("Received metadata"); | ||
console.log(status.metadata); | ||
} | ||
}); | ||
stream.on('error', function(err) { | ||
echoapp.EchoApp.addRightMessage('Error code: '+err.code+' "'+err.message+'"'); | ||
}); | ||
stream.on('end', function() { | ||
console.log("stream end signal received"); | ||
}); | ||
}; | ||
|
||
echoapp.EchoApp.prototype.send = function(e) { | ||
var msg = $("#msg").val().trim(); | ||
$("#msg").val(''); // clear the text box | ||
if (!msg) return false; | ||
|
||
if (msg.indexOf(' ') > 0) { | ||
var count = msg.substr(0, msg.indexOf(' ')); | ||
if (/^\d+$/.test(count)) { | ||
this.repeatEcho(msg.substr(msg.indexOf(' ') + 1), count); | ||
} else if (count == 'err') { | ||
this.echoError(msg.substr(msg.indexOf(' ') + 1)); | ||
} else { | ||
this.echo(msg); | ||
} | ||
} else { | ||
this.echo(msg); | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
echoapp.EchoApp.prototype.load = function() { | ||
var self = this; | ||
$(document).ready(function() { | ||
// event handlers | ||
$("#send").click(self.send); | ||
$("#msg").keyup(function (e) { | ||
if (e.keyCode == 13) self.send(); // enter key | ||
return false; | ||
}); | ||
|
||
$("#msg").focus(); | ||
}); | ||
}; | ||
|
||
module.exports = echoapp; |
Oops, something went wrong.