Henrique Mendonça | a9e6248 | 2013-09-20 16:47:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | var thrift = require('thrift'); |
| 20 | var TBufferedTransport = require('thrift/transport').TBufferedTransport; |
| 21 | var TJSONProtocol = require('thrift/protocol').TJSONProtocol; |
| 22 | var HelloSvc = require('./gen-nodejs/HelloSvc.js'); |
| 23 | var TimesTwoSvc = require('./gen-nodejs/TimesTwo.js'); |
| 24 | |
| 25 | var helloHandler = { |
| 26 | hello_func: function(result) { |
| 27 | this.call_counter = this.call_counter || 0; |
| 28 | console.log("Client call: " + (++this.call_counter)); |
| 29 | result(null, "Hello Apache Thrift for JavaScript " + this.call_counter); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | var timesTwoHandler = { |
| 34 | dbl: function(val, result) { |
| 35 | console.log("Client call: " + val); |
| 36 | result(null, val * 2); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | var helloService = { |
| 41 | transport: TBufferedTransport, |
| 42 | protocol: TJSONProtocol, |
| 43 | cls: HelloSvc, |
| 44 | handler: helloHandler |
| 45 | }; |
| 46 | |
| 47 | var dblService = { |
| 48 | transport: TBufferedTransport, |
| 49 | protocol: TJSONProtocol, |
| 50 | cls: TimesTwoSvc, |
| 51 | handler: timesTwoHandler |
| 52 | }; |
| 53 | |
| 54 | var StaticHttpThriftServerOptions = { |
| 55 | staticFilePath: ".", |
| 56 | services: { |
| 57 | "/hello": helloService, |
| 58 | "/dbl": dblService, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | var server = thrift.createStaticHttpThriftServer(StaticHttpThriftServerOptions); |
| 63 | var port = 8585; |
| 64 | server.listen(port); |
| 65 | console.log("Http/Thrift Server running on port: " + port); |