You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by he...@apache.org on 2013/09/20 17:16:33 UTC

[1/2] git commit: THRIFT-2164 Add a Get/Post Http Server to Node along with examples Client: nodejs Patch: Randy Abernethy

Updated Branches:
  refs/heads/master 7cf085e6c -> e594dccc8


THRIFT-2164 Add a Get/Post Http Server to Node along with examples
Client: nodejs
Patch: Randy Abernethy

small change to run on 0.6.x


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/a9e62481
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/a9e62481
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/a9e62481

Branch: refs/heads/master
Commit: a9e624811f07e23fc280932e6b05587ce69c1644
Parents: 6b43fa3
Author: Henrique Mendonça <he...@apache.org>
Authored: Fri Sep 20 16:47:02 2013 +0200
Committer: Henrique Mendonça <he...@apache.org>
Committed: Fri Sep 20 17:09:50 2013 +0200

----------------------------------------------------------------------
 lib/nodejs/examples/README.md          |   3 +
 lib/nodejs/examples/hello.html         |  65 ++++++++++++
 lib/nodejs/examples/hello.js           |  65 ++++++++++++
 lib/nodejs/examples/hello.thrift       |  27 +++++
 lib/nodejs/lib/thrift/index.js         |   3 +
 lib/nodejs/lib/thrift/server.js        |   8 +-
 lib/nodejs/lib/thrift/static_server.js | 153 ++++++++++++++++++++++++++++
 7 files changed, 319 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/a9e62481/lib/nodejs/examples/README.md
----------------------------------------------------------------------
diff --git a/lib/nodejs/examples/README.md b/lib/nodejs/examples/README.md
index a87581f..0773e60 100644
--- a/lib/nodejs/examples/README.md
+++ b/lib/nodejs/examples/README.md
@@ -25,5 +25,8 @@ NODE_PATH=../lib:../lib/thrift node server.js
 #Now run the client:
 NODE_PATH=../lib:../lib/thrift node client.js
 
+#For an example using JavaScript in the browser to connect to 
+#a node.js server look at hello.html, hello.js and hello.thrift
+
 
     

