1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | var fs = require('fs-extra');
|
32 | var ask = require('readline-sync');
|
33 |
|
34 | function run() {
|
35 |
|
36 | var pathx = require('path');
|
37 |
|
38 | var startFile = ask.questionPath('Startup filename: ', {
|
39 | isFile: true,
|
40 | exists: null
|
41 | });
|
42 |
|
43 | var fext = pathx.extname(startFile);
|
44 | var froot = pathx.basename(startFile, fext);
|
45 | var rootPath = pathx.dirname(startFile);
|
46 |
|
47 | var configFile = pathx.join(rootPath, froot + '_config.json');
|
48 | var routesFile = pathx.join(rootPath, froot + '_routes.json');
|
49 |
|
50 | var config = {
|
51 | managementPassword: 'keepThisSecret!',
|
52 | serverName: 'QEWD Server',
|
53 | port: 8080,
|
54 | poolSize: 2,
|
55 | database: {
|
56 | type: 'gtm'
|
57 | }
|
58 | };
|
59 |
|
60 | if (fs.existsSync(configFile)) {
|
61 | config = require(configFile);
|
62 | }
|
63 |
|
64 | console.log('startFile = ' + startFile);
|
65 | console.log('configFile = ' + configFile);
|
66 |
|
67 | console.log('config = ' + JSON.stringify(config));
|
68 |
|
69 | var mgtPW = ask.question('QEWD Monitor Management Password (' + config.managementPassword + '): ', {
|
70 | defaultInput: config.managementPassword
|
71 | });
|
72 |
|
73 | var serverName = ask.question('Server name to appear in QEWD Monitor (' + config.serverName + '): ', {
|
74 | defaultInput: config.serverName
|
75 | });
|
76 |
|
77 | var port = ask.questionInt('Web Server Listener Port (' + config.port + '): ', {
|
78 | defaultInput: config.port
|
79 | });
|
80 |
|
81 | var poolSize = ask.questionInt('Maximum Worker Pool Size (' + config.poolSize + '): ', {
|
82 | defaultInput: config.poolSize
|
83 | });
|
84 |
|
85 | var dbLookup = {
|
86 | 'Cache': 'cache',
|
87 | 'GT.M': 'gtm',
|
88 | 'YottaDB': 'gtm',
|
89 | 'Redis': 'redis',
|
90 | }
|
91 |
|
92 | var dbs = [
|
93 | 'Cache',
|
94 | 'GT.M',
|
95 | 'YottaDB',
|
96 | 'Redis'
|
97 | ];
|
98 |
|
99 | var dbValue = {
|
100 | cache: 1,
|
101 | gtm: 2,
|
102 | redis: 4
|
103 | };
|
104 |
|
105 | var dbDefault = dbValue[config.database.type];
|
106 |
|
107 | var dbIndex = ask.keyInSelect(dbs, 'Database: ', {
|
108 | defaultInput: dbDefault,
|
109 | cancel: false
|
110 | });
|
111 |
|
112 | var db = dbLookup[dbs[dbIndex]];
|
113 |
|
114 | var params;
|
115 |
|
116 | if (db === 'cache') {
|
117 |
|
118 | console.log('Cache-specific parameters:');
|
119 |
|
120 | var mgrPath = ask.questionPath('Mgr Directory Path (/opt/cache/mgr): ', {
|
121 | defaultInput: '/opt/cache/mgr'
|
122 | });
|
123 |
|
124 | var username = ask.question('Username (_SYSTEM): ', {
|
125 | defaultInput: '_SYSTEM'
|
126 | });
|
127 |
|
128 | var password = ask.question('Password (SYS): ', {
|
129 | defaultInput: 'SYS'
|
130 | });
|
131 |
|
132 | var namespace = ask.question('Namespace (USER): ', {
|
133 | defaultInput: 'USER'
|
134 | });
|
135 |
|
136 | params = {
|
137 | path: mgrPath,
|
138 | username: username,
|
139 | password: password,
|
140 | namespace: namespace
|
141 | };
|
142 |
|
143 | }
|
144 |
|
145 | var config = {
|
146 | managementPassword: mgtPW,
|
147 | serverName: serverName,
|
148 | port: port,
|
149 | poolSize: poolSize,
|
150 | database: {
|
151 | type: db,
|
152 | params: params
|
153 | }
|
154 | };
|
155 |
|
156 | fs.writeJsonSync(configFile, config, {spaces: 2});
|
157 |
|
158 | console.log('-----------------');
|
159 | console.log(' ');
|
160 | console.log('Now specify a top-level route, eg /api');
|
161 |
|
162 | var uri = ask.questionPath('Top-Level REST API route (/api): ', {
|
163 | defaultInput: '/api',
|
164 | exists: null
|
165 | });
|
166 |
|
167 | var module = ask.question('Name of module that will handle requests for this path: (test): ', {
|
168 | defaultInput: 'test'
|
169 | });
|
170 |
|
171 | var routes = [
|
172 | {
|
173 | path: uri,
|
174 | module: module
|
175 | }
|
176 | ];
|
177 |
|
178 | fs.writeJsonSync(routesFile, routes, {spaces: 2});
|
179 |
|
180 | var addExamples = ask.keyInYNStrict('Add a set of API examples?');
|
181 |
|
182 | var text = [
|
183 | "var config = require('" + configFile + "');",
|
184 | "var routes = require('" + routesFile + "');",
|
185 | "var qewd = require('qewd').master;",
|
186 | "qewd.start(config, routes);"
|
187 | ];
|
188 |
|
189 | fs.outputFileSync(startFile, text.join('\n'));
|
190 |
|
191 | var packageJsonFile = pathx.join(rootPath, 'node_modules', module, 'package.json');
|
192 |
|
193 | var package = {
|
194 | name: module,
|
195 | version: '1.0.0',
|
196 | main: 'index.js'
|
197 | };
|
198 |
|
199 | fs.outputJsonSync(packageJsonFile, package, {spaces: 2});
|
200 |
|
201 | text = [
|
202 | "var router = require('qewd-router');",
|
203 | "var routes = require('./routeModule');",
|
204 | "module.exports = {",
|
205 | " restModule: true,",
|
206 | " init: function() {",
|
207 | " routes = router.initialise(routes, module.exports);",
|
208 | " }",
|
209 | "};"
|
210 | ]
|
211 |
|
212 | var indexFile = pathx.join(rootPath, 'node_modules', module, 'index.js');
|
213 | fs.outputFileSync(indexFile, text.join('\n'));
|
214 |
|
215 | text = [
|
216 | "var routeDef = require('./routes.json');",
|
217 | "var routes = [];",
|
218 | "routeDef.forEach(function(route) {",
|
219 | " route.handler = require('./handlers/' + route.use);",
|
220 | " delete route.use;",
|
221 | " routes.push(route);",
|
222 | "});",
|
223 | "module.exports = routes;"
|
224 | ];
|
225 |
|
226 | var routeFile = pathx.join(rootPath, 'node_modules', module, 'routeModule.js');
|
227 | fs.outputFileSync(routeFile, text.join('\n'));
|
228 |
|
229 | var routes = [
|
230 | {
|
231 | path: '/api/test/:input',
|
232 | method: 'GET',
|
233 | use: 'test_get'
|
234 | }
|
235 | ];
|
236 |
|
237 | text = [
|
238 | "module.exports = function(args, finished) {",
|
239 | " finished({test_get: 'Ran OK', args: args});",
|
240 | "};"
|
241 | ];
|
242 |
|
243 | var handlerFile = pathx.join(rootPath, 'node_modules', module, 'handlers', 'test_get.js');
|
244 | fs.outputFileSync(handlerFile, text.join('\n'));
|
245 |
|
246 | if (addExamples) {
|
247 |
|
248 | routes.push({
|
249 | path: '/api/test',
|
250 | method: 'POST',
|
251 | use: 'test_post'
|
252 | });
|
253 |
|
254 | text = [
|
255 | "module.exports = function(args, finished) {",
|
256 | " finished({test_post: 'Ran OK', args: args});",
|
257 | "};"
|
258 | ];
|
259 |
|
260 | var handlerFile = pathx.join(rootPath, 'node_modules', module, 'handlers', 'test_post.js');
|
261 | fs.outputFileSync(handlerFile, text.join('\n'));
|
262 |
|
263 | }
|
264 |
|
265 | routeFile = pathx.join(rootPath, 'node_modules', module, 'routes.json');
|
266 | fs.outputJsonSync(routeFile, routes, {spaces: 2});
|
267 |
|
268 | }
|
269 |
|
270 | run();
|
271 |
|