]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | #!/usr/bin/env node |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * 'License'); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * 'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | const fs = require("fs"); | |
23 | const path = require("path"); | |
24 | const thrift = require("../lib/thrift"); | |
25 | const program = require("commander"); | |
26 | const helpers = require("./helpers"); | |
27 | ||
28 | program | |
29 | .option( | |
30 | "-p, --protocol <protocol>", | |
31 | "Set thrift protocol (binary|compact|json)", | |
32 | "binary" | |
33 | ) | |
34 | .option( | |
35 | "-t, --transport <transport>", | |
36 | "Set thrift transport (buffered|framed|http)", | |
37 | "buffered" | |
38 | ) | |
39 | .option("--ssl", "use ssl transport") | |
40 | .option("--port <port>", "Set thrift server port", 9090) | |
41 | .option("--domain-socket <path>", "Set thift server unix domain socket") | |
42 | .option( | |
43 | "-t, --type <type>", | |
44 | "Select server type (http|multiplex|tcp|websocket)", | |
45 | "tcp" | |
46 | ) | |
47 | .option("--callback", "test with callback style functions") | |
48 | .option("--es6", "Use es6 code") | |
49 | .option("--es5", "Use es5 code") | |
50 | .parse(process.argv); | |
51 | ||
52 | const ThriftTest = require(`./${helpers.genPath}/ThriftTest`); | |
53 | const SecondService = require(`./${helpers.genPath}/SecondService`); | |
54 | const { ThriftTestHandler } = require("./test_handler"); | |
55 | ||
56 | const port = program.port; | |
57 | const domainSocket = program.domainSocket; | |
58 | const ssl = program.ssl; | |
59 | ||
60 | let type = program.type; | |
61 | if (program.transport === "http") { | |
62 | program.transport = "buffered"; | |
63 | type = "http"; | |
64 | } | |
65 | ||
66 | let options = { | |
67 | transport: helpers.transports[program.transport], | |
68 | protocol: helpers.protocols[program.protocol] | |
69 | }; | |
70 | ||
71 | if (type === "http" || type === "websocket") { | |
72 | options.handler = ThriftTestHandler; | |
73 | options.processor = ThriftTest; | |
74 | ||
75 | options = { | |
76 | services: { "/test": options }, | |
77 | cors: { | |
78 | "*": true | |
79 | } | |
80 | }; | |
81 | } | |
82 | ||
83 | let processor; | |
84 | if (type === "multiplex") { | |
85 | const SecondServiceHandler = { | |
86 | secondtestString: function(thing, result) { | |
87 | console.log('testString("' + thing + '")'); | |
88 | result(null, 'testString("' + thing + '")'); | |
89 | } | |
90 | }; | |
91 | ||
92 | processor = new thrift.MultiplexedProcessor(); | |
93 | ||
94 | processor.registerProcessor( | |
95 | "ThriftTest", | |
96 | new ThriftTest.Processor(ThriftTestHandler) | |
97 | ); | |
98 | ||
99 | processor.registerProcessor( | |
100 | "SecondService", | |
101 | new SecondService.Processor(SecondServiceHandler) | |
102 | ); | |
103 | } | |
104 | ||
105 | if (ssl) { | |
106 | if ( | |
107 | type === "tcp" || | |
108 | type === "multiplex" || | |
109 | type === "http" || | |
110 | type === "websocket" | |
111 | ) { | |
112 | options.tls = { | |
113 | key: fs.readFileSync(path.resolve(__dirname, "server.key")), | |
114 | cert: fs.readFileSync(path.resolve(__dirname, "server.crt")) | |
115 | }; | |
116 | } | |
117 | } | |
118 | ||
119 | let server; | |
120 | if (type === "tcp") { | |
121 | server = thrift.createServer(ThriftTest, ThriftTestHandler, options); | |
122 | } else if (type === "multiplex") { | |
123 | server = thrift.createMultiplexServer(processor, options); | |
124 | } else if (type === "http" || type === "websocket") { | |
125 | server = thrift.createWebServer(options); | |
126 | } | |
127 | ||
128 | if (domainSocket) { | |
129 | server.listen(domainSocket); | |
130 | } else if ( | |
131 | type === "tcp" || | |
132 | type === "multiplex" || | |
133 | type === "http" || | |
134 | type === "websocket" | |
135 | ) { | |
136 | server.listen(port); | |
137 | } |