http://git-wip-us.apache.org/repos/asf/thrift/blob/a9e62481/lib/nodejs/examples/hello.html
----------------------------------------------------------------------
diff --git a/lib/nodejs/examples/hello.html b/lib/nodejs/examples/hello.html
new file mode 100644
index 0000000..0897ea9
--- /dev/null
+++ b/lib/nodejs/examples/hello.html
@@ -0,0 +1,65 @@
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ *   http://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>
+    <title>Apache Thrift JavaScript Browser Client Demo</title>
+    <script src="thrift.js" type="text/javascript"></script>
+    <script src="gen-js/HelloSvc.js" type="text/javascript"></script>
+    <script src="gen-js/TimesTwo.js" type="text/javascript"></script>
+</head>
+<body>
+    <h1>Apache Thrift JavaScript Browser Client Demo</h1>
+    <p>This html file demonstrates Apache Thrift JavaScrpt RPC between a browser client to a node.js server. Clicking the button below will call the hello_func() hosted by the Apache Thrift server at localhost:8585. The file hello.js contains the JavaScript node.js server required. Here are the steps to get the example running:</p>
+    <ol>
+        <li>Install Node.js <pre><a href="http://nodejs.org">nodejs.org</a></pre></li>
+        <li>Install Apache Thrift for node (note that the node package manager will create the node_modules folder in the current directory so make sure to run npm from the same directory as hello.js so that the server can find the Thrift libraries. This example requires Apache Thrift 0.9.2+) <pre>$ npm install thrift</pre></li>
+        <li>Compile the hello.idl for JavaScript and Node.js (you'll need to have the Apache Thrift compiler installed for this step. This also needs to be executed in the same directory as hello.js because hello.js and hello.html look for the gen-nodejs and gen-js directories here.)<pre>$ thrift -gen js -gen js:node hello.thrift</pre></li>
+        <li>Run the node server in the directory with the hello.html file<pre>$ node hello.js</pre></li>
+        <li>Copy the Apache Thrift JavaScript library, thrift.js, into the directory with this html file.<pre>$ cp ...../thrift.js .</pre>
+        <li>Reload this page in a browser through the node server using using the URL: <pre>http://localhost:8585/hello.html</pre>then click a button below to make an RPC call</li>
+    </ol>
+    <button id="btn">Get Message from Node Server</button>
+    <button id="btnDbl">Double 25</button>
+    <script type="text/javascript">
+        document.getElementById("btn").addEventListener("click", getMessage, false);
+
+        function getMessage() {
+            var transport = new Thrift.Transport("http://localhost:8585/hello");
+            var protocol  = new Thrift.Protocol(transport);
+            var client = new HelloSvcClient(protocol);
+            var msg = client.hello_func();
+            document.getElementById("output").innerHTML = msg;
+        }
+
+        document.getElementById("btnDbl").addEventListener("click", dblMessage, false);
+
+        function dblMessage() {
+            var transport = new Thrift.Transport("http://localhost:8585/dbl");
+            var protocol  = new Thrift.Protocol(transport);
+            var client = new TimesTwoClient(protocol);
+            var val = client.dbl(25);
+            document.getElementById("output2").innerHTML = val;
+        }
+    </script>
+    <h2>Server Response: <div id="output"></div></h2>
+    <h2>Server Dbl: <div id="output2"></div></h2>
+</body> 
+</html>
+

http://git-wip-us.apache.org/repos/asf/thrift/blob/a9e62481/lib/nodejs/examples/hello.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/examples/hello.js b/lib/nodejs/examples/hello.js
new file mode 100644
index 0000000..90634c9
--- /dev/null
+++ b/lib/nodejs/examples/hello.js
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ *   http://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.
+ */
+var thrift = require('thrift');
+var TBufferedTransport = require('thrift/transport').TBufferedTransport;
+var TJSONProtocol = require('thrift/protocol').TJSONProtocol;
+var HelloSvc = require('./gen-nodejs/HelloSvc.js');
+var TimesTwoSvc = require('./gen-nodejs/TimesTwo.js');
+
+var helloHandler = {
+	hello_func: function(result) {
+		this.call_counter = this.call_counter || 0;
+		console.log("Client call: " + (++this.call_counter));
+		result(null, "Hello Apache Thrift for JavaScript " + this.call_counter);
+	}
+}
+
+var timesTwoHandler = {
+	dbl: function(val, result) {
+		console.log("Client call: " + val);
+		result(null, val * 2);
+	}
+}
+
+var helloService = {
+	transport: TBufferedTransport,
+	protocol: TJSONProtocol,
+	cls: HelloSvc,
+	handler: helloHandler
+};
+
+var dblService = {
+	transport: TBufferedTransport,
+	protocol: TJSONProtocol,
+	cls: TimesTwoSvc,
+	handler: timesTwoHandler
+};
+
+var StaticHttpThriftServerOptions = {
+	staticFilePath: ".",
+	services: {
+		"/hello": helloService,
+		"/dbl": dblService,
+	}
+}
+
+var server = thrift.createStaticHttpThriftServer(StaticHttpThriftServerOptions);
+var port = 8585;
+server.listen(port);
+console.log("Http/Thrift Server running on port: " + port);

http://git-wip-us.apache.org/repos/asf/thrift/blob/a9e62481/lib/nodejs/examples/hello.thrift
----------------------------------------------------------------------
diff --git a/lib/nodejs/examples/hello.thrift b/lib/nodejs/examples/hello.thrift
new file mode 100644
index 0000000..deaf5a5
--- /dev/null
+++ b/lib/nodejs/examples/hello.thrift
@@ -0,0 +1,27 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ *   http://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.
+ */
+service HelloSvc {
+    string hello_func(),
+}
+
+service TimesTwo {
+    i64 dbl(1: i64 val),
+}
+
+

http://git-wip-us.apache.org/repos/asf/thrift/blob/a9e62481/lib/nodejs/lib/thrift/index.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/lib/thrift/index.js b/lib/nodejs/lib/thrift/index.js
index 92014fd..4992bed 100644
--- a/lib/nodejs/lib/thrift/index.js
+++ b/lib/nodejs/lib/thrift/index.js
@@ -31,6 +31,9 @@ exports.createHttpServer = server.createHttpServer;
 exports.httpMiddleware = server.httpMiddleware;
 exports.createMultiplexServer = server.createMultiplexServer;
 
+var static_server = require('./static_server')
+exports.createStaticHttpThriftServer = static_server.createStaticHttpThriftServer;
+
 exports.Int64 = require('node-int64')
 
 var mprocessor = require('./multiplexed_processor');

http://git-wip-us.apache.org/repos/asf/thrift/blob/a9e62481/lib/nodejs/lib/thrift/server.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/lib/thrift/server.js b/lib/nodejs/lib/thrift/server.js
index e9a7623..fee07d4 100644
--- a/lib/nodejs/lib/thrift/server.js
+++ b/lib/nodejs/lib/thrift/server.js
@@ -19,8 +19,8 @@
 var net = require('net');
 var http = require('http');
 
-var ttransport = require('./transport')
-  , TBinaryProtocol = require('./protocol').TBinaryProtocol;
+var ttransport = require('./transport'),
+    TBinaryProtocol = require('./protocol').TBinaryProtocol;
 
 
 exports.createMultiplexServer = function(processor, options) {
@@ -71,8 +71,6 @@ function httpRequestHandler(cls, handler, options) {
   var protocol = (options && options.protocol) ? options.protocol : TBinaryProtocol;
 
   return function(request, response) {
-    var self = this;
-
     request.on('data', transport.receiver(function(transportWithData) {
       var input = new protocol(transportWithData);
       var output = new protocol(new transport(undefined, function(buf) {
@@ -99,7 +97,7 @@ function httpRequestHandler(cls, handler, options) {
       }
     }));
   };
-};
+}
 
 exports.httpMiddleware = httpRequestHandler;
 

http://git-wip-us.apache.org/repos/asf/thrift/blob/a9e62481/lib/nodejs/lib/thrift/static_server.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/lib/thrift/static_server.js b/lib/nodejs/lib/thrift/static_server.js
new file mode 100644
index 0000000..0895c60
--- /dev/null
+++ b/lib/nodejs/lib/thrift/static_server.js
@@ -0,0 +1,153 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ *   http://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.
+ */
+var http = require('http');
+var url = require("url");
+var path = require("path");
+var fs = require("fs");
+
+var ttransport = require('./transport');
+var TBinaryProtocol = require('./protocol').TBinaryProtocol;
+
+/**
+ * @class
+ * @name StaticHttpThriftServerOptions
+ * @property {string} staticFilePath - Path to serve static files from, default
+ *                                is ".", use "" to disable static file service.
+ * @property {object} services - An object hash mapping service URIs to 
+ *                               ThriftServiceOptions objects.
+ * @see {@link ThriftServiceOptions}
+ */
+
+/**
+ * @class
+ * @name ThriftServiceOptions
+ * @property {object} transport - The layered transport to use (defaults to none).
+ * @property {object} protocol - The Thrift Protocol to use (defaults to TBinaryProtocol).
+ * @property {object} cls - The Thrift Service class generated by the IDL Compiler for the service.
+ * @property {object} handler - The handler methods for the Thrift Service.
+ */
+
+/** 
+ * Creates a Thrift server which can serve static files and/or one or
+ * more Thrift Services. 
+ * @param {StaticHttpThriftServerOptions} - The server configuration.
+ * @returns {object} - The Thrift server.
+ */
+exports.createStaticHttpThriftServer = function(options) {
+  //Set the static dir to serve files from
+  var baseDir = typeof options.staticFilePath != "string" ? process.cwd() : options.staticFilePath;
+
+  //Setup all of the services
+  var services = options.services;
+  for (svc in services) {
+    var svcObj = services[svc];
+    var processor = svcObj.cls.Processor || svcObj.cls;
+    svcObj.processor = new processor(svcObj.handler);
+    svcObj.transport = svcObj.transport ? svcObj.transport : ttransport.TBufferedTransport;
+    svcObj.protocol = svcObj.protocol ? svcObj.protocol : TBinaryProtocol;
+  }
+
+  //Handle POST methods
+  function processPost(request, response) {
+    var uri = url.parse(request.url).pathname;
+    var svc = services[uri];
+    if (!svc) {
+      //Todo: add support for non Thrift posts
+      response.writeHead(500);
+      response.end();
+      return;
+    }
+
+    request.on('data', svc.transport.receiver(function(transportWithData) {
+      var input = new svc.protocol(transportWithData);
+      var output = new svc.protocol(new svc.transport(undefined, function(buf) {
+        try {
+          response.writeHead(200);
+          response.end(buf);
+        } catch (err) {
+          response.writeHead(500);
+          response.end();
+        }
+      }));
+
+      try {
+        svc.processor.process(input, output);
+        transportWithData.commitPosition();
+      }
+      catch (err) {
+        if (err instanceof ttransport.InputBufferUnderrunError) {
+          transportWithData.rollbackPosition();
+        }
+        else {
+          response.writeHead(500);
+          response.end();
+        }
+      }
+    }));
+  }
+
+  //Handle GET methods
+  function processGet(request, response) {
+    //An empty string base directory means do not serve static files
+    if ("" == baseDir) {
+      response.writeHead(404);
+      response.end();
+      return;      
+    }
+    //Locate the file requested
+    var uri = url.parse(request.url).pathname;
+    var filename = path.join(baseDir, uri);
+    path.exists(filename, function(exists) {
+      if (!exists) {
+        response.writeHead(404);
+        response.end();
+        return;
+      }
+     
+      if (fs.statSync(filename).isDirectory()) {
+        filename += '/index.html';
+      }
+     
+      fs.readFile(filename, "binary", function(err, file) {
+        if (err) {        
+          response.writeHead(500);
+          response.end(err + "\n");
+          return;
+        }
+     
+        response.writeHead(200);
+        response.write(file, "binary");
+        response.end();
+      });
+    });
+  }
+
+  //Return the server
+  return http.createServer(function(request, response) {
+    if (request.method === 'POST') {
+      processPost(request, response);
+    } else if (request.method === 'GET') {
+      processGet(request, response);
+    } else {
+      response.writeHead(500);
+      response.end();
+    }
+  });
+};
+


[2/2] git commit: Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/thrift

Posted by he...@apache.org.
Merge branch 'master' of https://git-wip-us.apache.org/repos/asf/thrift

Conflicts:
	lib/nodejs/lib/thrift/static_server.js


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/e594dccc
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/e594dccc
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/e594dccc

Branch: refs/heads/master
Commit: e594dccc85740ecc1d61f236e9135c5b90301d31
Parents: a9e6248 7cf085e
Author: Henrique Mendonça <he...@apache.org>
Authored: Fri Sep 20 17:15:55 2013 +0200
Committer: Henrique Mendonça <he...@apache.org>
Committed: Fri Sep 20 17:15:55 2013 +0200

----------------------------------------------------------------------
 lib/nodejs/lib/thrift/static_server.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/e594dccc/lib/nodejs/lib/thrift/static_server.js
----------------------------------------------------------------------
diff --cc lib/nodejs/lib/thrift/static_server.js
index 0895c60,1627328..a1d2eaa
--- a/lib/nodejs/lib/thrift/static_server.js
+++ b/lib/nodejs/lib/thrift/static_server.js
@@@ -125,7 -125,7 +125,7 @@@ exports.createStaticHttpThriftServer = 
        }
       
        fs.readFile(filename, "binary", function(err, file) {
-         if (err) {        
 -        if(err) {        
++        if (err) {
            response.writeHead(500);
            response.end(err + "\n");
            return;